代码演示

图:associatio、- 关联连接查询 -、:1 m:1 1:m 的流程。

图::1 m:1 1:m、associatio、- 关联连接查询 - 的流程。

图:associatio、:1 m:1 1:m、- 关联连接查询 - 的层次。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- 为了方便权限管理  - 查询用户及其角色信息
- 关联连接查询 - 只能使用resultMap
1:1 m:1 1:m m:n
- 关联对象属性

type=“user” 表示对应映射的是user这个pojo对象
id="userRoleMap" id对应给select语句使用的resultMap名称
autoMapping="true" 自动映射相同的表字段和pojo属性

association 对应的pojo对象
property="role" 对应表字段
javaType="Role" 对应pojo对象
-->
<resultMap type="user" id="userRoleMap" autoMapping="true">

<id column="userId" property="userId"/> (可以不写)

<!-- 关联对象 - 一个 1:1 m:1关联 association对象关联数据封装 -->
<association property="role" javaType="Role" autoMapping="true">
</association>

</resultMap>
1
2
3
4
5
6
7
<!-- 登录不能使用缓存 ,需要关闭:useCache="false"-->
<select id="loginUserRole" parameterType="user" resultMap="userRoleMap" useCache="false">
select u.userId, userCode, name,r.roleId,r.rolename
from tuser u,role r
where u.roleId=r.roleId and
userCode = #{userCode} and password = #{password}
</select>
1
2
3
4
5
6
<!-- 查询结果多条(即List<User>),用的也是 - resultType - 单个对象  -->	
<select id="queryUsersRole" resultMap="userRoleMap">
select u.userId, userCode, name,r.roleId,r.rolename
from tuser u,role r
where u.roleId=r.roleId
</select>