MyBatis(八)、多表查询

  1. 写sql语句 sql测试
  2. 改造实体类,在实体类里面加入需要多表查询查出来的
  3. 如果是一对一 [把association标签封装进resultMap,传进去另一个实体][实体类多一个实例字段]
    • 还有一种实现不常用:要查询的类继承另一个类
  4. 如果是一对多[把collection标签封装进resultMap,传进入一个集合集合相当于一堆association][实体类多一个List]

一对一

<?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.itheima.dao.IAccountDao">
<!-- 建立对应关系 -->
<resultMap type="account" id="accountMap">
<id column="aid" property="id"/>
<result column="uid" property="uid"/>
<result column="money" property="money"/>
<!-- 它是用于指定从表方的引用实体属性的 -->
<association property="user" javaType="user">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="birthday" property="birthday"/>
<result column="address" property="address"/>
</association>
</resultMap>
<select id="findAll" resultMap="accountMap">
select u.*,a.id as aid,a.uid,a.money from account a,user u where a.uid =u.id;
</select>
</mapper>

一对多

<?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.itheima.dao.IUserDao">
<resultMap type="user" id="userMap">
<id column="id" property="id"></id>
<result column="username" property="username"/>
<result column="address" property="address"/>
<result column="sex" property="sex"/>
<result column="birthday" property="birthday"/>
<!-- collection 是用于建立一对多中集合属性的对应关系
ofType 用于指定集合元素的数据类型
-->
<collection property="accounts" ofType="account">
<id column="aid" property="id"/>
<result column="uid" property="uid"/>
<result column="money" property="money"/>
</collection>
</resultMap>
<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">
select u.*,a.id as aid ,a.uid,a.money from user u left outer join account 
a on u.id =a.uid
</select>
</mapper>


collection
部分定义了用户关联的账户信息。表示关联查询结果集
property="accList":
关联查询的结果集存储在 User 对象的上哪个属性。
ofType="account":
指定关联查询的结果集中的对象类型即List中的对象类型。此处可以使用别名,也可以使用全限定名。

多对多[一对多基础上加上关系表]

<?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.itheima.dao.IRoleDao">
 <!--定义 role 表的 ResultMap- ->
 <resultMap id="roleMap" type="role">
 <id property="roleId" column="rid"></id>
 <result property="roleName" column="role_name"></result>
 <result property="roleDesc" column="role_desc"></result>
 <collection property="users" ofType="user">
 <id column="id" property="id"></id>
 <result column="username" property="username"></result>
 <result column="address" property="address"></result>
 <result column="sex" property="sex"></result>
 <result column="birthday" property="birthday"></result>
 </collection>
 </resultMap>
 <!--查询所有-->
 <select id="findAll" resultMap="roleMap">
 select u.*,r.id as rid,r.role_name,r.role_desc from role r
  left outer join user_role ur on r.id = ur.rid
 left outer join user u on u.id = ur.uid
 </select>
</mapper>
posted @ 2020-03-19 01:47  Biturd  阅读(10)  评论(0编辑  收藏  举报