问题
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>