mybatis之表关系

<!-- if条件判断的时候要判断的是 最后的一层  其实就是盘但 要用到的 .此时写map!=null是不对的原因是此时的参数类型是类型不是具体的某个值 -->
<!-- 是值的判定而不是类型的判定 类型不能未null吧 -->
<resultMap type="user" id="ref">
   <id      column="id"    property="id"/>
   <result  column="username"      property="username"/>
   <result  column="birthday"      property="birthday"/>
   <result  column="sex"           property="sex"/>
   <result  column="address"       property="address"/>
   <association property="role"  javaType="role">
     <id      column="rid"  property="id"/>
     <result  column="ROLE_NAME"   property="Rolename"/>
     <result  column="ROLE_DESC"   property="roledesc"/>
   </association>
   
   <!-- 关于表的数据查询了2条或者多条的时候会在取最后的关联数据 并将前面的覆盖掉。因为1对1的设置不允许出现一堆多的情况
        所以表关联必须明确他们的关系 -->
<!--    <collection property="roles"   ofType="role">
     <id      column="rid"  property="id"/>
     <result  column="ROLE_NAME"   property="Rolename"/>
     <result  column="ROLE_DESC"   property="roledesc"/>
   </collection> -->
</resultMap>
<select id="finduser"  parameterType="map"  resultMap="ref">
      SELECT  u.*,r.id as rid ,r.ROLE_NAME ROLE_NAME ,r.ROLE_DESC ROLE_DESC from user  u inner JOIN  user_role  ur on u.id=ur.UID  join role r  on  ur.RID=r.ID   
        <where>
        <if  test="username !=null">
            and u.username=#{username}
           </if>
        </where>     
</select>

<select id="finduser1"  parameterType="map"  resultMap="ref">
      SELECT  u.* from user  u inner JOIN  user_role  ur on u.id=ur.UID  join role r  on  ur.RID=r.ID   
        <where>
        <if  test="username !=null">
            and u.username=#{username}
           </if>
        </where>     

</select>


@Test
public void   finduser() {

Map  map=new HashMap<>();
map.put("username", "老王");
List<User> users=mapper.finduser(map);
System.out.println(users);
}

@Test
public void   finduser1() {

Map  map=new HashMap<>();
map.put("username", "老王");
/* u.username ='老王';*/
User users=mapper.finduser1(map);
//转换成json数据返回给前端页面
System.out.println(users);

}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值