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

 

posted @   Arbitrary233  阅读(3399)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示