<!-- 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>
<!-- 是值的判定而不是类型的判定 类型不能未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);
}