MyBatis映射文件的resultMap如何做表关联_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > MyBatis映射文件的resultMap如何做表关联

MyBatis映射文件的resultMap如何做表关联

 2015/1/12 18:36:06  hae  程序员俱乐部  我要评论(0)
  • 摘要:MyBatis的核心是其映射文件,SqlMap文件,里面配置了项目中用到了什么SQL语句,和数据库相关的逻辑都在这个映射文件里.顾名思义,映射文件就是对Java对象和SQL的映射.这里简单介绍一下映射文件中resultMap的用法:resultMap–它描述如何将结果集映射到Java对象.resultMap属性:type为java实体类;id为此resultMap的标识:<resultMapid="BaseResultMap"type="com.test.mybatis.vo
  • 标签:Map 文件

MyBatis的核心是其映射文件,SqlMap文件,里面配置了项目中用到了什么SQL语句,和数据库相关的逻辑都在这个映射文件里.顾名思义,映射文件就是对Java对象和SQL的映射.这里简单介绍一下映射文件中resultMap的用法:resultMap – 它描述如何将结果集映射到Java对象.
resultMap属性:type为java实体类;id为此resultMap的标识:<resultMap id="BaseResultMap" type="com.test.mybatis.vo.MybatisOrder" >
resultMap的子元素:
id – 一般对应到数据库中该行的ID,设置此项可以提高Mybatis性能.
result – 映射到JavaBean 的某个"简单类型"属性,String,int等.
association – 映射到JavaBean 的某个"复杂类型"属性,其他JavaBean类.
collection –复杂类型集合,a collection of complex types
比如现在有一个Order表,Customer表和OrderItem表,它们之间的关系为:一个Order关联到一个Customer(单向关联),一个Order有多个OrderItem(双向关联).

对应的Java对象文件为,

?

[html]?view plaincopy ?
    class="dp-xml">
  1. public?class?MybatisOrder?{??
  2. ????....??
  3. ????private?Mybatiscustomer?customer;??
  4. ????private?List<MybatisOrderItem>?itemList;??
  5. ????....??
  6. public?class?MybatisOrderItem?{??
  7. ????private?MybatisOrder?order;??
  8. ????...??

对应的mapper文件为:
MybatiscustomerMapper.xml:

[html]?view plaincopy ?
  1. <resultMap?id="AssociationSelectMap"?type="com.test.mybatis.vo.MybatisOrder"?>??
  2. ????<id?column="ORDERID"?property="orderid"?jdbcType="DECIMAL"?/>??
  3. ????<result?column="ORDERTYPE"?property="ordertype"?jdbcType="VARCHAR"?/>??
  4. ????<result?column="ORDERDATE"?property="orderdate"?jdbcType="DATE"?/>??
  5. ????<association?property="customer"?column="CUSTOMERID"???
  6. ????????select="com.test.mybatis.mapper.MybatiscustomerMapper.getCustomerByID"/>???
  7. ????<collection?property="itemList"?column="ORDERID"?javaType="ArrayList"?ofType="MybatisOrderItem"???
  8. ????????select="com.test.mybatis.mapper.MybatisOrderItemMapper.selectItemsByOrderID"/>??
  9. </resultMap>??
  10. <select?id="getOrderByID"?resultMap="AssociationSelectMap"?parameterType="java.math.BigDecimal"?>??
  11. ????select?*from?MYBATISORDER?where?ORDERID?=?#{orderid,jdbcType=DECIMAL}??
  12. </select>??

MybatisOrderItemMapper.xml:

[html]?view plaincopy ?
  1. ?<resultMap?id="AssociationMap"?type="com.test.mybatis.vo.MybatisOrderItem"?>??
  2. ???<id?column="ITEMID"?property="itemid"?jdbcType="DECIMAL"?/>??
  3. ???<result?column="ITEMTYPE"?property="itemtype"?jdbcType="VARCHAR"?/>??
  4. ???<result?column="ITEMQUANTITY"?property="itemquantity"?jdbcType="VARCHAR"?/>??
  5. ???<result?column="ITEMCOST"?property="itemcost"?jdbcType="VARCHAR"?/>??
  6. ???<association?property="order"?column="ORDERID"???
  7. select="com.test.mybatis.mapper.MybatisOrderMapper.getOrderByID"/>???
  8. ?</resultMap>??
  9. ?<select?id="selectItemByID"?resultMap="AssociationMap"?parameterType="java.math.BigDecimal"?>??
  10. ???select?*from?MYBATISORDERITEMwhere?ITEMID?=?#{itemid,jdbcType=DECIMAL}??
  11. ?</select>??

MybatiscustomerMapper.xml:因为不和别的表做关联,只有简单的id,result配置.

[html]?view plaincopy ?
  1. <resultMap?id="BaseResultMap"?type="com.test.mybatis.vo.Mybatiscustomer"?>??
  2. ??<id?column="ID"?property="id"?jdbcType="DECIMAL"?/>??
  3. ??<result?column="NAME"?property="name"?jdbcType="VARCHAR"?/>??
  4. </resultMap>??
  5. <select?id="getCustomerByID"?resultMap="BaseResultMap"?parameterType="java.math.BigDecimal"?>??
  6. ??select?*from?MYBATISCUSTOMER?where?ID?=?#{id,jdbcType=DECIMAL}??
  7. </select>??

?

MybatisOrderMapper的getOrderByID会先从MYBATISORDER取数据,然后根据CUSTOMERID调用MybatiscustomerMapper.getCustomerByID,然后根据ORDERID调用MybatisOrderItemMapper.selectItemsByOrderID,对应每一条MYBATISORDER数据,都会分别访问另外两个表各一次.
通过将association和collection的select功能替换为resultMap,再用join方式的SQL可以用一条SQL语句将关联数据取出来:

?

[html]?view plaincopy ?
  1. ?<resultMap?id="AssociationResultMap"?type="com.test.mybatis.vo.MybatisOrder"?>??
  2. ???<id?column="ORDERID"?property="orderid"?jdbcType="DECIMAL"?/>??
  3. ???<result?column="ORDERTYPE"?property="ordertype"?jdbcType="VARCHAR"?/>??
  4. ???<result?column="ORDERDATE"?property="orderdate"?jdbcType="DATE"?/>??
  5. ??
  6. <association?property="customer"?column="CUSTOMERID"???
  7. ????resultMap="com.test.mybatis.mapper.MybatiscustomerMapper.BaseResultMap"/>???
  8. <collection?property="itemList"?column="ORDERID"?javaType="ArrayList"???
  9. ????ofType="com.test.mybatis.vo.MybatisOrderItem"???
  10. ????resultMap="com.test.mybatis.mapper.MybatisOrderItemMapper.BaseResultMap"/>??
  11. ?</resultMap>??
  12. ?<select?id="getOrderAssociation"?parameterType="String"?resultMap="AssociationResultMap">????
  13. ????SELECT?*????
  14. ??????FROM?mybatisOrder?ord?LEFT?JOIN?mybatiscustomer?customer?ON?ord.customerId?=?customer.ID???
  15. ??????LEFT?JOIN?mybatisOrderItem?item?ON?ord.orderid?=?item.orderid???
  16. ?????WHERE?ord.orderid?=?#{id}??
  17. ??</select>???

?

发表评论
用户名: 匿名