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了。
阅读全文
0 0
- mybatis联合查询遇到相同字段解决办法
- MyBatis两张表中存在相同字段名,联表查询时的冲突解决办法
- mysql 多张表联合查询、一个字段 结构相同
- Mybatis联合查询
- mybatis 联合查询
- Mybatis中联合查询
- MyBatis的联合查询
- mybatis联合查询
- mybatis 联合查询注意事项
- mybatis 联合查询
- mybatis 联合查询2
- mysql两个字段相同的表联合查询(union的使用)
- 最终解决Mybatis多表联合查询中字段名重复导致的各路问题
- mybatis联合查询(2)
- mybatis扩展联合查询条件
- MyBatis之多表联合查询
- MyBatis联合查询配置详解
- mybatis 多表联合查询
- opencv 学习之 SVM / ANN 图片分类(OPENCV3.2)
- mysql RPM 安装的卸载方式
- GitChat · 安全 | 揭秘我国的电子取证技术
- Java 二叉树的四种递归与非递归遍历
- Ribbon负载均衡机制(搭配eureka)
- mybatis联合查询遇到相同字段解决办法
- 【大数据部落】(数据挖掘)如何用大数据做用户异常行为分析
- 类和接口的定义
- 由H5游戏谈起,H5开发具有哪些优势?
- 深度学习之数值计算 (一)overflow and underflow
- vuforiaSDK发布AR手机端应用的小细节和小建议(1)
- 【Oracle】EXPDP和IMPDP数据泵进行导出导入的方法
- warning C4819 解决方案 warning C4819: The file contains a character that cannot be represented in the cu
- MyBatis动态传表名,字段名