JavaEE第1次作业

This commit is contained in:
maxxie 2024-10-19 22:21:46 +08:00
commit 85f0621172
42 changed files with 1257 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

20
.idea/compiler.xml Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="SimpleStudentManagementSystem" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel target="17" />
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="SimpleStudentManagementSystem" options="-parameters" />
</option>
</component>
</project>

6
.idea/encodings.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/SimpleStudentManagementSystem/src/main/java" charset="UTF-8" />
</component>
</project>

20
.idea/jarRepositories.xml Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

14
.idea/misc.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/C05-1-MyBatis-XML-Demo/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/JavaEEHW1.iml" filepath="$PROJECT_DIR$/JavaEEHW1.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

8
JavaEEHW1.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="GENERAL_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

View File

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>edu.maxxie</groupId>
<artifactId>SimpleStudentManagementSystem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SimpleStudentManagementSystem</name>
<description>SimpleStudentManagementSystem</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<!-- 热部署 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,15 @@
package edu.maxxie;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication()
@MapperScan("edu.maxxie.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,30 @@
package edu.maxxie.controller;
import edu.maxxie.pojo.Student;
import edu.maxxie.service.AccountStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class AccountStudentController {
private final AccountStudentService accountStudentService;
@Autowired
public AccountStudentController(AccountStudentService accountStudentService) {
this.accountStudentService = accountStudentService;
}
@PostMapping("/accountstudent")
public String addAccountStudent(Student student,
Model model){
accountStudentService.addStudentAccount(student);
return "redirect:/students";
}
@GetMapping("/deleteaccountstudent/{account}")
public String deleteAccountStudent(@PathVariable String account, Model model){
System.out.println(account);
accountStudentService.deleteStudentAccount(account);
return "redirect:/students";
}
}

View File

@ -0,0 +1,35 @@
package edu.maxxie.controller;
import edu.maxxie.pojo.Account;
import edu.maxxie.service.AccountService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class LoginController {
private final AccountService accountService;
@Autowired
public LoginController(AccountService accountService) {
this.accountService = accountService;
}
@GetMapping({"/", "/login"})
public String loginPage(Model model){
model.addAttribute("title","教务系统");
return "login";
}
@PostMapping("/login")
public String login(String username, String password, Model model){
boolean result = accountService.validate(username, password);
if (result){
return "index";
}
return "login";
}
}

View File

@ -0,0 +1,54 @@
package edu.maxxie.controller;
import edu.maxxie.pojo.Account;
import edu.maxxie.pojo.Student;
import edu.maxxie.service.AccountService;
import edu.maxxie.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
// 加载页面
@GetMapping("/studentpage")
public String getStudentPage(){
return "student";
}
@GetMapping("addstudentpage")
public String addStudentPage(@CookieValue(value = "username")String username, Model model){
Account account = new Account();
account.setAccount(username);
model.addAttribute("account",account);
return "addstudent";
}
// 处理数据
@GetMapping("/students")
public String getAllStudents(Model model){
List<Student> students = studentService.getAllStudents();
model.addAttribute("students",students);
return "student";
}
@GetMapping("updatestudentpage/{id}")
public String updateStudentPage(@PathVariable String id,
Model model){
Student student = studentService.getStudentById(id);
System.out.println(student);
model.addAttribute("student",student);
return "updatestudent";
}
@PostMapping("/updatestudent")
public String updateStudent(@ModelAttribute Student student) {
studentService.updateStudent(student);
return "redirect:/students";
}
}

View File

@ -0,0 +1,18 @@
package edu.maxxie.mapper;
import edu.maxxie.pojo.Account;
import java.util.List;
public interface AccountMapper {
// 查询账号
Account getAccount(String account);
// 添加账号
void insertAccount(Account account);
// 根据账号获取学生信息
List<Account> getAccountWithDetails();
void deleteByAccount(String account);
}

View File

@ -0,0 +1,24 @@
package edu.maxxie.mapper;
import edu.maxxie.pojo.Score;
import java.util.List;
public interface ScoreMapper {
// 新增成绩
void insert(Score score);
// 更新成绩
void update(Score score);
// 根据id删除成绩
void deleteById(Integer id);
// 批量删除成绩
void deleteByIds(List<Integer> ids);
// 根据no查询成绩
List<Score> getAll();
}

View File

