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的全局配置中配置了延迟加载

  • 相关阅读:
    繁星CSS3之旅-CSS基本样式-CSS文本字体
    繁星CSS3之旅-CSS基本样式-CSS文本
    繁星CSS3之旅-CSS基本样式-CSS背景
    繁星CSS3之旅-CSS入门基础知识
    繁星H5之旅-前端学习入门
    繁星H5之旅-HTML5表单新增元素与属性
    字符串分割split()
    返回指定的字符串首次出现的位置
    python中非关键字可变长参数和关键字变量参数的区别
    喝了一碗毒鸡汤
  • 原文地址:https://www.cnblogs.com/lyh233/p/12350077.html
Copyright © 2011-2022 走看看