Mybatis嵌套查询映射Java bean的集合属性

这篇博客详细介绍了MyBatis中resultMap的使用,特别是如何配置resultMap来映射复杂的查询结果,包括主结果集与子结果集的关联。通过一个具体的select查询示例,展示了如何处理一对多的关系,并强调了column属性在resultMap和collection中的命名规则,避免查询结果冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<resultMap id="ProjectPageableRespVoResultMap" type="com.ioam.devaddrmanage.controller.response.ProjectPageableRespVo">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="deviceNum" column="device_num" jdbcType="INTEGER"/>
        <result property="serialNum" column="serial_num" jdbcType="VARCHAR"/>
        <result property="regionCode" column="region_code" jdbcType="VARCHAR"/>
        <result property="projectName" column="project_name" jdbcType="VARCHAR"/>
        <result property="mac0" column="mac0" jdbcType="CHAR"/>
        <result property="mac1" column="mac1" jdbcType="CHAR"/>
        <result property="used" column="used" jdbcType="TINYINT"/>
        <result property="type" column="type" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="DATE"/>
        <result property="updateTime" column="update_time" jdbcType="DATE"/>
        <result property="regionName" column="region_name" jdbcType="VARCHAR"/>
        <collection property="deviceInfos" ofType="com.ioam.devaddrmanage.entity.DamDeviceInfo"
                    javaType="java.util.ArrayList">
            <id property="id" column="did" jdbcType="INTEGER"/>
            <result property="projectId" column="project_id" jdbcType="INTEGER"/>
            <result property="ip" column="ip" jdbcType="VARCHAR"/>
            <result property="projectName" column="d_project_name" jdbcType="VARCHAR"/>
            <result property="used" column="d_used" jdbcType="TINYINT"/>
            <result property="mac" column="mac" jdbcType="CHAR"/>
            <result property="deviceType" column="device_type" jdbcType="INTEGER"/>
            <result property="regionCode" column="d_region_code" jdbcType="VARCHAR"/>
            <result property="createTime" column="d_create_time" jdbcType="DATE"/>
            <result property="updateTime" column="d_update_time" jdbcType="DATE"/>
        </collection>
    </resultMap>

<select id="selectPageList" resultMap="ProjectPageableRespVoResultMap" parameterType="com.ioam.devaddrmanage.controller.request.ProjectPageableReqDto">
        select
            p.id,
            p.device_num,
            p.serial_num,
            p.region_code,
            p.project_name,
            p.mac0,
            p.mac1,
            p.used,
            p.type,
            p.create_time,
            p.update_time,
            r.region_name,
            d.id as did,
            d.project_id,
            d.ip,
            d.project_name as d_project_name,
            d.used as d_used,
            d.mac,
            d.device_type,
            d.region_code as d_region_code,
            d.create_time as d_create_time,
            d.update_time as d_update_time
        FROM dam_project_info p
        left join dam_device_info d on p.id = d.project_id
        left join t_sys_region r on p.region_code = r.region_code
 </select>

注意resultmap的column和collection的column名称不能重复,否则会忽略collection的查询结果而是用父结果集的重名字段

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值