@ -0,0 +1,32 @@
package edu.maxxie.mapper;
import edu.maxxie.pojo.Student;
import org.apache.ibatis.annotations.*;
import java.util.List;
// 在运行时会自动生成该接口的实现类对象 并将该对交给IOC容器管理 成为一个bean
public interface StudentMapper {
// 查询全部学生信息
List<Student> getAll();
// 根据id查询学生
Student getById(String id);
// 添加学生
void insertStudent(Student student);
// 动态条件查询 根据姓名字数和性别查询
List<Student> getByCondition(Integer len, Integer gender);
// 动态更新学生
void update(Student student);
List<Student> getAllWithGender();
Student getStudentScoresById(String id);
void deleteById(String id);
}

View File

@ -0,0 +1,8 @@
package edu.maxxie.mapper;
import edu.maxxie.pojo.StudentScore;
public interface StudentScoreMapper {
StudentScore getByNo(String id);
}

View File

@ -0,0 +1,13 @@
package edu.maxxie.pojo;
import lombok.Data;
@Data
public class Account extends BaseEntity {
private Integer id;
private String account;
private String password;
private Integer role = 1;
// 一对一查询
private Student studentDetails;
}

View File

@ -0,0 +1,13 @@
package edu.maxxie.pojo;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class BaseEntity {
private Integer createBy;
private LocalDateTime createTime;
private Integer updateBy;
private LocalDateTime updateTime;
}

View File

@ -0,0 +1,9 @@
package edu.maxxie.pojo;
import lombok.Data;
@Data
public class Course extends BaseEntity{
private Integer id;
private String name;
}

View File

@ -0,0 +1,9 @@
package edu.maxxie.pojo;
import lombok.Data;
@Data
public class Gender extends BaseEntity {
private Integer id;
private String name;
}

View File

@ -0,0 +1,20 @@
package edu.maxxie.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
public class Score extends BaseEntity{
private Integer id;
private String studentId;
private String courseId;
private Integer score;
private LocalDateTime createTime;
// 学生和课程是一的一方
private Student student;
private Course course;
}

View File

@ -0,0 +1,20 @@
package edu.maxxie.pojo;
import lombok.*;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
@Data
public class Student extends BaseEntity {
private String id;
private String name;
private Integer gender;
// @DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate birthday;
// 性别是一的一方
private Gender genderDetails;
private List<Score> scores;
}

View File

@ -0,0 +1,11 @@
package edu.maxxie.pojo;
import lombok.Data;
import java.util.List;
@Data
public class StudentScore {
private Student student;
private List<Score> scores;
}

View File

@ -0,0 +1,9 @@
package edu.maxxie.service;
import edu.maxxie.pojo.Account;
public interface AccountService {
// 验证数据集账号密码
boolean validate(String username, String password);
void insertAccount(Account account);
}

View File

@ -0,0 +1,12 @@
package edu.maxxie.service;
import edu.maxxie.pojo.Student;
import org.springframework.transaction.annotation.Transactional;
public interface AccountStudentService {
@Transactional
void addStudentAccount(Student student);
@Transactional
void deleteStudentAccount(String account);
}

View File

@ -0,0 +1,12 @@
package edu.maxxie.service;
import edu.maxxie.pojo.Student;
import java.util.List;
public interface StudentService {
List<Student> getAllStudents();
void insertStudent(Student student);
Student getStudentById(String id);
void updateStudent(Student student);
}

View File

@ -0,0 +1,31 @@
package edu.maxxie.service.impl;
import edu.maxxie.mapper.AccountMapper;
import edu.maxxie.pojo.Account;
import edu.maxxie.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AccountServiceImpl implements AccountService {
private final AccountMapper accountMapper;
@Autowired
public AccountServiceImpl(AccountMapper accountMapper) {
this.accountMapper = accountMapper;
}
@Override
public boolean validate(String username, String password) {
Account dbAccount = accountMapper.getAccount(username);
if(dbAccount != null && dbAccount.getPassword().equals(password)){
return true;
}
return false;
}
@Override
public void insertAccount(Account account) {
accountMapper.insertAccount(account);
}
}

View File

