Myabtis中的resultMap中一对多映射使用

项目源码:https://github.com/AbitGo/mybatis_csdn/tree/master/workspace_mybatis_0x03

 

首先我们将association改成了collection,后将property配置为reloList。

 

<resultMap id="userRoleListMap" extends="userMap" type="com.mybatis.chapter3.SysUser">
        <id property="id" column="id"/>
        <result property="userName" column = "user_name"/>
        <result property="userPassword" column = "user_password"/>
        <result property="userEmail" column = "user_email"/>
        <result property="userInfo" column = "user_info"/>
        <result property="headImg" column = "head_img" jdbcType="BLOB"/>
        <result property="create_time" column = "create_time" jdbcType="TIMESTAMP"/>
        
        <collection property="roleList" columnPrefix="role_"
                    ofType="com.mybatis.chapter3.SysRole">
            <id property="id" column="id"/>
            <result property="role_Name" column = "role_Name"/>
            <result property="enabled" column = "enabled"/>
            <result property="create_by" column = "create_by"/>
            <result property="create_time" column = "create_time" jdbcType="TIMESTAMP"/>
        </collection>
    </resultMap>

我们逐步对resultMap进行简化,我们可以使用resultMap进行快速简化。

<resultMap id="userRoleListMap" extends="userMap" type="com.mybatis.chapter3.SysUser">
        <id property="id" column="id"/>
        <collection property="roleList"
                    columnPrefix="role_"
                    resultMap="com.mybatis.chapter3.UserMapper.selectRolebyId">
        </collection>
    </resultMap>

userMap以及roleMap

<resultMap id="userMap" type="com.mybatis.chapter3.SysUser">
        <id property="id" column="id"/>
        <result property="userName" column = "user_name"/>
        <result property="userPassword" column = "user_password"/>
        <result property="userEmail" column = "user_email"/>
        <result property="userInfo" column = "user_info"/>
        <result property="headImg" column = "head_img" jdbcType="BLOB"/>
        <result property="create_time" column = "create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="roleMap" type="com.mybatis.chapter3.SysRole">
        <id property="id" column="id"/>
        <result property="role_Name" column = "role_Name"/>
        <result property="enabled" column = "enabled"/>
        <result property="create_by" column = "create_by"/>
        <result property="create_time" column = "create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

提供select

<select id="selectAllUserAndRoles" resultMap="userRoleListMap">
        select
        u.id,
        u.user_name,
        u.user_password,
        u.user_email,
        u.user_info,
        u.head_img,
        u.create_time,
        r.id role_id,
        r.role_name role_role_Name,
        r.enabled role_enabled,
        r.create_by role_create_by,
        r.create_time role_create_time
        from sys_user u
        inner join sys_user_role ur on u.id = ur.user_id
        inner join sys_role r on ur.role_id = r.id
    </select>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值