Mybatis多对多级联查询mapper.xml文件的设置

现有t_user,t_user_role,t_role三张表,其中t_user_role是他们的中间表,t_user和t_role是多对多的关系

1 结果嵌套的方式

使用结果嵌套的方式来获得t_user表的tUser对象

TUser实体

创建一个List成员属性
在这里插入图片描述

TUserMappers接口

 添加TUser getUserAndRoleAndPosition(Integer id);方法

TUserMappers.xml配置文件的设置

select语句
 <select id="getUserAndRoleAndPosition" parameterType="java.lang.Integer" resultMap="getRoles" >
    select *
    from t_user tu,t_user_role tur,t_role tr
    where
    tu.id = tur.user_id
    and
    tur.role_id = tr.id
    and tu.id = #{id,jdbcType=INTEGER}
  </select>
resultMap的设置
 <resultMap id="BaseResultMap" type="com.fxl.entities.TUser">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="real_name" jdbcType="VARCHAR" property="realName" />
    <result column="sex" jdbcType="TINYINT" property="sex" />
    <result column="mobile" jdbcType="VARCHAR" property="mobile" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="note" jdbcType="VARCHAR" property="note" />
    <result column="position_id" jdbcType="INTEGER" property="positionId" />
  </resultMap>

 <resultMap id="getRoles" type="TUser" extends="BaseResultMap">
   <collection property="roles" ofType="TRole" columnPrefix="role_">
     <result column="id" property="id" />
     <result column="Name" property="roleName" />
     <result column="note" property="note" />
   </collection>
 </resultMap>
测试
 @Test
    public void getUserAndRoleAndPosition(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);
        TUser user = mapper.getUserAndRoleAndPosition(1);
        List<TRole> roles = user.getRoles();
        for (TRole tRole : roles){
            System.out.println(tRole.getRoleName());
        }
        sqlSession.close();
    }
结果

在这里插入图片描述

2 查询语句嵌套的方式

使用查询语句嵌套的方式获得t_role表的tRole对象

TRole实体

 创建一个List<TUser>的成员变量

在这里插入图片描述

TRoleMappers接口

添加 TRole selectByPrimaryKey(Integer id);方法

TRoleMappers.xml配置文件的设置

select语句
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="GetUser">
   select id, role_name, note
   from t_role
   where id = #{id,jdbcType=INTEGER}
</select>
resultMap的设置
<resultMap id="BaseResultMap" type="com.fxl.entities.TRole">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="role_name" jdbcType="VARCHAR" property="roleName" />
    <result column="note" jdbcType="VARCHAR" property="note" />
 </resultMap>

  <resultMap id="GetUser" type="TRole" extends="BaseResultMap">
    <collection property="users" column="id" select="com.fxl.mapper.TUserMapper.selectByRoleId">
    </collection>
  </resultMap>

TUserMappers接口

添加List<TUser> selectByRoleId(Integer roleId);方法

TUserMappers.xml配置文件的设置

select语句
<select id="selectByRoleId" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        select tu.id, tu.user_name, tu.real_name, tu.sex, tu.mobile, tu.email, tu.note, tu.position_id
        from t_user tu,t_user_role tur
        where tu.id = tur.user_id
        and tur.role_id = #{roleId}
    </select>

测试

   @Test
    public void getRoleAndUser(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        TRoleMapper mapper = sqlSession.getMapper(TRoleMapper.class);
        TRole tRole = mapper.selectByPrimaryKey(1);
        List<TUser> users = tRole.getUsers();
        users.stream().forEach(e -> {
            System.out.println(e.getUserName());
        });
    }
结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/fxl5202301/article/details/88234132