13、mybatis一对多resultMap中用collection指定集合的封装规则

一的一方College.java:

多的一方Student.java

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>

College的mapper接口方法

测试方法

    //测试根据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();
    }

结果:

原文地址:https://www.cnblogs.com/lyh233/p/12350077.html

时间: 2024-08-29 20:19:25

13、mybatis一对多resultMap中用collection指定集合的封装规则的相关文章

resultMap之collection聚集

<select id="getCarsWithCollection" resultMap="superCarResult"> select c1.carid,c1.cartype,c2.enginetype,c2.enginecylinders from cars c1,cars c2 where c1.carid=c2.carid </select> <resultMap type="tk.mybatis.springboo

mybatis一对多关联查询——(九)

1.需求: 查询所有订单信息及订单下的订单明细信息. 订单信息与订单明细为一对多关系. 2.      sql语句 确定主查询表:订单表 确定关联查询表:订单明细表 在一对一查询基础上添加订单明细表关联即可. SELECT orders.*, USER.username, USER.sex, USER.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.o

mybatis中的resultMap 的高级映射

引用:https://www.cnblogs.com/yansum/p/5774873.html Mybatis 高级结果映射 ResultMap Association Collection 在阅读本文章时,先说几个mybatis中容易混淆的地方: 1. mybatis中的列不是数据库里的列而是查询里的列,可以是别名(如 select user_name as userName,这时column='userName' property="userName") 2.Mysql中使用自增

mybatis 一对多和多对一

在学习MyBatis3的过程中,文档上面一直在强调一个id的东西!在做这个实验的时候,也因为没有理解清楚id含义而导致一对多的"多"中也只有一条数据.id和result的唯一不同是id表示的结果将是当比较对象实例时用到的标识属性.这帮助来改进整体表现,特别是缓存和嵌入结果映射.所以不同数据的id应该唯一区别,不然导致数据结果集只有一条数据. 一.表 二.实体 1.person [java] view plain copy package com.kerwin.mybatis.pojo;

MyBatis对象关联关系---- association与collection

Mybatis处理“一对多”的关系时,需要用到associasion元素.处理”多对一“用collection元素来实现(这两个元素在之前mapper文件中提到过). 本例子中,假设一名User可以有多个Orders,用associasion来实现关联关系 首先数据库表结构 CREATE TABLE `user` ( `id` int(8) NOT NULL AUTO_INCREMENT, `username` varchar(20) COLLATE utf8_bin NOT NULL, `us

MyBatis一对多,多对一,多对多

MyBatis中的一对多和对多一,多对多 主要就是resultMap中 association – 一个复杂的类型关联;许多结果将包成这种类型(一对多) collection – 复杂类型的集合(多对一) 这2个属性的使用,而一对多和多对一都是相互的,只是站的角度不同. 实例: 这个实例只说这2个属性的使用方法,具体的配置和运行结果在SSM环境搭建文章中写出. 1.首先是数据库,沿用之前员工管理系统的数据库设计点击这里,上面是之前写过的一篇数据库设计,包含sql语句. 2.实体类: 我这里只列要

MyBatis一对多和多对多xml配置

MyBatis一对多和多对多xml配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ktcx.

mybatis使用collection查询集合属性规则

接上篇mybatis使用associaton进行分步查询 相关的类还是上篇中的类. 查询部门的时候将部门对应的所有员工信息也查询出来 DepartmentMapper.xml <!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 --> <resultMap type="com.mybatis.bean.Department" id="MyDept"> <id column="did"

mybatis文件映射之利用collection定义关联集合(五)

Employee.java public class Employee { private Integer id; private String lastName; private String gender; private String email; Department dept; } Department.java public class Department { private Integer id; private String deptName; private List<Emp