mybatis多对多查询问题

张宾 发布于 2013/10/11 15:35
阅读 3K+
收藏 0

直接上代码:

<mapper namespace="com.inspur.idcrm.mapper.UserMapper">
 <resultMap type="User" id="userMap">
  <id property="id" column="id"/>
      <result property="userID" column="USER_ID"/>
      <result property="userName" column="USER_NAME"/>
      <result property="userSex" column="USER_SEX"/>
      <result property="userTel" column="USER_TEL"/>
      <result property="userEmail" column="USER_EMAIL"/>
  <association property="customer" column="USER_CUSTOMERID" javaType="Customer">             
   <id property="id" column="id" />             
       <result property="customerCode" column="CUSTOMER_CODE"/>
       <result property="customerName" column="CUSTOMER_NAME"/>
       <result property="customeraddress" column="CUSTOMER_ADDRESS"/>
       <result property="customerContancts" column="CUSTOMER_CONTANCTS"/>
       <result property="customerTel" column="CUSTOMER_TEL"/>
       <result property="customerEmail" column="CUSTOMER_EMAIL"/>
       <result property="customerMemo" column="CUSTOMER_MEMO"/>
       <result property="customerTypeID" column="CUSTOMER_TYPEID"/>
       <result property="manageName" column="MANAGE_NAME"/>
       <result property="manager" column="MANAGER"/>
       <result property="managerTel" column="MANAGER_TEL"/>
       <result property="customerTeName" column="CUSTOMER_TE_NAME"/>
       <result property="customerTeTel" column="CUSTOMER_TE_TEL"/>       
  </association>
  <collection property="roleList" javaType="ArrayList" column="id" ofType="Role" select="selectUserRoles" > 
   <id property="id" column="id"/>
       <result property="roleName" column="role_name"/>
       <result property="roleDesc" column="role_desc"/>
  </collection>
 </resultMap>
 
 <select id="getUserList2"  parameterType="User" resultType="java.util.List" resultMap="userMap">
  select *
   from t_user as u, t_customer as c where 1=1 and u.USER_CUSTOMERID = c.id
 </select>
 
 <select id="selectUserRoles" resultType="Role" parameterType="int"> 
  select r.id,r.role_name,r.role_desc from t_role r left join t_user_role u on r.id=u.ROLE_ID where u.USER_ID=#{id}
 </select>  

用户表和角色表进行关联查询,打印查询结果时发现,

public void testGetUserList2(){
  User user=new User();
  List list=userService.getUserList2(user);
  if(list!=null&&list.size()>0){
   for(int i=0;i<list.size();i++){
    User obj=(User)list.get(i);
    System.out.println(obj.getUserName());
    System.out.println(obj.getCustomer().getCustomerName());
    if(obj.getRoleList()!=null&&obj.getRoleList().size()>0){
     for(int j=0;j<obj.getRoleList().size();j++){
      Role role=(Role)obj.getRoleList().get(j);
      System.out.println(role.getId());
      System.out.println(role.getRoleName());
      System.out.println(role.getRoleDesc());
     }
    }
   }
  }
 }

 

角色ID有值,角色名称roleName为空,查看后台debug日志,

2013/10/11 15:24:23 OSS DEBUG [java.sql.ResultSet] - <==        Row: 1, mahuateng, 马化腾, 1, 2345678, 1@qq.com, 2, 2, 00002, 腾讯, 幕府, 马化腾, 123456, mayhuateng@qq.com, memo, b2c, 江苏移动, wangliang, 00001, 马化腾, 123456
2013/10/11 15:24:23 OSS DEBUG [java.sql.PreparedStatement] - ==>  Executing: select r.id,r.role_name,r.role_desc from t_role r left join t_user_role u on r.id=u.ROLE_ID where u.USER_ID=?
2013/10/11 15:24:23 OSS DEBUG [java.sql.PreparedStatement] - ==> Parameters: 1(Integer)
2013/10/11 15:24:23 OSS DEBUG [java.sql.ResultSet] - <==    Columns: id, role_name, role_desc
2013/10/11 15:24:23 OSS DEBUG [java.sql.ResultSet] - <==        Row: 9, 超级管理员, 超级管理员
2013/10/11 15:24:23 OSS DEBUG [java.sql.ResultSet] - <==        Row: 4, zhaochaoyang, 张朝阳, 1, 5

 

发现结果已经查询到,就是有两个字段没有复制,请问怎样解决

 

加载中
返回顶部
顶部