例子1、
<?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.atguigu.dao.KeyDao">
<!--public List<Key> getKeysByLockId(Integer id);
按照锁子id查出所有的key
-->
<select id="getKeysByLockId" resultType="com.atguigu.bean.Key">
select * from t_key where lockid=#{id}
</select>
<!-- public Key getKeyByIdSimple(Integer id); -->
<!-- 查询key的时候也可以带上锁子信息 -->
<!--
private Integer id;//钥匙的id
private String keyName;//钥匙的名
private Lock lock;//当前钥匙能开哪个锁;
-->
<!-- id keyname lockid -->
<select id="getKeyByIdSimple" resultMap="mykey02">
select * from t_key where id=#{id}
</select>
<resultMap type="com.atguigu.bean.Key" id="mykey02">
<id property="id" column="id"/>
<result property="keyName" column="keyname"/>
<!--告诉mybatis自己去调用一个查询查锁子
select="":指定一个查询sql的唯一标识;mybatis自动调用指定的sql将查出的lock封装进来
public Lock getLockByIdSimple(Integer id);需要传入锁子id
告诉mybatis把哪一列的值传递过去
column:指定将哪一列的数据传递过去
-->
<association property="lock"
select="com.atguigu.dao.LockDao.getLockByIdSimple"
column="lockid" fetchType="lazy"></association>
</resultMap>
<!-- getKeyById(Integer) -->
<!--
private Integer id;//钥匙的id
private String keyName;//钥匙的名
private Lock lock;//当前钥匙能开哪个锁;
id keyname lockid lid lockName
-->
<select id="getKeyById" resultMap="mykey">
select k.id,k.`keyname`,k.`lockid`,
l.`id` lid,l.`lockName` from t_key k
left join t_lock l on k.`lockid`=l.`id`
where k.`id`=#{id}
</select>
<!-- 自定义封装规则:使用级联属性封装联合查询出的结果 -->
<!-- <resultMap type="com.atguigu.bean.Key" id="mykey">
<id property="id" column="id"/>
<result property="keyName" column="keyname"/>
<result property="lock.id" column="lid"/>
<result property="lock.lockName" column="lockName"/>
</resultMap> -->
<!-- mybatis推荐的 <association property=""></association>-->
<resultMap type="com.atguigu.bean.Key" id="mykey">
<id property="id" column="id"/>
<result property="keyName" column="keyname"/>
<!-- 接下来的属性是一个对象,自定义这个对象的封装规则;使用association;表示联合了一个对象 -->
<!-- javaType:指定这个属性的类型 -->
<association property="lock" javaType="com.atguigu.bean.Lock">
<!-- 定义lock属性对应的这个Lock对象如何封装 -->
<id property="id" column="lid"/>
<result property="lockName" column="lockName"/>
</association>
</resultMap>
例子2、
<?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.atguigu.dao.LockDao">
<!-- public Lock getLockByIdByStep(Integer id);
id lockName
1 1号锁
-->
<select id="getLockByIdByStep" resultMap="mylockstep">
select * from t_lock where id=#{id}
</select>
<!-- collection分步查询 -->
<resultMap type="com.atguigu.bean.Lock" id="mylockstep">
<id property="id" column="id"/>
<result property="lockName" column="lockName"/>
<!-- collection指定集合类型的属性封装规则 -->
<collection property="keys"
select="com.atguigu.dao.KeyDao.getKeysByLockId"
column="{id=id}"></collection>
<!-- {key1=列名,key2=列名} -->
</resultMap>
<!--
update bs_book
set title=?,author=?,price=?,sales=?,stock=?,img_path=?
where id=?";
String sql = "update bs_book set";
if(employee.getTitle()){
sql+="title=?,"
}
if(){
sql + = "price=?,";
}
-->
<!--public Lock getLockByIdSimple(Integer id); -->
<select id="getLockByIdSimple" resultType="com.atguigu.bean.Lock">
select * from t_lock where id=#{id}
</select>
<!-- public Lock getLockById(Integer id); -->
<select id="getLockById" resultMap="mylock">
select l.*,k.id kid,k.`keyname`,k.`lockid` from t_lock l
left join t_key k on l.`id`=k.`lockid`
where l.id=#{id}
</select>
<!--
private Integer id;
private String lockName;
//查询锁子的时候把所有的钥匙也查出来
private List<Key> keys;
id lockName kid keyname lockid
3 303办公室的锁子 3 303钥匙1 3
3 303办公室的锁子 4 303钥匙2 3
3 303办公室的锁子 5 303钥匙3 3
-->
<resultMap type="com.atguigu.bean.Lock" id="mylock">
<id property="id" column="id"/>
<result property="lockName" column="lockName"/>
<!--
collection:定义集合元素的封装
property="":指定哪个属性是集合属性
javaType:指定对象类型;association
ofType="":指定集合里面元素的类型
-->
<collection property="keys" ofType="com.atguigu.bean.Key">
<!-- 标签体中指定集合中这个元素的封装规则 -->
<id property="id" column="kid"/>
<result property="keyName" column="keyname"/>
</collection>
</resultMap>
原文:https://www.cnblogs.com/jock766/p/14668419.html