MyBatis关联映射
将多个表记录提取,封装成具有关联关系的对象。
关系类型:分为单个对象关联和多个对象关联
cn_notebook-->Book对象
cn_user+cn_notebook-->具有对象关联关系的对象
Book-->User
一个Book对应一个User
User-->Book
一个User对应多个Book
public class User{
//user属性
//追加关联属性
private List<Book> books;
}
select * from cn_user u join cn_notebook b
on(u.cn_user_id=b.cn_user_id) where u.cn_user_id=?
select * from cn_user where cn_user_id=? User
select * from cn_notebook where cn_user_id=? List<Book>
result.setData();
关联数据加载过程:
-根据主对象User一起加载books信息
-先加载User然后再发送SQL加载books
n+1条
user-->发送sql加载books
user-->发送sql加载books
user-->发送sql加载books
user-->发送sql加载books
user-->发送sql加载books
...-->发送sql加载books
代码示例:
Dao接口
public interface AssociationDao {
public List<Book> findAllBooksAndUser();
public Book findBookAndUser(String bookId);
public User findUserAndBooks1(String userId);
public User findUserAndBooks(String userId);
}
Mapper配置文件<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="org.tedu.cloudnote.dao.AssociationDao">
<!-- 通过关联查询一次取出user和books值 -->
<select id="findUserAndBooks"
parameterType="string" resultMap="userMap">
select *
from cn_user u
join cn_notebook b
on(u.cn_user_id=b.cn_user_id)
where u.cn_user_id=#{userId}
</select>
<resultMap id="userMap"
type="org.tedu.cloudnote.entity.User">
<id property="cn_user_id" column="cn_user_id"/>
<result property="cn_user_name" column="cn_user_name"/>
<result property="cn_user_password" column="cn_user_password"/>
<result property="cn_user_token" column="cn_user_token"/>
<result property="cn_user_nick" column="cn_user_nick"/>
<!-- 指定books属性装载数据 -->
<collection property="books"
javaType="java.util.List"
ofType="org.tedu.cloudnote.entity.Book">
<id property="cn_notebook_id" column="cn_notebook_id"/>
<result property="cn_user_id" column="cn_user_id"/>
<result property="cn_notebook_type_id" column="cn_notebook_type_id"/>
<result property="cn_notebook_name" column="cn_notebook_name"/>
<result property="cn_notebook_desc" column="cn_notebook_desc"/>
<result property="cn_notebook_createtime" column="cn_notebook_createtime"/>
</collection>
</resultMap>
<!-- 通过发送另一个SQL加载books属性值 -->
<select id="findUserAndBooks1"
parameterType="string" resultMap="userMap1">
select * from cn_user
where cn_user_id=#{userId}
</select>
<resultMap id="userMap1"
type="org.tedu.cloudnote.entity.User">
<id property="cn_user_id" column="cn_user_id"/>
<result property="cn_user_name" column="cn_user_name"/>
<!-- 略 -->
<collection property="books"
javaType="java.util.List"
ofType="org.tedu.cloudnote.entity.Book"
select="findUserBooks"
column="cn_user_id">
</collection>
</resultMap>
<select id="findUserBooks" parameterType="string"
resultType="org.tedu.cloudnote.entity.Book">
select * from cn_notebook
where cn_user_id=#{userId}
</select>
<!-- 通过一个sql取出book和相关的user信息 -->
<select id="findBookAndUser"
parameterType="string" resultMap="bookMap">
select *
from cn_notebook b
join cn_user u
on(b.cn_user_id=u.cn_user_id)
where b.cn_notebook_id=#{bookId}
</select>
<resultMap id="bookMap"
type="org.tedu.cloudnote.entity.Book">
<id property="cn_notebook_id" column="cn_notebook_id"/>
<result property="cn_user_id" column="cn_user_id"/>
<result property="cn_notebook_type_id" column="cn_notebook_type_id"/>
<result property="cn_notebook_name" column="cn_notebook_name"/>
<result property="cn_notebook_desc" column="cn_notebook_desc"/>
<result property="cn_notebook_createtime" column="cn_notebook_createtime"/>
<!-- 加载user关联属性值 -->
<association property="user"
javaType="org.tedu.cloudnote.entity.User">
<id property="cn_user_id" column="cn_user_id"/>
<result property="cn_user_name" column="cn_user_name"/>
<result property="cn_user_password" column="cn_user_password"/>
<result property="cn_user_token" column="cn_user_token"/>
<result property="cn_user_nick" column="cn_user_nick"/>
</association>
</resultMap>
<!-- 提取所有笔记信息 -->
<select id="findAllBooksAndUser"
resultMap="bookMap">
select *
from cn_notebook b
join cn_user u
on(b.cn_user_id=u.cn_user_id)
</select>
</mapper>