我是靠谱客的博主 平常水池,最近开发中收集的这篇文章主要介绍Mybatis 多对多关联查询的两种方式:嵌套结果与嵌套查询先决条件嵌套结果嵌套查询,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

先决条件

  1. 多对多需要一种中间表建立连接关系
  2. 多对多关系是由两个一对多关系组成的,一对多可以也可以用两种方式实现

嵌套结果

Mapper 接口

List<TUser> selectUserRole();

Mapper XML配置

<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 接口

List<TRole> selectRoleandUsers();

Mapper XML配置

<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 多对多关联查询的两种方式:嵌套结果与嵌套查询先决条件嵌套结果嵌套查询所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部