@ -0,0 +1,52 @@
package edu.maxxie.service.impl;
import edu.maxxie.mapper.AccountMapper;
import edu.maxxie.mapper.StudentMapper;
import edu.maxxie.pojo.Account;
import edu.maxxie.pojo.Student;
import edu.maxxie.service.AccountService;
import edu.maxxie.service.AccountStudentService;
import edu.maxxie.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
@Service
public class AccountStudentServiceImpl implements AccountStudentService {
private final AccountMapper accountMapper;
private final StudentMapper studentMapper;
@Autowired
public AccountStudentServiceImpl(AccountMapper accountMapper, StudentMapper studentMapper) {
this.accountMapper = accountMapper;
this.studentMapper = studentMapper;
}
@Override
@Transactional
public void addStudentAccount(Student student) {
LocalDateTime now = LocalDateTime.now();
// 在数据库中创建Account
Account account = new Account();
account.setAccount(student.getId());// 学号
account.setPassword("12346"); // 默认密码
account.setRole(1); // 默认角色
account.setCreateBy(1);
account.setCreateTime(now);
accountMapper.insertAccount(account);
// 在数据库中创建Student
student.setCreateBy(1);
student.setCreateTime(now);
studentMapper.insertStudent(student);
}
@Override
@Transactional
public void deleteStudentAccount(String account) {
// 在数据库中删除student
studentMapper.deleteById(account);
// 在数据库中删除account
accountMapper.deleteByAccount(account);
}
}

View File

@ -0,0 +1,45 @@
package edu.maxxie.service.impl;
import edu.maxxie.mapper.AccountMapper;
import edu.maxxie.mapper.StudentMapper;
import edu.maxxie.pojo.Account;
import edu.maxxie.pojo.Student;
import edu.maxxie.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
private final StudentMapper studentMapper;
@Autowired
public StudentServiceImpl(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
@Override
public List<Student> getAllStudents() {
return studentMapper.getAll();
}
@Override
public void insertStudent(Student student) {
// 添加学生之前需要先创建账号
studentMapper.insertStudent(student);
}
@Override
public Student getStudentById(String id) {
Student student = studentMapper.getById(id);
return student;
}
@Override
public void updateStudent(Student student) {
student.setUpdateBy(1);
student.setUpdateTime(LocalDateTime.now());
studentMapper.update(student);
}
}

View File

