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

来源:互联网 发布:全球数据化时代txt 编辑:程序博客网 时间:2024/06/07 01:53

问题

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了。