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

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对象文件为,

public class MybatisOrder {
	....
    private Mybatiscustomer customer;
    private List<MybatisOrderItem> itemList;
	....
public class MybatisOrderItem {
    private MybatisOrder order;
	...
对应的mapper文件为:
MybatiscustomerMapper.xml:
<resultMap id="AssociationSelectMap" type="com.test.mybatis.vo.MybatisOrder" >
	<id column="ORDERID" property="orderid" jdbcType="DECIMAL" />
	<result column="ORDERTYPE" property="ordertype" jdbcType="VARCHAR" />
	<result column="ORDERDATE" property="orderdate" jdbcType="DATE" />
	<association property="customer" column="CUSTOMERID" 
		select="com.test.mybatis.mapper.MybatiscustomerMapper.getCustomerByID"/> 
	<collection property="itemList" column="ORDERID" javaType="ArrayList" ofType="MybatisOrderItem" 
		select="com.test.mybatis.mapper.MybatisOrderItemMapper.selectItemsByOrderID"/>
</resultMap>
<select id="getOrderByID" resultMap="AssociationSelectMap" parameterType="java.math.BigDecimal" >
	select *from MYBATISORDER where ORDERID = #{orderid,jdbcType=DECIMAL}
</select>
MybatisOrderItemMapper.xml:
  <resultMap id="AssociationMap" type="com.test.mybatis.vo.MybatisOrderItem" >
    <id column="ITEMID" property="itemid" jdbcType="DECIMAL" />
    <result column="ITEMTYPE" property="itemtype" jdbcType="VARCHAR" />
    <result column="ITEMQUANTITY" property="itemquantity" jdbcType="VARCHAR" />
    <result column="ITEMCOST" property="itemcost" jdbcType="VARCHAR" />
    <association property="order" column="ORDERID" 
	select="com.test.mybatis.mapper.MybatisOrderMapper.getOrderByID"/> 
  </resultMap>
  <select id="selectItemByID" resultMap="AssociationMap" parameterType="java.math.BigDecimal" >
    select *from MYBATISORDERITEMwhere ITEMID = #{itemid,jdbcType=DECIMAL}
  </select>
MybatiscustomerMapper.xml:因为不和别的表做关联,只有简单的id,result配置.
  <resultMap id="BaseResultMap" type="com.test.mybatis.vo.Mybatiscustomer" >
    <id column="ID" property="id" jdbcType="DECIMAL" />
    <result column="NAME" property="name" jdbcType="VARCHAR" />
  </resultMap>
  <select id="getCustomerByID" resultMap="BaseResultMap" parameterType="java.math.BigDecimal" >
    select *from MYBATISCUSTOMER where ID = #{id,jdbcType=DECIMAL}
  </select>

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

  <resultMap id="AssociationResultMap" type="com.test.mybatis.vo.MybatisOrder" >
    <id column="ORDERID" property="orderid" jdbcType="DECIMAL" />
    <result column="ORDERTYPE" property="ordertype" jdbcType="VARCHAR" />
    <result column="ORDERDATE" property="orderdate" jdbcType="DATE" />
	
	<association property="customer" column="CUSTOMERID" 
		resultMap="com.test.mybatis.mapper.MybatiscustomerMapper.BaseResultMap"/> 
	<collection property="itemList" column="ORDERID" javaType="ArrayList" 
		ofType="com.test.mybatis.vo.MybatisOrderItem" 
		resultMap="com.test.mybatis.mapper.MybatisOrderItemMapper.BaseResultMap"/>
  </resultMap>
  <select id="getOrderAssociation" parameterType="String" resultMap="AssociationResultMap">  
		SELECT *  
		  FROM mybatisOrder ord LEFT JOIN mybatiscustomer customer ON ord.customerId = customer.ID 
		  LEFT JOIN mybatisOrderItem item ON ord.orderid = item.orderid 
		 WHERE ord.orderid = #{id}
   </select> 

  • 12
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值