在开发中需要用到表联查,而联查的表正好有重名字段。这时候再获取子表值时,会获取错误,将拿到的是父表的值。
像如下 WechatCategoryArticleListMapper.xml 文件:
留意 association 添加的位置 ,否则会报错误:Caused by: org.xml.sax.SAXParseException: 元素类型为 “resultMap” 的内容必须匹配 “(constructor?,id*,result*,association*,collection*,discriminator?)”
<?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.techpsp.wechatapi.mapper.WechatCategoryArticleListMapper"> <resultMap type="WechatCategoryArticleList" id="WechatCategoryArticleListResult"> <result property="id" column="id"/> <result property="categoryId" column="category_id"/> <result property="name" column="name"/> <result property="banner" column="banner"/> <result property="tips" column="tips"/> <result property="content" column="content"/> <result property="productName" column="product_name"/> <result property="productList" column="product_list"/> <result property="itemName" column="item_name"/> <result property="itemBanner" column="item_banner"/> <result property="itemList" column="item_list"/> <result property="createBy" column="create_by"/> <result property="createTime" column="create_time"/> <result property="updateBy" column="update_by"/> <result property="updateTime" column="update_time"/> <association property="wechatCategory" javaType="WechatCategory" resultMap="WechatCategoryResult"/> </resultMap> <resultMap id="WechatCategoryResult" type="WechatCategory"> <id property="id" column="id"/> <result property="categoryName" column="category_name"/> <result property="childName" column="child_name"/> </resultMap> <sql id="selectWechatCategoryArticleListVo"> select a.id, a.category_id, a.name, a.banner, a.tips, a.content, a.product_name, a.product_list, a.item_name, a.item_banner, a.item_list, a.create_by, a.create_time, a.update_by, a.update_time, b.name as category_name, b.child_name from wechat_category_article as a LEFT JOIN wechat_category as b ON a.category_id = b.id </sql> <select id="selectWechatCategoryArticleList" parameterType="WechatCategoryArticleList" resultMap="WechatCategoryArticleListResult"> <include refid="selectWechatCategoryArticleListVo"/> <where> <if test="name != null and name != ''">and a.name like concat('%', #{name}, '%')</if> </where> </select> <select id="selectWechatCategoryArticleListById" parameterType="Integer" resultMap="WechatCategoryArticleListResult"> <include refid="selectWechatCategoryArticleListVo"/> where a.id = #{id} </select> </mapper>
1、通过 as 定义表别名,通过 as 定义字段别名
2、对子表返回类型重新使用新的字段别名
3、实体类中定义 get/set 方法
如 domain/WechatCategory.java 文件
private String categoryName; public void setCategoryName(String categoryName) { this.categoryName = categoryName; } public String getCategoryName() { return categoryName; }
这就造成,WechatCategory 实体对象,返回一个表中并不存在的字段。而在联查操作时会有值,单独使用时,会是 null。
另外,记录下另一个问题:
WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver – [logException,208] – Resolved [org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘art’ in ‘class com.techpsp.wechatapi.domain.WechatCategoryArticleList’]
<select id="selectWechatCategoryArticleList" parameterType="WechatCategoryArticleList" resultMap="WechatCategoryArticleListResult"> <include refid="selectWechatCategoryArticleListVo"/> <where> <if test="a.name != null and a.name != ''">and a.name like concat('%', #{name}, '%')</if> </where> </select>
错误原因是,<if test=”a.name != null and a.name != ””>,使用了a 别名,if test 时,无法获取这个属性。
移除 a 别名即可,即修改后:
<if test=”name != null and name != ””>and a.name like concat(‘%’, #{name}, ‘%’)</if>