码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis 在遇到查询属性同名时的解决方法

时间:2016-09-20 09:00:41      阅读:838      评论:0      收藏:0      [点我收藏+]

标签:

在多表查询或者一个表中父级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 在遇到查询属性同名时的解决方法

标签:

原文地址:http://www.cnblogs.com/yaket/p/5887420.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!