MyBatis的一对多映射(九)下

简介: MyBatis的一对多映射(九)

三. 部门到员工的一对多关联映射


一对多关联映射,也有两种方式, 一种是一对多的嵌套结果, 一种是一对多的嵌套Select查询, 相比较一对一来说, 一对一查询出来的结果最多只有一个值,而一对多却可以查询出一个集合。 另外,一对一 用的是 javaType, 而一对多用的是 ofType. 这一点非常重要。


三.一 一对多的嵌套结果方式


DeptMapper.java 中接口:


public Dept getAllInfoByIdWithResult(int id);


DeptMapper.xml 中的sql语句:


<resultMap type="dept" id="deptCollectionResultMapWithResult">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="description" column="description"/>
    <!-- 用的是ofType的类型。 -->
    <collection property="allUser" ofType="user">
      <id property="id" column="id"/>
      <result property="name" column="name"/>
      <result property="sex" column="sex"/>
      <result property="age" column="age"/>
      <result property="description" column="description"/>
    </collection>
  </resultMap>
<select id="getAllInfoByIdWithResult" parameterType="int" resultMap="deptCollectionResultMapWithResult">
    select * from dept d,user u where d.id=u.deptId and d.id=#{id}
  </select>


测试方法:


@Test
  public void getAllInfoByIdWithResultTest(){
    SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
    DeptMapper deptMapper=sqlSession.getMapper(DeptMapper.class);
    Dept dept=deptMapper.getAllInfoByIdWithResult(1);
    System.out.println(dept);
    List<User> allUser=dept.getAllUser();
    allUser.forEach(n ->System.out.println(n));
  }


20190708165337732.png


三.二 一对多的嵌套Select 查询


DeptMapper.java 中的接口:


public Dept getAllInfoByIdWithSelect(int id);


UserMapper.java 中的接口:

传入参数,用 @Param 注解的形式。


public List<User> findUserByDeptId(@Param(value="deptId") int deptId);


DeptMapper.xml 中的sql语句:


<resultMap type="dept" id="deptCollectionResultMapWithSelect">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="description" column="description"/>
    <!-- 用的是ofType的类型。 -->
    <collection property="allUser" ofType="user" column="id"
    select="com.yjl.mapper.UserMapper.findUserByDeptId"></collection>
</resultMap>
<select id="getAllInfoByIdWithSelect" parameterType="int" resultMap="deptCollectionResultMapWithSelect">
    select * from dept where id=#{id}
</select>


UserMapper.xml 中的sql语句:


<resultMap type="user" id="userResultMap">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="sex" column="sex"/>
    <result property="age" column="age"/>
    <result property="description" column="description"/>
</resultMap>
<select id="findUserByDeptId" parameterType="int" resultMap="userResultMap">
    select * from user u where u.deptId=#{deptId}
</select>


测试方法:


@Test
  public void getAllInfoByIdWithSelectTest(){
    SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
    DeptMapper deptMapper=sqlSession.getMapper(DeptMapper.class);
    Dept dept=deptMapper.getAllInfoByIdWithSelect(1);
    System.out.println(dept);
    List<User> allUser=dept.getAllUser();
    allUser.forEach(n ->System.out.println(n));
  }


20190708165348821.png


谢谢!!!

相关文章
|
2月前
|
XML Oracle Java
mybatis反向生成实体类、dao层以及映射文件
mybatis反向生成实体类、dao层以及映射文件
15 1
|
5天前
|
SQL 缓存 Java
mybatis 一对多查询
mybatis 一对多查询
15 0
|
1天前
|
Java 数据库连接 mybatis
mybatis的一对多
mybatis的一对多
|
10天前
|
SQL Java 数据库连接
15:MyBatis对象关系与映射结构-Java Spring
15:MyBatis对象关系与映射结构-Java Spring
30 4
|
16天前
|
SQL Java 数据库连接
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
【Mybatis】深入学习MyBatis:概述、主要特性以及配置与映射
|
26天前
|
XML Java 数据库连接
java对象有集合mybatis如何映射
java对象有集合mybatis如何映射
19 4
|
1月前
|
存储 XML Java
mybatis使用内部类处理一对多类型数据2
mybatis使用内部类处理一对多类型数据2
13 0
|
2月前
|
XML SQL Java
mybatis的一对多,多对一,以及多对对的配置和使用
mybatis的一对多,多对一,以及多对对的配置和使用
17 2
|
2月前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
17 1
|
2月前
|
SQL Java 数据库连接
mybatis关联配置(一对多配置)
mybatis关联配置(一对多配置)
14 1