ssm一对多步骤

本文介绍了如何在SSM(Spring、SpringMVC、MyBatis)框架下实现一对多的数据查询,包括实体类的设计如BigType, SmallType, Product,mapper.xml文件的配置,以及DAO接口的定义。通过这些步骤,可以在控制器中顺利获取并展示关联数据。" 136346023,7925799,OpenWrt系统升级中内存占用问题及解决,"['Linux', 'OpenWrt', '系统升级', '内存优化']
摘要由CSDN通过智能技术生成

1.使用一对多,取到两表之前或多表之间的值
(1)第一步实体类(三表关系)
    (a)BigType.java          

             //商品分类--大
            public class BigType {
                private int id;//编号
                private String name;//类型名称
                private String remarks;//别名
                //一对多--one端
                private Set<SmallType> smallTypes = new HashSet<SmallType>();
                private Set<Product> products = new HashSet<Product>();
                //封装
            }


    (b)SmallType.java      

         //商品分类--小
        public class SmallType {
            private int id;//编号
            private String name;//类型名称
            private String remarks;//别名
            private int bigTypeId;//商品类型ID-大
            private BigType bigType;//many端
            private Set<Product> products = new HashSet<Product>();            
            //封装
        }


    (c)Product.java
          

 public class Product {
                private int id;//编号
                privat
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值