@ -0,0 +1,36 @@
spring.application.name=SimpleStudentManagementSystem
#热部署生效
spring.devtools.restart.enabled=true
#设置重启目录
spring.devtools.restart.additional-paths=src/main/java
# 指定时区 避免因时区差异而导致的日期时间解析错误
spring.jackson.time-zone=Asia/Shanghai
#设置 cLasspath 目录下的WEB-INF文件夹内容修改不重启(有静态资源才需要)(需要修改成对应值)
spring.devtools.restart.exclude=static/**
# 数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库链接(需要修改成对应值)
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
# 数据库账号(需要修改成对应值)
spring.datasource.username=root
# 数据库密码(需要修改成对应值)
spring.datasource.password=123456
# 配置 mybatis 的日志,指定输出到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# 开启 mybatis 的驼峰命名自动映射开关
mybatis.configuration.map-underscore-to-camel-case=true
# 配置 mybatis 的映射文件路径
mybatis.mapper-locations=classpath*:mapper/*.xml
# 自动为包下所有Java类注册类型别名需要修改成对应值
mybatis.type-aliases-package=edu.maxxie.pojo
# 启用事务管理
spring.transaction.annotation-proxy-target-class=true
# Thymeleaf 配置
server.port=8080
server.servlet.context-path=/
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.mvc.hiddenmethod.filter.enabled=true

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="edu.maxxie.mapper.AccountMapper">
<select id="getAccount" resultType="Account">
select id, account, password, role
from account
where account = #{account}
</select>
<delete id="deleteByAccount">
delete from account where account = #{account}
</delete>
<insert id="insertAccount" keyProperty="id" useGeneratedKeys="true">
insert into account(id, account, password, role, create_by)
values(#{id}, #{account}, #{password}, #{role}, #{createBy})
</insert>
<select id="getAccountWithDetails" resultMap="AccountWithDetails">
select a.id, a.account, s.`name`, s.gender, s.birthday
from account as a, student as s
where a.account = s.id
</select>
<resultMap id="AccountWithDetails" type="Account">
<id column="id" property="id" />
<result column="account" property="account" />
<association property="studentDetails" javaType="Student">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="gender" property="gender" />
<result column="birthday" property="birthday" />
</association>
</resultMap>
</mapper>

View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="edu.maxxie.mapper.ScoreMapper">
<!-- 插入成绩 -->
<insert id="insert" keyProperty="id" useGeneratedKeys="true">
insert into score(id, student_id, course_id, score, create_by, create_time)
values(#{id}, #{studentId}, #{courseId}, #{score}, #{createBy}, #{createTime})
</insert>
<update id="update">
update grade
<set>
<if test="grade != null">grade = #{grade},</if>
<if test="updateTime != null"> update_time = #{updateTime}</if>
</set>
where no = #{no}
</update>
<!-- 删除一条成绩 -->
<delete id="deleteById">
delete from score where id = #{id}
</delete>
<!-- 删除多条成绩 -->
<delete id="deleteByIds">
delete from score where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
<!-- 查询成绩带上名称 -->
<select id="getAll" resultMap="ScoreWithName">
SELECT score.id, s.`name` as student_name, c.`name` as course_name, score.score
FROM score, student as s, course as c
WHERE s.id = score.student_id and c.id = score.course_id
</select>
<resultMap id="ScoreWithName" type="Score">
<id column="id" property="id" />
<result column="score" property="score" />
<association property="student" javaType="Student">
<result column="student_name" property="name" />
</association>
<association property="course" javaType="Course">
<result column="course_name" property="name" />
</association>
</resultMap>
<!-- <select id="getByNo" resultMap="GradeWithName">-->
<!-- SELECT-->
<!-- `no`,-->
<!-- student_id,-->
<!-- course_id,-->
<!-- grade,-->
<!-- g.create_time,-->
<!-- g.update_time,-->
<!-- s.id as uid,-->
<!-- s.`name` as student_name-->
<!-- FROM grade g-->
<!-- JOIN student s ON s.id = g.student_id-->
<!-- WHERE `no` = #{no}-->
<!-- </select>-->
<!-- <resultMap id="GradeWithName" type="Score">-->
<!-- <id column="no" property="no" />-->
<!-- <result column="student_id" property="studentID" />-->
<!-- <result column="course_id" property="courseID" />-->
<!-- <result column="grade" property="grade"></result>-->
<!-- <result column="creat_time" property="createTime" />-->
<!-- <result column="update_time" property="updateTime" />-->
<!-- <association property="student" javaType="Student">-->
<!-- <id column="uid" property="id" />-->
<!-- <result column="student_name" property="name" />-->
<!-- </association>-->
<!-- </resultMap>-->
</mapper>

View File

@ -0,0 +1,121 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="edu.maxxie.mapper.StudentMapper">
<!-- 对select的封装 -->
<sql id="commonSelect">
select id, name, gender, birthday
from student
</sql>
<!-- resultType="" 单条记录所封装的类型-->
<select id="getAll" resultType="Student">
select id, name, gender, birthday from student
<!-- <include refid="commonSelect"></include>-->
</select>
<!-- 根据id查询学生 -->
<select id="getById" resultType="Student">
<include refid="commonSelect"></include>
<where>
id = #{id}
</where>
</select>
<!-- 根据条件查询学生 -->
<select id="getByCondition" resultType="Student">
<include refid="commonSelect"></include>
<where>
<if test="len != null">
char_length(student.name) = #{len}
</if>
<if test="gender != null">
and gender = #{gender};
</if>
</where>
</select>
<!-- 插入学生信息 -->
<insert id="insertStudent" parameterType="Student">
insert into student(id, name, gender, birthday, create_by, create_time)
values(#{id}, #{name}, #{gender}, #{birthday}, #{createBy}, #{createTime})
</insert>
<!-- 更新学生信息 -->
<update id="update">
update student
<set>
<if test="name != null"> name = #{name},</if>
<if test="gender != null"> gender = #{gender},</if>
<if test="birthday != null"> birthday = #{birthday},</if>
<if test="updateBy != null"> update_by = #{updateBy},</if>
<if test="updateTime != null"> update_time = #{updateTime}</if>
</set>
where id = #{id}
</update>
<!-- collection集合名称-->
<!-- item集合遍历出来的元素/项-->
<!-- separator每一次遍历使用的分隔符-->
<!-- open遍历开始前拼接的片段-->
<!-- close遍历结束后拼接的片段-->
<delete id="deleteByIds">
delete from student where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
<delete id="deleteById">
delete from student where id = #{id}
</delete>
<!-- 查询学生信息带上性别名称 -->
<select id="getAllWithGender" resultMap="StudentWithGender">
SELECT s.id, s.`name`, g.id as gid, g.name as gname, s.birthday
from student as s
JOIN `gender` as g on g.id = s.gender
</select>
<resultMap id="StudentWithGender" type="Student">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="birthday" property="birthday"/>
<association property="genderDetails" javaType="Gender">
<id column="gid" property="id" />
<result column="gname" property="name"/>
</association>
</resultMap>
<!-- 查询学生的所有成绩信息-->
<select id="getStudentScoresById" resultMap="StudentWithScores">
SELECT s.id as sid, s.`name` as sname, c.id as cid, c.`name` as cname, sc.`id` as scid, sc.score
FROM student as s, score as sc, course as c
where s.id = #{id} and s.id = sc.student_id and c.id = sc.course_id
</select>
<resultMap id="StudentWithScores" type="Student">
<id column="sid" property="id" />
<result column="sname" property="name" />
<collection property="scores" ofType="Score">
<id column="gid" property="id" />
<result column="score" property="score" />
<result column="cname" property="course.name" />
<result column="sname" property="student.name" />
<!-- <association property="student" javaType="Student">-->
<!-- <result column="sname" property="name" />-->
<!-- </association>-->
<!-- <association property="course" javaType="Course">-->
<!-- <result column="cname" property="name" />-->
<!-- </association>-->
</collection>
</resultMap>
</mapper>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="edu.maxxie.mapper.StudentScoreMapper">
<select id="getByNo" resultMap="StudentWithScores">
SELECT s.id as sid, s.`name` as sname, c.id as cid, c.`name` as cname, sc.`id` as scid, sc.score
FROM student as s, score as sc, course as c
where s.id = #{id} and s.id = sc.student_id and c.id = sc.course_id
</select>
<resultMap id="StudentWithScores" type="StudentScore">
<id column="sid" property="student.id"></id>
<result column="sname" property="student.name"/>
<collection property="scores" ofType="Score">
<id column="scid" property="id"/>
<id column="score" property="score" />
<association property="course" javaType="Course">
<id column="cid" property="id"/>
<id column="cname" property="name"/>
</association>
</collection>
</resultMap>
</mapper>

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<h1>教务系统</h1>
<h2>学生管理——添加</h2>
<a th:href="@{/students}">查询</a>
<a th:href="@{/addstudentpage}">添加</a>
<form th:action="@{/accountstudent}" th:method="post">
<p>学号: <input type="text" name="id"/></p>
<p>姓名: <input type="text" name="name" /></p>
<p>性别: <input type="text" name="gender" /></p>
<p>出生日期: <input type="date" name="birthday"/></p>
<input type="submit" />
</form>
</body>
</html>

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
<title>教务系统</title>
</head>
<body>
<h1>教务系统</h1>
<div>
<a th:href="@{/studentpage}">学生管理</a>
<a>课程管理</a>
<a>成绩管理</a>
</div>
</body>
</html>

View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1><span th:text="${title}"></span></h1>
<h1>登陆</h1>
<!-- 登陆注册 -->
<form th:action="@{/login}" method="post">
<div><label>账号: <input type="text" name="username" value="admin"/></label></div>
<div><label>密码: <input type="password" name="password" value="admin"/></label></div>
<div><input type="submit" value="登录"/></div>
</form>
</body>
</html>

View File

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<h1>教务系统</h1>
<h2>学生管理</h2>
<a th:href="@{/students}">查询</a>
<a th:href="@{/addstudentpage}">添加</a>
<table border="1">
<tr>
<td>id</td>
<td>姓名</td>
<td>性别</td>
<td>出生日期</td>
<td>删除操作</td>
<td>更新操作</td>
</tr>
<tr th:each="student:${students}">
<td th:text="${student.id}"></td>
<td th:text="${student.name}"></td>
<td th:text="${student.gender}"></td>
<td th:text="${student.birthday}"></td>
<td>
<a th:href="@{/deleteaccountstudent/{id}(id=${student.id})}">删除</a>
</td>
<td >
<a th:href="@{/updatestudentpage/{id}(id=${student.id})}">更新</a>
</td>
</tr>
</table>
</body>
</html>

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<h1>教务系统</h1>
<h2>学生管理——更新</h2>
<a th:href="@{/students}">查询</a>
<a th:href="@{/addstudentpage}">添加</a>
<form th:action="@{/updatestudent}" th:method="post">
<p>学号: <input type="text" name="id" th:value="${student.id}"/></p>
<p>姓名: <input type="text" name="name" th:value="${student.name}"/></p>
<p>性别: <input type="text" name="gender" th:value="${student.gender}"/></p>
<p>出生日期: <input type="date" name="birthday" th:value="${student.birthday}"/></p>
<input type="submit" />
</form>
</body>
</html>

View File

@ -0,0 +1,175 @@
package edu.maxxie;
import edu.maxxie.mapper.AccountMapper;
import edu.maxxie.mapper.ScoreMapper;
import edu.maxxie.mapper.StudentScoreMapper;
import edu.maxxie.mapper.StudentMapper;
import edu.maxxie.pojo.Account;
import edu.maxxie.pojo.Score;
import edu.maxxie.pojo.Student;
import edu.maxxie.pojo.StudentScore;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
@SpringBootTest
class ApplicationTests {
@Autowired
private StudentMapper studentMapper;
@Autowired
private ScoreMapper scoreMapper;
@Autowired
private StudentScoreMapper studentScoreMapper;
@Autowired
private AccountMapper accountMapper;
@Test
public void testGetAll(){
// 查询所有学生信息
List<Student> stuList = studentMapper.getAll();
System.out.println(stuList);
}
@Test
public void testGetById(){
// 根据 id 查学生
Student stu = studentMapper.getById("12138");
System.out.println(stu);
}
@Test
public void testGetByCondition(){
// 学生条件查询
List<Student> stuList = studentMapper.getByCondition(null, null);
System.out.println(stuList);
}
@Test
public void testInsertAccount(){
// 新增账号
Account account = new Account();
account.setAccount("12143");
account.setPassword("123456");
account.setCreateBy(1);
LocalDateTime now = LocalDateTime.now();
account.setCreateTime(now);
accountMapper.insertAccount(account);
}
@Test
public void testInsertStudent(){
Student student = new Student();
student.setId("12143");
student.setName("陆展博");
student.setGender(1);
student.setBirthday(LocalDate.of(2002,10,14));
student.setCreateBy(1);
LocalDateTime now = LocalDateTime.now();
student.setCreateTime(now);
studentMapper.insertStudent(student);
}
@Test
public void testInsertScore(){
Score score = new Score();
score.setStudentId("12142");
score.setCourseId("102");
score.setScore(90);
score.setCreateBy(1);
LocalDateTime now = LocalDateTime.now();
score.setCreateTime(now);
scoreMapper.insert(score);
System.out.println(score.getId());
}
@Test
public void testUpdateStudent(){
Student student = new Student();
student.setId("12141");
student.setName("张益达");
student.setUpdateBy(1);
LocalDateTime now = LocalDateTime.now();
student.setUpdateTime(now);
studentMapper.update(student);
}
@Test
public void testDeleteScoreById(){
// 删除一条成绩
scoreMapper.deleteById(28);
}
@Test
public void testDeleteByIds(){
// 删除多条成绩
List<Integer> ids = Arrays.asList(29,30,31);
scoreMapper.deleteByIds(ids);
}
@Test
public void testAccountWithDetails(){
// 一对一
List<Account> accounts = accountMapper.getAccountWithDetails();
System.out.println(accounts);
}
@Test
public void testStudentWithGender(){
List<Student> students = studentMapper.getAllWithGender();
System.out.println(students);
}
@Test
public void testScoreWithName(){
List<Score> scores = scoreMapper.getAll();
System.out.println(scores);
}
@Test
public void testStudentGrade(){
Student student = studentMapper.getStudentScoresById("12138");
System.out.println("学生:" + student.getName());
for (Score score : student.getScores()){
System.out.println("课程:" + score.getCourse().getName() + ",成绩:" + score.getScore());
System.out.println(score);
}
}
@Test
public void testStudentGrade2(){
StudentScore studentScore = studentScoreMapper.getByNo("12138");
System.out.println("学生:" + studentScore.getStudent().getName());
for (Score score : studentScore.getScores()){
System.out.println("课程:" + score.getCourse().getName() + ",成绩:" + score.getScore());
}
}
/*@Test
public void testGradeUpdate(){
Score score = new Score();
score.setStudentID("12142");
score.setCourseID("101");
score.setGrade(90);
score.setUpdateTime(LocalDateTime.now());
gradeMapper.update(score);
System.out.println(score);
}
@Test
public void testGradeDelete(){
gradeMapper.deleteByNo(24);
}
@Test
public void testGetByNo(){
Score score = gradeMapper.getByNo(1);
System.out.println(score);
}
*/
}