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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM