此篇将三四中的一对一以及一对多进行整合,修改User.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="User">
<!-- 一对多、一对一 -->
<resultMap type="User" id="userResult">
<id property="id" column="user_id" />
<result property="userName" column="user_name" />
<association property="dept" column="user_dept_no"
javaType="Dept">
<id property="deptNo" column="dept_no" />
<result property="deptName" column="dept_name" />
</association>
<collection property="pets" ofType="Pet">
<id property="id" column="pet_id" />
<result property="petName" column="pet_name" />
<result property="petColor" column="pet_color" />
</collection>
</resultMap>
<select id="select" parameterType="int" resultMap="userResult">
select u.id as user_id,
u.username as user_name,
u.deptno as user_dept_no,
d.deptno as dept_no,
d.deptname as dept_name,
p.id as pet_id,
p.petname as pet_name,
p.petcolor as pet_color from
t_user u,t_dept d,t_pet p
where u.deptno = d.deptno and
u.id = p.userid and u.id = #{id}
</select>
</mapper>
在test类中进行测试。