1. 创建实体类

辅导员实体类:

public class Teacher {
    private Integer id;
    private String name;
    // 省略getter和setter方法
}

班级实体类:

public class Class {
    private Integer id;
    private String name;
    private Integer teacherId;
    // 省略getter和setter方法
}

学生实体类:

public class Student {
    private Integer id;
    private String name;
    private Integer classId;
    // 省略getter和setter方法
}
  1. 创建Mapper接口

TeacherMapper接口:

public interface TeacherMapper {
    Teacher selectById(Integer id);
}

ClassMapper接口:

public interface ClassMapper {
    List<Student> selectByTeacherId(Integer teacherId);
}

StudentMapper接口:

public interface StudentMapper {
    List<Student> selectByClassId(Integer classId);
}
  1. 创建Mapper映射文件

TeacherMapper.xml:

<mapper namespace="com.example.mapper.TeacherMapper">
    <select id="selectById" resultType="com.example.entity.Teacher">
        select * from teacher where id = #{id}
    </select>
</mapper>

ClassMapper.xml:

<mapper namespace="com.example.mapper.ClassMapper">
    <select id="selectByTeacherId" resultType="com.example.entity.Student">
        select s.* from student s
        join class c on s.class_id = c.id
        where c.teacher_id = #{teacherId}
    </select>
</mapper>

StudentMapper.xml:

<mapper namespace="com.example.mapper.StudentMapper">
    <select id="selectByClassId" resultType="com.example.entity.Student">
        select * from student where class_id = #{classId}
    </select>
</mapper>
  1. 创建Service接口和实现类

TeacherService接口:

public interface TeacherService {
    Teacher getById(Integer id);
}

TeacherServiceImpl实现类:

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public Teacher getById(Integer id) {
        return teacherMapper.selectById(id);
    }
}

ClassService接口:

public interface ClassService {
    List<Student> getByTeacherId(Integer teacherId);
}

ClassServiceImpl实现类:

@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Override
    public List<Student> getByTeacherId(Integer teacherId) {
        return classMapper.selectByTeacherId(teacherId);
    }
}

StudentService接口:

public interface StudentService {
    List<Student> getByClassId(Integer classId);
}

StudentServiceImpl实现类:

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Student> getByClassId(Integer classId) {
        return studentMapper.selectByClassId(classId);
    }
}
  1. 创建Controller类
@Controller
public class StudentController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private ClassService classService;

    @Autowired
    private StudentService studentService;

    @GetMapping("/students")
    public String students(Model model, @RequestParam("teacherId") Integer teacherId) {
        Teacher teacher = teacherService.getById(teacherId);
        List<Student> students = classService.getByTeacherId(teacherId);
        model.addAttribute("teacher", teacher);
        model.addAttribute("students", students);
        return "students";
    }
}
  1. 创建视图文件

students.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生信息</title>
</head>
<body>
    <h1>学生信息</h1>
    <p>辅导员:${teacher.name}</p>
    <table>
        <thead>
            <tr>
                <th>学生编号</th>
                <th>学生姓名</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach items="${students}" var="student">
                <tr>
                    <td>${student.id}</td>
                    <td>${student.name}</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
</body>
</html>
  1. 在页面中使用a标签调用控制器方法
<a href="/students?teacherId=1">查看1班学生信息</a>

以上就是使用SSM框架实现通过点击a标签,调用控制器方法到Dao层,使得页面输出某辅导员所管理的班级的所有学生信息的示例代码


原文地址: https://www.cveoy.top/t/topic/crdl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录