zoukankan      html  css  js  c++  java
  • mybatis-复杂查询(一对多,多对一)

    一、多对一(例如多个学生对应一个老师)

    1、学生实体

    package com.me.domain;
    
    import lombok.Data;
    
    @Data
    public class Student {
        private int id;
        private String name;
        private Teacher teacher;
    }
    View Code

    2、老师实体

    package com.me.domain;
    
    import lombok.Data;
    
    @Data
    public class Teacher {
        private int id;
        private String name;
    }
    View Code

    方法一:按查询嵌套处理

    1、 StudentMapper-getStudentList:

        <select id="getStudentList" resultMap="studentTea">
            select * from student
        </select>
        <resultMap id="studentTea" type="com.me.domain.Student">
            <result property="id" column="id"/>
            <result property="name" column="name"/>
            
            <association property="teacher" column="tid" javaType="com.me.domain.Teacher" select="getTeacher"/>
    
        </resultMap>
        <select id="getTeacher" resultType="com.me.domain.Teacher">
            select * from teacher where id = #{tid}
        </select>
    View Code

    2、测试

     @org.junit.Test
        public void getStudentList(){
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            List<Student> studentList = mapper.getStudentList();
            for (Student student : studentList) {
                System.out.println(student.toString());
            }
            sqlSession.close();
        }
    View Code

    3、结果

     方法二、案结果嵌套处理(联表查询)

    1、 StudentMapper-getStudentList2:

       <select id="getStudentList2" resultMap="studentTea2">
            select s.id sid ,s.name sname ,t.id tid ,t.name tname
            from student s,teacher t
            where s.tid = t.id
        </select>
        <resultMap id="studentTea2" type="com.me.domain.Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <association property="teacher" javaType="com.me.domain.Teacher">
                <result property="id" column="tid"/>
                <result property="name" column="tname"/>
            </association>
        </resultMap>
    View Code

    2、测试

    @org.junit.Test
        public void getStudentList2(){
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            List<Student> studentList = mapper.getStudentList2();
            for (Student student : studentList) {
                System.out.println(student.toString());
            }
            sqlSession.close();
        }
    View Code

    3、结果

     二、一对多(一个老师有多个学生)

    1、学生实体

    package com.me.domain;
    
    import lombok.Data;
    
    @Data
    public class Student2 {
        private int id;
        private String name;
        private int tid;
    }
    View Code

    2、老师实体

    package com.me.domain;
    
    import lombok.Data;
    
    import java.util.List;
    
    @Data
    public class Teacher2 {
        private int id;
        private String name;
        private List<Student2> students;
    }
    View Code

    方法一:按查询嵌套处理

    1、 TeacherMapper-getTeacher2:

        <select id="getTeacher2" resultMap="teacherStu2">
            select * from teacher where id = #{id}
        </select>
        <resultMap id="teacherStu2" type="com.me.domain.Teacher2" >
            <result property="id" column="id"/>
            <result property="name" column="name"/>
            <collection property="students" javaType="ArrayList" ofType="com.me.domain.Student2" select="getStudentByTid" column="id"/>
        </resultMap>
    
    
        <select id="getStudentByTid" resultType="com.me.domain.Student2">
            select * from student where tid = #{id}
        </select>
    View Code

    2、测试

     @org.junit.Test
        public void getTeacher2(){
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
            Teacher2 teacher = mapper.getTeacher2(1);
            System.out.println(teacher.toString());
            sqlSession.close();
        }
    View Code

    3、结果

      方法二、案结果嵌套处理(联表查询)

    1、 TeacherMapper-getTeacher:

        <select id="getTeacher" resultMap="teacherStu">
            select s.id sid ,s.name sname ,t.id tid ,t.name tname
            from student s, teacher t
            where s.tid = t.id and t.id =#{id}
        </select>
        <resultMap id="teacherStu" type="com.me.domain.Teacher2">
            <result property="id" column="tid"/>
            <result property="name" column="tname"/>
            <collection property="students" ofType="com.me.domain.Student2">
                <result property="id" column="sid"/>
                <result property="name" column="sname"/>
                <result property="tid" column="tid"/>
            </collection>
        </resultMap>
    View Code

    2、测试

    @org.junit.Test
        public void getTeacher(){
            SqlSession sqlSession =  MyBatisUtils.getSqlSession();
            TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
            Teacher2 teacher = mapper.getTeacher(1);
            System.out.println(teacher.toString());
            sqlSession.close();
        }
    View Code

    3、结果

     三、小结

    1、集合--collection【一对多】

    2、关联--association【多对一】

    3、javaType:用来指定实体类中属性的类型

    4、ofType:用来指定映射到List或集合中的实体类型,泛型中的约束类型。

  • 相关阅读:
    P2523 [HAOI2011]Problem c
    P2518 [HAOI2010]计数
    P2513 [HAOI2009]逆序对数列
    P2519 [HAOI2011]problem a
    P5020 货币系统
    P2580 于是他错误的点名开始了(Trie)
    P3805 【模板】manacher算法
    基础
    白兔的字符串(hash入门)
    ACM的分类训练题集(转载)
  • 原文地址:https://www.cnblogs.com/20183544-wangzhengshuai/p/12490894.html
Copyright © 2011-2022 走看看