项目源码:https://github.com/AbitGo/mybatis_csdn/tree/master/workspace_mybatis_0x03
首先我们将association改成了collection,后将property配置为reloList。
<resultMap id="userRoleListMap" extends="userMap" type="com.mybatis.chapter3.SysUser">
<id property="id" column="id"/>
<result property="userName" column = "user_name"/>
<result property="userPassword" column = "user_password"/>
<result property="userEmail" column = "user_email"/>
<result property="userInfo" column = "user_info"/>
<result property="headImg" column = "head_img" jdbcType="BLOB"/>
<result property="create_time" column = "create_time" jdbcType="TIMESTAMP"/>
<collection property="roleList" columnPrefix="role_"
ofType="com.mybatis.chapter3.SysRole">
<id property="id" column="id"/>
<result property="role_Name" column = "role_Name"/>
<result property="enabled" column = "enabled"/>
<result property="create_by" column = "create_by"/>
<result property="create_time" column = "create_time" jdbcType="TIMESTAMP"/>
</collection>
</resultMap>
我们逐步对resultMap进行简化,我们可以使用resultMap进行快速简化。
<resultMap id="userRoleListMap" extends="userMap" type="com.mybatis.chapter3.SysUser">
<id property="id" column="id"/>
<collection property="roleList"
columnPrefix="role_"
resultMap="com.mybatis.chapter3.UserMapper.selectRolebyId">
</collection>
</resultMap>
userMap以及roleMap
<resultMap id="userMap" type="com.mybatis.chapter3.SysUser">
<id property="id" column="id"/>
<result property="userName" column = "user_name"/>
<result property="userPassword" column = "user_password"/>
<result property="userEmail" column = "user_email"/>
<result property="userInfo" column = "user_info"/>
<result property="headImg" column = "head_img" jdbcType="BLOB"/>
<result property="create_time" column = "create_time" jdbcType="TIMESTAMP"/>
</resultMap>
<resultMap id="roleMap" type="com.mybatis.chapter3.SysRole">
<id property="id" column="id"/>
<result property="role_Name" column = "role_Name"/>
<result property="enabled" column = "enabled"/>
<result property="create_by" column = "create_by"/>
<result property="create_time" column = "create_time" jdbcType="TIMESTAMP"/>
</resultMap>
提供select
<select id="selectAllUserAndRoles" resultMap="userRoleListMap">
select
u.id,
u.user_name,
u.user_password,
u.user_email,
u.user_info,
u.head_img,
u.create_time,
r.id role_id,
r.role_name role_role_Name,
r.enabled role_enabled,
r.create_by role_create_by,
r.create_time role_create_time
from sys_user u
inner join sys_user_role ur on u.id = ur.user_id
inner join sys_role r on ur.role_id = r.id
</select>