202101080119

This commit is contained in:
鸭鸭鸭鸭 2024-11-21 15:03:18 +08:00
commit f499ab2a8a
49 changed files with 1586 additions and 0 deletions

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,93 @@
<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.tq;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication()
@MapperScan("edu.tq.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,30 @@
package edu.tq.controller;
import edu.tq.pojo.Student;
import edu.tq.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,36 @@
package edu.tq.controller;
import edu.tq.pojo.Course;
import edu.tq.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/courses")
public class CourseController {
@Autowired
private CourseService courseService;
@GetMapping
public List<Course> getAllCourses() {
return courseService.getAllCourses();
}
@PostMapping
public void addCourse(@RequestBody Course course) {
courseService.addCourse(course);
}
@PutMapping
public void updateCourse(@RequestBody Course course) {
courseService.updateCourse(course);
}
@DeleteMapping("/{courseId}")
public void deleteCourse(@PathVariable String courseId) {
courseService.deleteCourse(courseId);
}
}

View File

@ -0,0 +1,35 @@
package edu.tq.controller;
import edu.tq.pojo.Account;
import edu.tq.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,52 @@
package edu.tq.controller;
import edu.tq.pojo.Account;
import edu.tq.pojo.Student;
import edu.tq.service.AccountService;
import edu.tq.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(){
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.tq.mapper;
import edu.tq.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,13 @@
package edu.tq.mapper;
import java.util.List;
import edu.tq.pojo.Course;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface CourseMapper {
List<Course> getAllCourses();
void insertCourse(Course course);
void updateCourse(Course course);
void deleteCourseById(String courseId);
}

View File

@ -0,0 +1,30 @@
package edu.tq.mapper;
import edu.tq.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();
List<Score> getAllScoresByStudentId(String studentId);
void insertScore(Score score);
void updateScore(Score score);
void deleteScoreById(Integer scoreId);
// 根据课程ID获取所有相关的成绩ID
List<Score> getScoresWithNamesByStudentId(String number);
}

View File

@ -0,0 +1,33 @@
package edu.tq.mapper;
import edu.tq.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.tq.mapper;
import edu.tq.pojo.StudentScore;
public interface StudentScoreMapper {
StudentScore getByNo(String id);
}

View File

@ -0,0 +1,13 @@
package edu.tq.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.tq.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.tq.pojo;
import lombok.Data;
@Data
public class Course extends BaseEntity{
private Integer id;
private String name;
}

View File

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

View File

@ -0,0 +1,20 @@
package edu.tq.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.tq.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 Integer 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.tq.pojo;
import lombok.Data;
import java.util.List;
@Data
public class StudentScore {
private Student student;
private List<Score> scores;
}

View File

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

View File

@ -0,0 +1,12 @@
package edu.tq.service;
import edu.tq.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,14 @@
package edu.tq.service;
import edu.tq.pojo.Course;
import java.util.List;
public interface CourseService {
List<Course> getAllCourses();
void addCourse(Course course);
void updateCourse(Course course);
void deleteCourseById(String courseId);
void deleteCourse(String courseId);
}

View File

@ -0,0 +1,12 @@
package edu.tq.service;
import edu.tq.pojo.Score;
import java.util.List;
public interface ScoreService {
List<Score> getAllScoresByStudentId(String studentId);
void addScore(Score score);
void updateScore(Score score);
void deleteScoreById(Integer scoreId);
}

View File

@ -0,0 +1,12 @@
package edu.tq.service;
import edu.tq.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,32 @@
package edu.tq.service.impl;
import edu.tq.mapper.AccountMapper;
import edu.tq.pojo.Account;
import edu.tq.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,53 @@
package edu.tq.service.impl;
import edu.tq.mapper.AccountMapper;
import edu.tq.mapper.StudentMapper;
import edu.tq.pojo.Account;
import edu.tq.pojo.Student;
import edu.tq.service.AccountService;
import edu.tq.service.AccountStudentService;
import edu.tq.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(String.valueOf(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,41 @@
package edu.tq.service.impl;
import edu.tq.mapper.CourseMapper;
import edu.tq.pojo.Course;
import edu.tq.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CourseServiceImpl implements CourseService {
@Autowired
private CourseMapper courseMapper;
@Override
public List<Course> getAllCourses() {
return courseMapper.getAllCourses();
}
@Override
public void addCourse(Course course) {
courseMapper.insertCourse(course);
}
@Override
public void updateCourse(Course course) {
courseMapper.updateCourse(course);
}
@Override
public void deleteCourseById(String courseId) {
courseMapper.deleteCourseById(courseId);
}
@Override
public void deleteCourse(String courseId) {
}
}

View File

@ -0,0 +1,39 @@
package edu.tq.service.impl;
import edu.tq.mapper.ScoreMapper;
import edu.tq.pojo.Score;
import edu.tq.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class ScoreServiceImpl implements ScoreService {
@Autowired
private ScoreMapper scoreMapper;
@Override
public List<Score> getAllScoresByStudentId(String studentId) {
return scoreMapper.getAllScoresByStudentId(studentId);
}
@Override
public void addScore(Score score) {
score.setCreateTime(LocalDateTime.now());
scoreMapper.insertScore(score);
}
@Override
public void updateScore(Score score) {
scoreMapper.updateScore(score);
}
@Override
public void deleteScoreById(Integer scoreId) {
scoreMapper.deleteScoreById(scoreId);
}
}

View File

@ -0,0 +1,45 @@
package edu.tq.service.impl;
import edu.tq.mapper.AccountMapper;
import edu.tq.mapper.StudentMapper;
import edu.tq.pojo.Account;
import edu.tq.pojo.Student;
import edu.tq.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,31 @@
package edu.tq.typehandler;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class GenderTypeHandler extends BaseTypeHandler<Integer> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Integer parameter, JdbcType jdbcType) throws SQLException {
ps.setInt(i, parameter);
}
@Override
public Integer getNullableResult(ResultSet rs, String columnName) throws SQLException {
int genderCode = rs.getInt(columnName);
return genderCode;
}
@Override
public Integer getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
int genderCode = rs.getInt(columnIndex);
return genderCode;
}
@Override
public Integer getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
int genderCode = cs.getInt(columnIndex);
return genderCode;
}
}

