mybatis 在遇到查询属性同名时的解决方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis 在遇到查询属性同名时的解决方法相关的知识,希望对你有一定的参考价值。
在多表查询或者一个表中父级id查询时,查询结果中出现同名属性,如下:
1 select cus.customer_id,cus.customer_name,cus.level_num,co.CO_NAME, 2 cus.customer_code,cus.industry,re.THE_NAME,re2.THE_NAME ,cus.customer_linkman_name, 3 cus.customer_linkman_phone,cus.customer_address,cus.Created_By,cus.Created_Time, 4 cus.Updated_By,cus.Updated_Time 5 from sd_bs_customer_info cus 6 left join ip_company co on cus.company_id=co.CO_ID 7 left join ip_region re on cus.city=re.THE_ID 8 left join ip_region re2 on re2.THE_ID=cus.Province 9 10 where cus.customer_name like ‘%1%‘ 11 limit 0,5
结果中会出现同名字段THE_NAME :
这样的同名属性,mybatis在映射时,会只选择其中一个属性。
解决方法如下:
给第二个THE_NAME起别名,as ProvinceName
1 <resultMap id="CustomerInfoResult" type="com.ufgov.sd.entity.bs.BsCustomerEntity"> 2 <id property="customerId" column="customer_id" /> 3 <result property="customerName" column="customer_name"/> 4 <result property="levelNum" column="level_num"/> 5 <result property="companyId" column="CO_NAME"/> 6 <result property="customerCode" column="customer_code"/> 7 <result property="industry" column="industry"/> 8 <result property="province" column="ProvinceName"/> 9 <result property="city" column="THE_NAME"/> 10 <result property="customerLinkmanName" column="customer_linkman_name"/> 11 <result property="customerLinkmanPhone" column="customer_linkman_phone"/> 12 <result property="customerAddress" column="customer_address"/> 13 <result property="createdBy" column="Created_By"/> 14 <result property="createdTime" column="Created_Time"/> 15 <result property="updatedBy" column="Updated_By"/> 16 <result property="updatedTime" column="Updated_Time"/> 17 </resultMap> 18 <!--根据客户名称, 查询客户档案信息,并进行分页--> 19 <!-- 在遇到查询属性同名时,可以通过起别名来解决映射问题,as:ProvinceName--> 20 <select id="queryCusInfo" parameterType="java.util.Map" resultMap="CustomerInfoResult"> 21 select cus.customer_id,cus.customer_name,cus.level_num,co.CO_NAME, 22 cus.customer_code,cus.industry,re.THE_NAME,re2.THE_NAME as ProvinceName,cus.customer_linkman_name, 23 cus.customer_linkman_phone,cus.customer_address,cus.Created_By,cus.Created_Time, 24 cus.Updated_By,cus.Updated_Time 25 from sd_bs_customer_info cus 26 left join ip_company co on cus.company_id=co.CO_ID 27 left join ip_region re on cus.city=re.THE_ID 28 left join ip_region re2 on re2.THE_ID=cus.Province 29 <where> 30 <if test="customerName!=null and customerName!=‘‘"> 31 customer_name like CONCAT(CONCAT(‘%‘,#{customerName}),‘%‘) 32 </if> 33 </where> 34 limit #{pageStart},#{pageSize} 35 </select>
结果如下:
以上是关于mybatis 在遇到查询属性同名时的解决方法的主要内容,如果未能解决你的问题,请参考以下文章
mybatis高级_数据库中的列和实体类不匹配时的两种解决方法_模糊查询_只能标签