先决条件
- 多对多需要一种中间表建立连接关系
- 多对多关系是由两个一对多关系组成的,一对多可以也可以用两种方式实现
嵌套结果
Mapper 接口
复制代码
1
2List<TUser> selectUserRole();
Mapper XML配置
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<select id="selectUserRole" resultMap="userRoleInfo"> select a.id, a.userName, a.realName, a.sex, a.mobile, a.note, b.role_id, c.role_name, c.note role_note from t_user a, t_user_role b, t_role c where a.id = b.user_id AND b.role_id = c.id </select>
t_user_role 就是中间表,只有两列,user_id、role_id
嵌套查询
Mapper 接口
复制代码
1
2List<TRole> selectRoleandUsers();
Mapper XML配置
复制代码
1
2
3
4
5
6
7
8
9<resultMap id="RoleandUsers" type="TRole" extends="BaseResultMap"> <collection property="users" fetchType="lazy" column="id" select="com.enjoylearning.mybatis.mapper.TUserMapper.selectUserByRoleId"></collection> </resultMap> <select id="selectRoleandUsers" resultMap="RoleandUsers"> select <include refid="Base_Column_List" /> from t_role </select>
最后
以上就是平常水池最近收集整理的关于Mybatis 多对多关联查询的两种方式:嵌套结果与嵌套查询先决条件嵌套结果嵌套查询的全部内容,更多相关Mybatis内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复