接one to one
二次查询的映射文件
PersonDao.xml
<?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.hsj.dao.PersonDao">
<select id="getPersons" resultMap="resultMap_Person">
select * from t_person
</select>
<resultMap type="person" id="resultMap_Person">
<id column="p_id" property="id"/>
<result column="p_name" property="name"/>
<!--
column="p_id":二次查询时传递的值对应的字段
-->
<association property="card" column="p_id" javaType="card" select="getCardById">
</association>
</resultMap>
<select id="getCardById" parameterType="int" resultMap="resultMap_card">
select * from t_card where person_id=#{id}
</select>
<resultMap type="card" id="resultMap_card">
<id column="c_id" property="id"/>
<result column="c_cardno" property="cardNo"/>
<result column="c_address" property="address"/>
</resultMap>
<!-- 根据人的id值查询人对象 -->
<select id="getPersonById" parameterType="int" resultMap="resultMap_Person">
select * from t_person p inner join t_card c on p.p_id=c.person_id and p_id=#{id}
</select>
</mapper>
CardDao.xml
<?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.hsj.dao.CardDao">
<select id="getCards" resultMap="resultMap_Card">
select * from t_card
</select>
<resultMap type="card" id="resultMap_Card">
<id column="c_id" property="id"/>
<result column="c_cardno" property="cardNo"/>
<result column="c_address" property="address"/>
<association property="person" column="person_id" javaType="person" select="getPersonById">
</association>
</resultMap>
<select id="getPersonById" parameterType="int" resultMap="resultMap_Person">
select * from t_person where p_id=#{id}
</select>
<resultMap type="person" id="resultMap_Person">
<id column="p_id" property="id"/>
<result column="p_name" property="name"/>
</resultMap>
<!-- 根据id查询卡对象 -->
<select id="getCardById" parameterType="int" resultMap="resultMap_Card">
select * from t_card where c_id=#{id}
</select>
</mapper>
测试类接one to one