springboot项目逻辑处理

springboot项目中后端处理逻辑的核心代码是在mapper.xml文件中吗?

xml version="1.0" encoding="UTF-8"?>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


    <select id="getPage" resultType="com.rainng.studentinformationsystem.model.vo.response.table.StudentCourseItemVO">
        SELECT sc_id AS 'id', course_name AS 'courseName', student_name AS 'studentName', class_name AS 'className',
        sc_daily_score AS 'dailyScore', sc_exam_score AS 'examScore', sc_score AS 'score'
        FROM rc_student_course
        JOIN rc_course on sc_course_id = course_id
        JOIN rc_student on sc_student_id = student_id
        JOIN rc_class on student_class_id = class_id
        <where>
            <if test="className != null and className != ''">
                AND class_name LIKE CONCAT('%',#{className},'%')
            if>
            <if test="courseName != null and courseName != ''">
                AND course_name LIKE CONCAT('%',#{courseName},'%')
            if>
            <if test="studentName != null and studentName != ''">
                AND student_name LIKE CONCAT('%',#{studentName},'%')
            if>
        where>
    select>

    <select id="count" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM rc_student_course
        JOIN rc_course on sc_course_id = course_id
        JOIN rc_student on sc_student_id = student_id
        JOIN rc_class on student_class_id = class_id
        <where>
            <if test="className != null and className != ''">
                AND class_name LIKE CONCAT('%',#{className},'%')
            if>
            <if test="courseName != null and courseName != ''">
                AND course_name LIKE CONCAT('%',#{courseName},'%')
            if>
            <if test="studentName != null and studentName != ''">
                AND student_name LIKE CONCAT('%',#{studentName},'%')
            if>
        where>
    select>

    <select id="listStudentCourseSelected" resultType="com.rainng.studentinformationsystem.model.vo.response.table.StudentCourseSelectedItemVO">
        SELECT sc_id          AS 'studentCourseId',
               course_name    AS 'courseName',
               teacher_name   AS 'teacherName',
               course_credit  AS 'credit',
               sc_daily_score AS 'dailyScore',
               sc_exam_score  AS 'examScore',
               sc_score       AS 'score'
        FROM rc_student_course
                 JOIN rc_student on sc_student_id = student_id
                 JOIN rc_course on sc_course_id = course_id
                 JOIN rc_teacher on course_teacher_id = teacher_id
        WHERE student_id = #{studentId}
    select>

    <select id="listStudentExam" resultType="com.rainng.studentinformationsystem.model.vo.response.table.StudentExamItemVO">
        SELECT sc_id                AS 'studentCourseId',
               course_name          AS 'courseName',
               teacher_name         AS 'teacherName',
               course_exam_date        'examDate',
               course_exam_location AS 'examLocation'
        FROM rc_student_course
                 JOIN rc_course on sc_course_id = course_id
                 JOIN rc_student on sc_student_id = student_id
                 JOIN rc_teacher on course_teacher_id = teacher_id
        WHERE student_id = #{studentId}
          AND course_exam_date IS NOT NULL
          AND course_exam_location IS NOT NULL
          AND sc_exam_score IS NULL
    select>

    <select id="countStudentCourseSelectedByTimePart" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM rc_student_course
                 JOIN rc_student on sc_student_id = student_id
                 JOIN rc_course on sc_course_id = course_id
        WHERE student_id = #{studentId}
          AND course_time LIKE CONCAT(#{timePart}, '-%')
    select>

    <select id="listStudentTimetable" resultType="com.rainng.studentinformationsystem.model.vo.response.table.TimetableItemVO">
        SELECT course_time     AS 'time',
               course_name     AS 'courseName',
               teacher_name    AS 'teacherName',
               course_location AS 'location'
        FROM rc_student_course
                 JOIN rc_course on sc_course_id = course_id
                 JOIN rc_teacher on course_teacher_id = teacher_id
        WHERE sc_student_id = #{studentId}
    select>

    <select id="getTeacherGradePage" resultType="com.rainng.studentinformationsystem.model.vo.response.table.TeacherGradeItemVO">
        SELECT sc_id AS 'studentCourseId', course_name AS 'courseName',
        student_name AS 'studentName', sc_daily_score AS 'dailyScore',
        sc_exam_score AS 'examScore', sc_score AS 'score'
        FROM rc_student_course
        JOIN rc_course on sc_course_id = course_id
        JOIN rc_student on sc_student_id = student_id
        JOIN rc_teacher on course_teacher_id = teacher_id
        <where>
            AND teacher_id = #{teacherId}
            <if test="courseName != null and courseName != ''">
                AND course_name LIKE CONCAT('%',#{courseName},'%')
            if>
            <if test="studentName != null and studentName != ''">
                AND student_name LIKE CONCAT('%',#{studentName},'%')
            if>
        where>
    select>

    <select id="countTeacherGrade" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM rc_student_course
        JOIN rc_course on sc_course_id = course_id
        JOIN rc_student on sc_student_id = student_id
        JOIN rc_teacher on course_teacher_id = teacher_id
        <where>
            AND teacher_id = #{teacherId}
            <if test="courseName != null and courseName != ''">
                AND course_name LIKE CONCAT('%',#{courseName},'%')
            if>
            <if test="studentName != null and studentName != ''">
                AND student_name LIKE CONCAT('%',#{studentName},'%')
            if>
        where>
    select>

这里都是select语句,为何选课时会有容量超过上限的错误提示,猜测可能与listStudentCourseSelected这个id有关。设计思路是设置一个课程容量,用选一次课程容量减一。当课程容量减为0时,就会提示超过上限。貌似xml文件里只有查询语句,删除和修改以及增加的处理逻辑一般放在哪里?

你有 mapper.xml 这个文件说明你的dao层用的是 mybatis,你的xml里面只有select语句没有插入语句。
如果你不是用的mybatis-plus的话,你的代码就没有更新和插入语句。
你可以将更新和插入逻辑写在你的mapper.xml文件中。
至于你说的选课容量,应该是你的代码逻辑控制的。容量的控制加减明显也不在这个xml里面

Springboot项目的核心代码主要在src目录下,会根据功能的不同分为几个不同的层,如实体层、服务层等。xml文件一般是配置文件。

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt:

    答案:

    通常情况下,增加,删除以及修改的处理逻辑不在mapper.xml文件中,而是在对应的service类中实现的。mapper.xml主要负责SQL查询语句的编写。对于设置课程容量以及选课逻辑,可以在对应的service类中实现。具体步骤如下:

    1. 在service类中定义一个变量用于存储课程容量,例如int capacity = 30;

    2. 编写一个选课方法,用于判断课程容量是否足够,如果足够则进行选课操作,并将课程容量减一;否则提示超过上限。例如:

    @Service
    public class CourseService {
        private int capacity = 30;
    
        public boolean selectCourse(Student student, Course course) {
            if (capacity > 0) {
                // 课程容量足够,进行选课操作
                // TODO: 将选课记录插入数据库中
                capacity--;
                return true;
            } else {
                // 课程容量不足,提示超过上限
                System.out.println("超过课程容量上限!");
                return false;
            }
        }
    }
    
    1. 在Controller类中调用选课方法进行选课操作。例如:
    @RestController
    @RequestMapping("/course")
    public class CourseController {
        @Autowired
        private CourseService courseService;
    
        @PostMapping("/select")
        public boolean selectCourse(Student student, Course course) {
            return courseService.selectCourse(student, course);
        }
    }
    

    当然,如果需要将选课记录插入数据库,可以在mapper.xml文件中编写对应的insert语句,并在相应的service类中调用mapper提供的insert方法来实现。例如:

    <insert id="insertCourseSelection" parameterType="com.example.demo.entity.CourseSelection">
        insert into course_selection (student_id, course_id)
        values (#{student.id}, #{course.id})
    </insert>
    
    @Service
    public class CourseService {
        @Autowired
        private CourseSelectionMapper courseSelectionMapper;
    
        public boolean selectCourse(Student student, Course course) {
            // TODO: 判断课程容量是否足够
    
            // 将选课记录插入数据库
            CourseSelection courseSelection = new CourseSelection();
            courseSelection.setStudent(student);
            courseSelection.setCourse(course);
            courseSelectionMapper.insertCourseSelection(courseSelection);
    
            // 更新课程容量
            // TODO: 更新课程容量
    
            return true;
        }
    }
    

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^