1、association实现分步查询
KeyDao.xml
<!-- public Key getKeyByIdSimple(Integer id); -->
<!-- 查询key的时候也可以带上锁子信息 -->
<!--
private Integer id;//钥匙的id
private String keyName;//钥匙的名
private Lock lock;//当前钥匙能开哪个锁;
-->
<!--association实现分步查询-->
<select id="getKeyByIdSimple" resultMap="KeySimpleMap">
select * from t_key where id = #{id}
</select>
<resultMap id="KeySimpleMap" type="com.czl.bean.Key">
<id property="id" column="id"></id>
<result property="keyName" column="keyname"></result>
<!--告诉mybatis自己去调用一个查询查锁子
select="":指定一个查询sql的唯一标识;mybatis自动调用指定的sql将查出的lock封装进来
public Lock getLockByIdSimple(Integer id);需要传入锁子id
告诉mybatis把哪一列的值传递过去
column:指定将哪一列的数据传递过去
-->
<association property="lock" select="com.czl.dao.LockDao.getLockByIdSimple" column="lockid">
<id property="id" column="id"></id>
<result property="lockName" column="lockname"></result>
</association>
LockDao.xml
<select id="getLockByIdSimple" resultType="com.czl.bean.Lock">
select * from t_lock where id = #{id}
</select>
2、collection实现分步查询
LockDao.xml
<!--
private Integer id;
private String lockName;
private List<Key> keys;
id lockname
3 3号锁
-->
<select id="getLockByIdStep" resultMap="mylockstep">
select * from t_lock where id=#{id}
</select>
<resultMap id="mylockstep" type="com.czl.bean.Lock">
<id property="id" column="id"></id>
<result property="lockName" column="lockname"></result>
<collection property="keys"
select="com.czl.dao.KeyDao.getKeysByIdStep"
column="id">
</collection>
</resultMap>
KeyDao.xml
<!--按照锁子id查出所有的key-->
<select id="getKeysByIdStep" resultType="com.czl.bean.Key">
select * from t_key where lockid =#{id}
</select>