zoukankan      html  css  js  c++  java
  • 13、mybatis一对多resultMap中用collection指定集合的封装规则

    一、sql语句中使用左连接查询方式

    一的一方College.java:

     多的一方Student.java

    College的mapper接口方法

    College的sqlmapper文件配置

        <resultMap type="com.pxxy.bean.College" id="collegeMap">
            <id column="id" property="id"/>
            <result column="collegeName" property="collegeName"/>
            
            <!-- collection定义关联集合类型的属性的封装规则 
                    property:指定属性中集合的名称
                    oftype:指定集合里面元素的类型;可以写别名-->
            <collection property="students" ofType="com.pxxy.bean.Student">
                <id column="s_id" property="id"/>
                <result column="name" property="name"/>
            </collection>
        </resultMap>
        
        <!-- 左连接查询 -->
        <select id="getColByIdPlus" resultMap="collegeMap">
            select c.id id,c.collegeName collegeName,s.id s_id,s.name name
            from college c
            left join student s
            on c.id = s.c_id
            where c.id=#{id}
        </select>

     测试方法

        //测试根据id查询学校以及学校的学生
        @Test
        public void testGetColByIdPlus() throws IOException {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            CollegeMapper collegeMapper = sqlSession.getMapper(CollegeMapper.class);
            College college =  collegeMapper.getColByIdPlus(1);
            System.out.println(college);
            for(Student stu:college.getStudents()) {
                System.out.println(stu);
            }
            sqlSession.close();
        }

    结果:

    二、分布查询方式

    一的一方College.java:

     多的一方Student.java

    StudentMapper接口定义根据学校id(c_id)查询所在该学校的学生方法

     StudentMapper的sql配置文件进行配置与接口方法相对应

         <select id="getStusByColId" resultType="student">
             <!-- 列名和属性名相同不用起别名 -->
             select * from student where c_id=#{colId}
         </select>

    CollegeMapper接口中定义分布查询学校及学校里的所有学生的方法

     CollegeMapper的sql配置文件进行配置与接口方法相对应

        <resultMap type="com.pxxy.bean.College" id="collegeStepMap">
            <id column="id" property="id"/>
            <result column="collegeName" property="collegeName"/>
            <!-- property="":指定哪个属性是联合的对象
                 select:表面当前属性是调用select指定的方法查出的结果
                 column:指定将哪一列的值传给这个方法作为参数
                 流程:使用select指定的方法(传入column指定的值作为参数)查出对象,并封装给property指定的属性 -->
            <collection property="students"
                select="com.pxxy.bean.StudentMapper.getStusByColId"
                column="id">
         </
    collection> </resultMap> <!-- 分布查询学校及学校里的学生 --> <select id="getColByIdPlusStep" resultMap="collegeStepMap"> select * from college where id = #{id} </select>

    测试方法

        //测试根据学校id分布查询学校以及学校的学生
        @Test
        public void testGetColByIdPlusStep() throws IOException {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            CollegeMapper collegeMapper = sqlSession.getMapper(CollegeMapper.class);
            College college =  collegeMapper.getColByIdPlusStep(1);
            System.out.println(college.getCollegeName());
            for(Student stu:college.getStudents()) {
                System.out.println(stu);
            }
            sqlSession.close();
        }

    测试结果

    可以看出这里也使用了延迟加载,因为在mybatis的全局配置中配置了延迟加载

  • 相关阅读:
    76、HTTP如何禁用缓存?如何确认缓存?
    多态与多态性,鸭子类型,内置方法,反射,异常处理
    继承的应用,继承实现原理,super(),多继承的代码规范,组合
    类的装饰器,绑定方法与非绑定方法,继承,继承背景下的属性查找
    面向对象,类与对象,__nit__方法,属性查找,隐藏属性
    xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块
    模块:re,json,pickle,time , datetime,random,猴子补丁
    区分py文件的两种用途,包的介绍和使用,日志模块
    模块的概念、使用,搜索路径与优先级,软件开发的目录规范
    三元表达式,列表、字典、集合生成式,生成器表达式,函数递归,匿名函数,面向过程编程
  • 原文地址:https://www.cnblogs.com/lyh233/p/12350077.html
Copyright © 2011-2022 走看看