mybatis联合查询遇到相同字段解决办法

23 篇文章 0 订阅

问题

mybatis在联合查询的时候,有时候会遇到相同的字段,前面的字段值会覆盖后面字段的值。

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.hand.core.demos.mapper.DemokeyMapper">
    <resultMap id="BaseResultMap" type="com.hand.core.demos.dto.Demokey">
        <result column="id" property="id" jdbcType="DECIMAL" />
        <result column="demo_id" property="demoId" jdbcType="DECIMAL" />
        <result column="name" property="name" jdbcType="VARCHAR" />
    </resultMap>
    <!--联合查询 star-->
    <resultMap id="WithDemoResultMap" type="com.hand.core.demos.dto.Demokey">
        <result column="id" property="id" jdbcType="DECIMAL" />
        <result column="demo_id" property="demoId" jdbcType="DECIMAL" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <association property="demos" javaType="com.hand.core.demos.dto.Demos">
            <result column="id" property="id" jdbcType="DECIMAL" />
            <result column="name" property="name" jdbcType="VARCHAR" />
        </association>
    </resultMap>
    <sql id="WithDemos_Column_List">
        k.id,k.demo_id,k.name,d.id,d.name
    </sql>
    <select id="selectWithDemos" parameterType="com.hand.core.demos.dto.Demokey" resultMap="WithDemoResultMap">
        SELECT <include refid="WithDemos_Column_List"/>
        from hap_demokey k LEFT JOIN hap_demos d on k.demo_id = d.id
    </select>
    <!--联合查询 end-->
</mapper>

之前想的是会一一的映射

k.id,k.demo_id,k.name,d.id,d.name 会映射 Demokey.id,Demokey.demo_id,Demokey.name,Demos.id,Demos.name

但是实际却是这样的:

Demokey.id,Demokey.demo_id,Demokey.name,Demokey.id,Demokey.name

解决办法

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.hand.core.demos.mapper.DemokeyMapper">
    <resultMap id="BaseResultMap" type="com.hand.core.demos.dto.Demokey">
        <result column="id" property="id" jdbcType="DECIMAL" />
        <result column="demo_id" property="demoId" jdbcType="DECIMAL" />
        <result column="name" property="name" jdbcType="VARCHAR" />
    </resultMap>
    <!--联合查询 star-->
    <resultMap id="WithDemoResultMap" type="com.hand.core.demos.dto.Demokey">
        <result column="id" property="id" jdbcType="DECIMAL" />
        <result column="demo_id" property="demoId" jdbcType="DECIMAL" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <association property="demos" javaType="com.hand.core.demos.dto.Demos">
            <result column="did" property="id" jdbcType="DECIMAL" />
            <result column="dname" property="name" jdbcType="VARCHAR" />
        </association>
    </resultMap>
    <sql id="WithDemos_Column_List">
        k.id,k.demo_id,k.name,d.id did,d.name dname
    </sql>
    <select id="selectWithDemos" parameterType="com.hand.core.demos.dto.Demokey" resultMap="WithDemoResultMap">
        SELECT <include refid="WithDemos_Column_List"/>
        from hap_demokey k LEFT JOIN hap_demos d on k.demo_id = d.id
    </select>
    <!--联合查询 end-->
</mapper>

给对应的字段起别名,在映射的结果集对象里面用别名,就ok了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值