Mybatis连3表查询数据resultMap结果映射

https://blog.csdn.net/sunrise_zhu/article/details/53310662

Mybatis连结3表查询数据resultMap结果映射

一、前言                                                                                                                                     

Mybatis实现了sql与java代码的分离,达到了解耦合的目的,配置sql语句时有个resultType=""的属性,用于定义sql查询返回结果数据类型,但它有局限性,就是当连表查询的时候,你很难说定义返回的是某一个类型,这时就需要用到一个标签了,那就是resultMap,结果映射.

二、从sql查询结果到模型实体                                                                                                     

在深入ResultMap标签前,我们需要了解从SQL查询结果集到JavaBean或POJO实体的过程。

  1. 通过JDBC查询得到ResultSet对象

  2. 遍历ResultSet对象并将每行数据暂存到HashMap实例中,以结果集的字段名或字段别名为键,以字段值为值

  3. 根据ResultMap标签的type属性通过反射实例化领域模型

  4. 根据ResultMap标签的type属性和id、result等标签信息将HashMap中的键值对,填充到领域模型实例中并返回

三、ResultMap标签                                                                                                                 

id属性:标识resultMap,通过它去识别.

type属性:返回值类型,类的全定向名.

autoMapping属性:值为true(默认)|false,是否自动映射。自动映射功能就是自动查找与字段名小写同名的属性名,并调用setter方法。而设置为false后,则需要在`resultMap`内明确注明映射关系才会调用对应的setter方法。

四、ResultMap中子标签                                                                                                           

在讲标签时先讲述下数据库中表数据的对应关系,如一对一,一对多,多对多等关系,具体来说,如有一个俱乐部表,一个全员表,一个俱乐部里有许多球员,而许多球员对应一个俱乐部,则俱乐部与球员之间的关系就是一对多与多对一的关系。


<collection>子标签:对应表格关系中的多

<association>子标签:对应表格关系中的一

五、联结三表示例                                                                                                                       

示例是联结三表,查询结果作为示范,就算联结再多表也能举一反三。这三个表的关系如下图

sql语句联结:http://download.csdn.net/detail/sunrise_zhu/9687991
核心代码:
clubMapper.xml中结果映射
[html]  view plain  copy
  1. <!-- 结果映射 -->  
  2. <resultMap type="com.sxt.entity.Club" id="clubBean" autoMapping="true">  
  3. <!--column指向数据库列名   property指向pojo对象中字段名-->  
  4.     <result column="cid" property="cid"/>  
  5.     <result column="cname" property="cname"/>  
  6.     <result column="city" property="city"/>  
  7.     <!-- property指的是在bean中字段名 ofType类的全定向名 -->  
  8.     <collection property="players" ofType="com.sxt.entity.Player">  
  9.         <result column="pid" property="pid"/>  
  10.         <result column="pname" property="pname"/>  
  11.         <result column="position" property="position"/>  
  12.         <result column="cid" property="cid"/>  
  13.         <association property="abilities" javaType="com.sxt.entity.Abilities">  
  14.             <result column="aid" property="aid"/>  
  15.             <result column="pid" property="pid"/>  
  16.             <result column="shoot" property="shoot"/>  
  17.         </association>  
  18.     </collection>  
  19. </resultMap>  
clubMapper.xml中sql语句
[html]  view plain  copy
  1. <select id="joinTwo" resultMap="clubBean">  
  2.     select c.*,p.*,a.*   
  3.     from clubs c   
  4.     join player p  
  5.     on c.cid = p.cid   
  6.     join abilities a   
  7.     on a.pid = p.pid;  
  8. </select>  
playerMapper.xml中的结果映射:
[html]  view plain  copy
  1. <!-- 结果映射 -->  
  2.     <resultMap type="com.sxt.entity.Player" id="playerBean">  
  3.         <!--column指向数据库列名 property指向pojo对象中字段名 -->  
  4.         <result column="pid" property="pid" />  
  5.         <result column="pname" property="pname" />  
  6.         <result column="position" property="position" />  
  7.         <result column="cid" property="cid" />  
  8.         <association property="club" javaType="com.sxt.entity.Club">  
  9.             <result column="cid" property="cid" />  
  10.             <result column="cname" property="cname" />  
  11.             <result column="city" property="city" />  
  12.         </association>  
  13.         <association property="abilities" javaType="com.sxt.entity.Abilities">  
  14.             <result column="aid" property="aid"/>  
  15.             <result column="pid" property="pid"/>  
  16.             <result column="shoot" property="shoot"/>  
  17.         </association>  
  18.     </resultMap>  
abilitiesMapper.xml结果映射
[html]  view plain  copy
  1. <!-- 结果映射 -->  
  2.     <resultMap type="com.sxt.entity.Abilities" id="abilitiesBean">  
  3.         <!--column指向数据库列名 property指向pojo对象中字段名 -->  
  4.         <result column="aid" property="aid"/>  
  5.         <result column="pid" property="pid"/>  
  6.         <result column="shoot" property="shoot"/>  
  7.         <association property="player" javaType="com.sxt.entity.Player">  
  8.             <result column="pid" property="pid"/>  
  9.             <result column="pname" property="pname"/>  
  10.             <result column="position" property="position"/>  
  11.             <result column="cid" property="cid"/>  
  12.         </association>  

五、参考文件                                                                                                                           

http://www.cnblogs.com/fsjohnhuang/p/4076592.html

猜你喜欢

转载自blog.csdn.net/weixin_39214481/article/details/80051282