我是靠谱客的博主 孝顺香烟,这篇文章主要介绍MyBatis关联查询,表字段相同,resultMap映射问题的解决办法,现在分享给大家,希望可以做个参考。

问题描述:在使用mybatis进行多表联合查询时,如果两张表中的字段名称形同,会出现无法正常映射的问题。

问题解决办法:在查询时,给重复的字段 起别名,然后在resultMap中使用别名进行映射。

给出一个小demo:如下是一个**mapper.xml映射文件的一个查询片段,用到了四表联合查询,其中订单id,项目id,回报id,是需要查询的数据,并且字段名都是id,显然是重复字段,此时就需要为这些重复的id起别名了,请看下面的红色部分代码:

[html]  view plain  copy
  1. <resultMap type="com.lz.pojo.MyOrder" id="myOrder">  
  2.         <result column="name" property="name" />  
  3.         <result column="type" property="type" />  
  4.         <result column="money" property="money" />  
  5.         <result column="content" property="content" />  
  6.         <result column="state" property="state" />  
  7.         <result column="pic" property="pic" />  
  8.         <result column="pid" property="pid" />  
  9.         <result column="hid" property="hid" />  
  10.         <result column="oid" property="oid" />  
  11.     </resultMap>  
  12.     <!-- findMyOrderByUserId:根据用户id,查询我的订单 -->  
  13.     <select id="findMyOrderByUserId" parameterType="int" resultMap="myOrder">  
  14.         SELECT p.name,p.type,o.money,h.content,o.state,h.pic,p.id pid,h.id hid,o.id oid  
  15.             FROM project p,huibao h,`order` o,`user` u  
  16.             WHERE o.projectId=p.id AND o.userId=u.id and o.hbId=h.id AND u.id=#{userId}   
  17.     </select>  

 

这样处理后,查询时就可以成功的完成,实体类与查询字段的映射了。

最后

以上就是孝顺香烟最近收集整理的关于MyBatis关联查询,表字段相同,resultMap映射问题的解决办法的全部内容,更多相关MyBatis关联查询,表字段相同,resultMap映射问题内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部