View File

@ -0,0 +1,28 @@
spring.application.name=SimpleStudentManagementSystem
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java
spring.jackson.time-zone=Asia/Shanghai
spring.devtools.restart.exclude=static/**
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://106.53.194.250:63306/mb202101080119
spring.datasource.username=mb202101080119
spring.datasource.password=UMFQEC304899
#spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
#spring.datasource.username=root
#spring.datasource.password=123456
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath*:mapper/*.xml
mybatis.type-aliases-package=edu.tq.pojo
spring.transaction.annotation-proxy-target-class=true
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,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="edu.tq.mapper.AccountMapper">
<!-- 定义Account类的resultMap -->
<resultMap id="accountResultMap" type="edu.tq.pojo.Account">
<id column="id" property="id" />
<result column="account" property="account" />
<!-- 假设Account类有password字段 -->
<result column="password" property="password" />
<!-- 如果Account类有其他字段继续添加result元素 -->
</resultMap>
<!-- 查询账号 -->
<select id="getAccount" resultType="Account">
select id, account, password, role
from account
where account = #{account}
</select>
<!-- 添加账号 -->
<!-- 添加账号 -->
<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="accountResultMap">
SELECT a.id, a.account, s.id as student_id, s.name, s.gender, s.birthday
FROM account a
INNER JOIN student s ON a.account = s.id
<!-- 确保这里的关联关系与数据库中的关系匹配 -->
</select>
<!-- 删除账号 -->
<delete id="deleteByAccount">
DELETE FROM account WHERE account = #{account}
</delete>
<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,23 @@
<?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">
<!-- CourseMapper.xml -->
<mapper namespace="edu.tq.mapper.CourseMapper">
<select id="getAllCourses" resultType="Course">
SELECT id, name FROM course
</select>
<insert id="insertCourse">
INSERT INTO course (id, name) VALUES (#{id}, #{name})
</insert>
<update id="updateCourse">
UPDATE course SET name = #{name} WHERE id = #{id}
</update>
<delete id="deleteCourseById">
DELETE FROM course WHERE id = #{courseId}
</delete>
</mapper>

View File

@ -0,0 +1,88 @@
<?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.tq.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="getAllScoresByStudentId" resultType="Score">
SELECT s.id, s.student_id, s.course_id, s.score, s.create_time, st.name AS student_name, c.name AS course_name
FROM score s
JOIN student st ON s.student_id = st.id
JOIN course c ON s.course_id = c.id
WHERE s.student_id = #{studentId}
</select>
<select id="getScoresWithNamesByStudentId" resultType="edu.tq.pojo.Score">
SELECT
s.id AS score_id,
s.student_id,
s.course_id,
s.score,
st.name AS student_name,
c.name AS course_name
FROM
score s
JOIN
student st ON s.student_id = st.id
JOIN
course c ON s.course_id = c.id
WHERE
s.student_id = #{number}
</select>
<insert id="insertScore">
INSERT INTO score (student_id, course_id, score, create_time) VALUES (#{studentId}, #{courseId}, #{score}, #{createTime})
</insert>
<update id="updateScore">
UPDATE score SET score = #{score} WHERE id = #{id}
</update>
<delete id="deleteScoreById">
DELETE FROM score WHERE id = #{scoreId}
</delete>
</mapper>

View File

@ -0,0 +1,129 @@
<?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.tq.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>
<resultMap id="studentResultMap" type="Student">
<!-- ... 其他映射 ... -->
<result column="gender" property="gender" jdbcType="INTEGER" javaType="Integer"/>
<result column="gender" property="genderDescription" jdbcType="VARCHAR" javaType="String"
typeHandler="edu.tq.typehandler.GenderTypeHandler"/>
</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.tq.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,13 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>增加课程</title>
</head>
<body>
<h1>增加课程</h1>
<form action="/addcourse" method="post">
<p>课程名: <input type="text" name="name" required></p>
<input type="submit" value="提交">
</form>
</body>
</html>

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>增加成绩</title>
</head>
<body>
<h1>增加成绩</h1>
<form action="/addscore" method="post">
<p>学生编号: <input type="text" name="studentId" required></p>
<p>课程编号: <input type="text" name="courseId" required></p>
<p>成绩: <input type="text" name="grade" required></p>
<input type="submit" value="提交">
</form>
</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="@{/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,25 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>课程管理</title>
</head>
<body>
<h1>课程列表</h1>
<a href="/addcoursepage">增加课程</a>
<table>
<tr>
<th>课程编号</th>
<th>课程名</th>
<th>操作</th>
</tr>
<tr th:each="course : ${courses}">
<td th:text="${course.id}"></td>
<td th:text="${course.name}"></td>
<td>
<a th:href="@{/updatecoursepage/{id}(id=${course.id})}">更新</a>
<a th:href="@{/deletecourse/{id}(id=${course.id})}">删除</a>
</td>
</tr>
</table>
</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,28 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>成绩管理</title>
</head>
<body>
<h1>学生成绩列表</h1>
<a href="/addscorepage">增加成绩</a>
<table>
<tr>
<th>学生姓名</th>
<th>课程名</th>
<th>成绩</th>
<th>操作</th>
</tr>
<tr th:each="score : ${scores}">
<td th:text="${score.studentName}"></td>
<td th:text="${score.courseName}"></td>
<td th:text="${score.grade}"></td>
<td>
<a th:href="@{/updatescorepage/{id}(id=${score.id})}">更新</a>
<a th:href="@{/deletescore/{id}(id=${score.id})}" onclick="return confirm('确定要删除这条成绩吗?')">删除</a>
</td>
</tr>
</table>
</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,14 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>更新课程</title>
</head>
<body>
<h1>更新课程</h1>
<form action="/updatecourse" method="post">
<input type="hidden" name="id" th:value="${course.id}">
<p>课程名: <input type="text" name="name" th:value="${course.name}" required></p>
<input type="submit" value="提交">
</form>
</body>
</html>

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>更新成绩</title>
</head>
<body>
<h1>更新成绩</h1>
<form action="/updatescore" method="post">
<input type="hidden" name="id" th:value="${score.id}">
<p>学生姓名: <input type="text" name="studentName" th:value="${score.studentName}" readonly></p>
<p>课程名: <input type="text" name="courseName" th:value="${score.courseName}" readonly></p>
<p>成绩: <input type="text" name="grade" th:value="${score.grade}" required></p>
<input type="submit" value="提交">
</form>
</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,235 @@
package edu.tq;
import edu.tq.mapper.*;
import edu.tq.pojo.*;
import edu.tq.service.AccountService;
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;
@Autowired
private AccountService accountService;
@Autowired
private CourseMapper courseMapper;// 正确注入 AccountService
@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("1214169");
account.setPassword("123456");
account.setCreateBy(1);
LocalDateTime now = LocalDateTime.now();
account.setCreateTime(now);
accountService.insertAccount(account); // 使用注入的 accountService 调用方法
}
@Test
public void testInsertStudent(){
Student student = new Student();
student.setId(12111);
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());
}
}
// 1. 查询所有课程名
@Test
public void testGetAllCourses() {
List<Course> courses = courseMapper.getAllCourses();
courses.forEach(course -> System.out.println(course.getName()));
}
// 2. 增加某个课程
@Test
public void testInsertCourse() {
Course course = new Course();
course.setId(107);
course.setName("医学信息工程");
course.setCreateBy(1);
course.setCreateTime(LocalDateTime.now());
courseMapper.insertCourse(course);
}
// 3. 更新某个课程
@Test
public void testUpdateCourse() {
Course course = new Course();
course.setId(103);
course.setName("线性代数");
course.setUpdateBy(1);
course.setUpdateTime(LocalDateTime.now());
courseMapper.updateCourse(course);
}
// 4. 删除某个课程
@Test
public void testDeleteCourse() {
courseMapper.deleteCourseById("107");
}
//5.查询某个人所有成绩
@Test
public void testGetScoresWithNamesByStudentId() {
List<Score> scores = scoreMapper.getScoresWithNamesByStudentId("12138");
for (Score score : scores) {
if (score.getStudent() != null) {
System.out.println("学生姓名:" + score.getStudent().getName() + ",课程名:" + score.getCourse().getName() + ",成绩:" + score.getScore());
} else {
System.out.println("学生记录为空");
}
}
}
// 6. 增加某个人某条成绩
@Test
public void testAddScoreForStudent() {
Score score = new Score();
score.setStudentId("12138");
score.setCourseId("103");
score.setScore(95);
score.setCreateTime(LocalDateTime.now());
score.setCreateBy(1);
scoreMapper.insertScore(score);
}
// 7. 更新某个人某条成绩
@Test
public void testUpdateScoreForStudent() {
Score score = new Score();
score.setId(1);
score.setScore(98);
score.setUpdateTime(LocalDateTime.now());
score.setUpdateBy(1);
scoreMapper.updateScore(score);
}
// 8. 删除某个人某条成绩
@Test
public void testDeleteScoreForStudent() {
scoreMapper.deleteScoreById(1);
}
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>