ssm框架一对一连接查询,一对多连接查询

一对一
在这里插入图片描述使用resultMap association为关联的表,注意里面的名字

SSM整合指的是Spring、SpringMVC和MyBatis三个框架的整合。而多对多查询是指关系型数据库中多个表之间存在多对多的关系,需要通过中间表来建立关联。下面是一个基于SSM整合的多对多查询的例子: 假设我们有两个表:学生表(student)和课程表(course),它们之间存在多对多的关系,需要通过中间表(student_course)来建立关联。我们可以通过以下步骤实现多对多查询: 1. 在MyBatis的映射文件中定义两个查询语句,一个用于查询学生信息,一个用于查询课程信息: ``` <!-- 查询学生信息 --> <select id="getStudentById" parameterType="int" resultType="Student"> select * from student where id = #{id} </select> <!-- 查询课程信息 --> <select id="getCourseById" parameterType="int" resultType="Course"> select * from course where id = #{id} </select> ``` 2. 在MyBatis的映射文件中定义一个查询语句,用于查询学生所选的所有课程: ``` <!-- 查询学生所选的所有课程 --> <select id="getCoursesByStudentId" parameterType="int" resultType="Course"> select c.* from course c, student_course sc where c.id = sc.course_id and sc.student_id = #{studentId} </select> ``` 3. 在SpringMVC中定义一个Controller,用于接收前端请求,调用Service层中的方法,查询学生信息、课程信息以及学生所选的所有课程信息: ``` @Controller @RequestMapping("/student") public class StudentController { @Autowired private StudentService studentService; @Autowired private CourseService courseService; @RequestMapping("/{id}") public ModelAndView getStudentCourses(@PathVariable("id") int studentId) { // 查询学生信息 Student student = studentService.getStudentById(studentId); // 查询学生所选的所有课程信息 List<Course> courses = courseService.getCoursesByStudentId(studentId); // 查询每个课程的详细信息 for (Course course : courses) { course = courseService.getCourseById(course.getId()); } // 返回结果 ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("student", student); modelAndView.addObject("courses", courses); modelAndView.setViewName("student_courses"); return modelAndView; } } ``` 4. 在前端页面中展示查询结果。 以上就是一个基于SSM整合的多对多查询的例子。注意,这只是一种示例,具体实现方式可能因为业务需求而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值