Mybatis中一对多遇到问题:无法映射对应的属性值

最近一个人做后台,项目中遇到许多坑,在此记录下来,防止今后再犯同样的错误。谨记!!
一、Mybatis一对多查询无法获取属性实例如下:
两个对象SysUser 和 SysRole 用户角色。

public class SysUser extends BaseEntity {
//    @Excel(name = "用户序号")
    private Long userId;
    private String loginname;
    private String usertype;
    @Excel(name = "用户名")
    private String username;
    @Excel(name = "地址")
    private String address;
    private String password;
    @Excel(name="用户组织")
    private String orgid;
    @Excel(name = "手机号码")
    private String mobilephone;
    private String isvalid;
    private List<SysUser> userList;
    @Excel(name = "性别",readConverterExp = "0=男,1=女,2=未知")
    private String sex;
    /**
     * 删除标记 0为删除,1为存在
     */
    private String delflag;
    @Excel(name="用户编码")
    private String usercode;
    private SysOrg org;
    private List<SysRole> roles;
    /** 角色组 */
    private Long[] roleIds;
    /** 岗位组 */
    private Long[] postIds;
    private Date resetPasswordTime;
    public boolean isAdmin()
    {
        return isAdmin(this.userId);
    }
    public static boolean isAdmin(Long userId)
    {
        return userId != null && 1L == userId;
    }

    public SysOrg getOrg() {
        return org;
    }
    public void setOrg(SysOrg org) {
        this.org = org;
    }
    public List<SysRole> getRoles() {
        return roles;
    }
    public void setRoles(List<SysRole> roles) {
        this.roles = roles;
    }
    public Long[] getRoleIds() {
        return roleIds;
    }
    public void setRoleIds(Long[] roleIds) {
        this.roleIds = roleIds;
    }
    public Long[] getPostIds() {
        return postIds;
    }
    public void setPostIds(Long[] postIds) {
        this.postIds = postIds;
    }

SysUserMapper.xml中
<resultMap id="BaseResultMap" type="com.zytt.models.system.SysUser">
        <id column="user_id" property="userId" />
        <result column="loginname" property="loginname" />
        <result column="username" property="username" />
        <result column="password" property="password" />
        <result column="orgid" property="orgid" />
        <result column="usertype" property="usertype" />
        <result column="mobilephone" property="mobilephone" />
        <result column="isvalid" property="isvalid" />
        <result column="sex" property="sex" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="remark" property="remark" />
        <result column="delflag" property="delflag" />
        <result column="address" property="address" />
        <result column="reset_password_time" property="resetPasswordTime" />
        <association property="org"    column="orgid" javaType="com.zytt.models.system.SysOrg" resultMap="orgResult" />
        <collection  property="roles" javaType="java.util.List" resultMap="RoleResult"/>
    </resultMap>
  collection中的properties对应SysUser对象中的属性名,id对应collection中的resultMap的属性值,type对应List中封装的类型。
<resultMap id="RoleResult" type="com.zytt.models.system.SysRole">
        <id property="roleId" column="role_id"/>
        <result property="roleName" column="role_name"/>
        <result property="roleKey" column="role_key"/>
        <result property="roleSort" column="role_sort"/>
        <result property="dataScope" column="data_scope"/>
        <result property="isvalid" column="role_isvalid"/>
    </resultMap>

遇到问题:登陆时查询无法自动映射找到对象对应的角色。这时我想到了在getRoles那打了个打断点,然后跟。。。。
在这里插入图片描述
继续跟下去得到:这里在这里插入图片描述这里应该就是到了Collection获取属性值roles然后去找到对应的role
在这里插入图片描述
在这里插入图片描述
找到这才发现原来是执行这个方法时没有得到对应的role_id,所以mybatis无法进行Collection关联。
找到这个mybatis中selectUserByUserName方法修改代码添加关联如下:

    <sql id="selectUserVO">
        select u.user_id,u.orgid,u.loginname,u.username,u.mobilephone,u.sex ,u.password,u.isvalid,u.delflag,
        o.ORGID, o.orgparent,o.ORGNAME,o.ISVALID as org_isvalid,
        r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.isvalid as role_isvalid
        from sys_user u
        left join sys_org o on o.ORGID	= u.ORGID
        left join sys_user_role ur on u.user_id = ur.user_id
        left join sys_role r on r.role_id = ur.role_id
    </sql>
    
   <select id="selectUserByUserName" parameterType="String" resultMap="BaseResultMap">
        <include refid="selectUserVO"/>
        where u.loginname = #{username,jdbcType=VARCHAR}
  </select>

总算解决啦!看来只要耐心跟着断点一定能找出问题!加油!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值