mybatis关联关系

Posted liuwenwu9527

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis关联关系相关的知识,希望对你有一定的参考价值。

mybatis关联关系

一对多&多对一

这是就那订单和订单项来做演示,一个订单对应多个订单项。一个订单项对应一个订单。

订单表 t_hibernate_order

技术图片

订单项表 t_hibernate_order_item

技术图片

首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper 如何生成,可以参考之前的,所以不会过多去讲解
实体类 OrderVo
package com.liuwenwu.model.vo;

import com.liuwenwu.model.Order;
import com.liuwenwu.model.OrderItem;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LWW
 * @site www.lww.com
 * @company
 * @create 2019-09-26 11:27
 */
public class OrderVo extends Order 
    private List<OrderItem> orderItems = new ArrayList<>();

    public List<OrderItem> getOrderItems() 
        return orderItems;
    

    public void setOrderItems(List<OrderItem> orderItems) 
        this.orderItems = orderItems;
    

 

实体类  OrderItemVo

package com.liuwenwu.model.vo;

import com.liuwenwu.model.Order;
import com.liuwenwu.model.OrderItem;

/**
 * @author LWW
 * @site www.lww.com
 * @company
 * @create 2019-09-26 11:30
 */
public class OrderItemVo extends OrderItem 
    private Order order;

    public Order getOrder() 
        return order;
    

    public void setOrder(Order order) 
        this.order = order;
    
OrderMapper.xml(一对多)
 <resultMap id="OrderVoMap" type="com.liuwenwu.model.vo.OrderVo" >
    <result property="orderId" column="order_id"></result>
    <result property="orderNo" column="order_no"></result>
    <collection property="orderItems" ofType="com.liuwenwu.model.OrderItem">
      <result property="orderItemId" column="order_item_id"></result>
      <result property="productId" column="product_id"></result>
      <result property="quantity" column="quantity"></result>
      <result property="oid" column="oid"></result>
    </collection>
  </resultMap>

  <select id="selectByOid" resultMap="OrderVoMap" parameterType="java.lang.Integer">
    select * from t_hibernate_order o,t_hibernate_order_item oi
    where o.order_id = oi.oid
    and o.order_id = #oid
  </select>

OrderMapper.java

package com.liuwenwu.mapper;

import com.liuwenwu.model.Order;
import com.liuwenwu.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface OrderMapper 
    int deleteByPrimaryKey(Integer orderId);

    int insert(Order record);

    int insertSelective(Order record);

    Order selectByPrimaryKey(Integer orderId);

    int updateByPrimaryKeySelective(Order record);

    int updateByPrimaryKey(Order record);

    //一对多
    OrderVo selectByOid(@Param("oid") Integer oid);

OrderItemMapper.xml (多对一)

 <resultMap id="OrderItemVoMap" type="com.liuwenwu.model.vo.OrderItemVo" >
    <result property="orderItemId" column="order_item_id"></result>
    <result property="productId" column="product_id"></result>
    <result property="quantity" column="quantity"></result>
    <result property="oid" column="oid"></result>
    <association property="order" javaType="com.liuwenwu.model.Order">
      <result property="orderId" column="order_id"></result>
      <result property="orderNo" column="order_no"></result>
    </association>
  </resultMap>

  <select id="selectByOrderItemId" resultMap="OrderItemVoMap" parameterType="java.lang.Integer">
    select * from t_hibernate_order o,t_hibernate_order_item oi
    where o.order_id = oi.oid
    and oi.order_item_id = #orderItemId
  </select>

OrderItemMapper.java

package com.liuwenwu.mapper;

import com.liuwenwu.model.OrderItem;
import com.liuwenwu.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface OrderItemMapper 
    int deleteByPrimaryKey(Integer orderItemId);

    int insert(OrderItem record);

    int insertSelective(OrderItem record);

    OrderItem selectByPrimaryKey(Integer orderItemId);

    int updateByPrimaryKeySelective(OrderItem record);

    int updateByPrimaryKey(OrderItem record);

    OrderItemVo selectByOrderItemId(@Param("orderItemId") Integer orderItemId);

One2ManyService
package com.liuwenwu.service;

import com.liuwenwu.model.vo.OrderItemVo;
import com.liuwenwu.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;

/**
 * @author LWW
 * @site www.lww.com
 * @company
 * @create 2019-09-26 18:22
 */

public interface One2ManyService 

    OrderVo selectByOid(Integer oid);

    OrderItemVo selectByOrderItemId(Integer orderItemId);

One2ManyServiceImpl
package com.liuwenwu.service.impl;

import com.liuwenwu.mapper.OrderItemMapper;
import com.liuwenwu.mapper.OrderMapper;
import com.liuwenwu.model.vo.OrderItemVo;
import com.liuwenwu.model.vo.OrderVo;
import com.liuwenwu.service.One2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author LWW
 * @site www.lww.com
 * @company
 * @create 2019-09-26 18:24
 */
@Service
public class One2ManyServiceImpl implements One2ManyService 

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public OrderVo selectByOid(Integer oid) 
        return orderMapper.selectByOid(oid);
    

    @Override
    public OrderItemVo selectByOrderItemId(Integer orderItemId) 
        return orderItemMapper.selectByOrderItemId(orderItemId);
    

测试  One2ManyServiceImplTest

package com.liuwenwu.service.impl;

import com.liuwenwu.SpringBaseTest;
import com.liuwenwu.model.OrderItem;
import com.liuwenwu.model.vo.OrderItemVo;
import com.liuwenwu.model.vo.OrderVo;
import com.liuwenwu.service.One2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

/**
 * @author LWW
 * @site www.lww.com
 * @company
 * @create 2019-09-26 18:31
 */
public class One2ManyServiceImplTest extends SpringBaseTest 

    @Autowired
    private One2ManyService one2ManyService;

    @Test
    public void selectByOid() 
        OrderVo orderVo = one2ManyService.selectByOid(1);
        System.out.println(orderVo);
        for (OrderItem orderItem : orderVo.getOrderItems()) 
            System.out.println(orderItem);
        

    

    @Test
    public void selectByOrderItemId() 
        OrderItemVo orderItemVo = one2ManyService.selectByOrderItemId(1);
        System.out.println(orderItemVo);
        System.out.println(orderItemVo.getOrder());

    

结果:一个订单对应多个订单项

技术图片

一个订单项对应一个订单

技术图片


多对多

多对多这里就使用书籍和数据类别来举个例子,一本书有多个类别,一个类别对应多本书。中间通过关联表进行关联。

书籍表t_hibernate_book

技术图片

 

书籍类别表 t_hibernate_category
技术图片


中间表 t_hibernate_book_category

技术图片

首先用逆向生成工具生成上面表对应的代码

创建对应的Vo类,这里只创建数据和类被。中间表就没必要:
HbookVo

package com.liuwenwu.model.vo;

import com.liuwenwu.model.Category;
import com.liuwenwu.model.Hbook;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LWW
 * @site www.lww.com
 * @company
 * @create 2019-09-26 19:02
 */
public class HbookVo extends Hbook 

    private List<Category> category =new ArrayList<>();

    public List<Category> getCategory() 
        return category;
    

    public void setCategory(List<Category> category) 
        this.category = category;
    
CategoryVo
package com.liuwenwu.model.vo;

import com.liuwenwu.model.Category;
import com.liuwenwu.model.Hbook;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LWW
 * @site www.lww.com
 * @company
 * @create 2019-09-26 19:04
 */
public class CategoryVo extends Category 
    private List<Hbook> hbooks =new ArrayList<>();

    public List<Hbook> getHbooks() 
        return hbooks;
    

    public void setHbooks(List<Hbook> hbooks) 
        this.hbooks = hbooks;
    

HbookCategoryMapper.xml

 <resultMap id="HbookVoMap" type="com.liuwenwu.model.vo.HbookVo" >
    <result property="bookId" column="book_id"></result>
    <result property="bookName" column="book_name"></result>
    <result property="price" column="price"></result>
    <collection property="category" ofType="com.liuwenwu.model.Category">
      <result property="categoryId" column="category_id"></result>
      <result property="categoryName" column="category_name"></result>
    </collection>
  </resultMap>

  <resultMap id="CategoryVoMap" type="com.liuwenwu.model.vo.CategoryVo" >
    <result property="categoryId" column="category_id"></result>
    <result property="categoryName" column="category_name"></result>
    <collection property="hbooks" ofType="com.liuwenwu.model.Hbook">
      <result property="bookId" column="book_id"></result>
      <result property="bookName" column="book_name"></result>
      <result property="price" column="price"></result>
    </collection>
  </resultMap>

  <select id="queryByBid" resultMap="HbookVoMap" parameterType="java.lang.Integer">
    select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
     where b.book_id = bc.bid and bc.cid = c.category_id and b.book_id = #bid

  </select>
  <select id="queryByCid" resultMap="CategoryVoMap" parameterType="java.lang.Integer">
    select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
     where b.book_id = bc.bid and bc.cid = c.category_id and c.category_id=#cid
  </select>
HbookCategoryMapper
package com.liuwenwu.mapper;

import com.liuwenwu.model.HbookCategory;
import com.liuwenwu.model.vo.CategoryVo;
import com.liuwenwu.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface HbookCategoryMapper 
    int deleteByPrimaryKey(Integer bcid);

    int insert(HbookCategory record);

    int insertSelective(HbookCategory record);

    HbookCategory selectByPrimaryKey(Integer bcid);

    int updateByPrimaryKeySelective(HbookCategory record);

    int updateByPrimaryKey(HbookCategory record);

    HbookVo queryByBid(@Param("bid") Integer bid);

    CategoryVo queryByCid(@Param("cid") Integer cid);
HbookCategoryService
package com.liuwenwu.service;

import com.liuwenwu.model.vo.CategoryVo;
import com.liuwenwu.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;

/**
 * @author LWW
 * @site www.lww.com
 * @company
 * @create 2019-09-26 19:26
 */

public interface HbookCategoryService 

    HbookVo queryByBid(Integer bid);

    CategoryVo queryByCid(Integer cid);
HbookCategoryServiceImpl
package com.liuwenwu.service.impl;

import com.liuwenwu.mapper.HbookCategoryMapper;
import com.liuwenwu.model.vo.CategoryVo;
import com.liuwenwu.model.vo.HbookVo;
import com.liuwenwu.service.HbookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author LWW
 * @site www.lww.com
 * @company
 * @create 2019-09-26 19:27
 */
@Service
public class HbookCategoryServiceImpl implements HbookCategoryService 

    @Autowired
    private HbookCategoryMapper hbookCategoryMapper;

    @Override
    public HbookVo queryByBid(Integer bid) 
        return hbookCategoryMapper.queryByBid(bid);
    

    @Override
    public CategoryVo queryByCid(Integer cid) 
        return hbookCategoryMapper.queryByCid(cid);
    
测试:HbookCategoryServiceImplTest
package com.liuwenwu.service.impl;

import com.liuwenwu.SpringBaseTest;
import com.liuwenwu.model.Category;
import com.liuwenwu.model.Hbook;
import com.liuwenwu.model.vo.CategoryVo;
import com.liuwenwu.model.vo.HbookVo;
import com.liuwenwu.service.HbookCategoryService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

/**
 * @author LWW
 * @site www.lww.com
 * @company
 * @create 2019-09-26 19:29
 */
public class HbookCategoryServiceImplTest extends SpringBaseTest 

    @Autowired
    private HbookCategoryService hbookCategoryService;

    @Test
    public void queryByBid() 
        HbookVo hbookVo = hbookCategoryService.queryByBid(1);
        System.out.println(hbookVo);
        for (Category category : hbookVo.getCategory()) 
            System.out.println(category);
        

    

    @Test
    public void queryByCid() 
        CategoryVo categoryVo = hbookCategoryService.queryByCid(1);
        System.out.println(categoryVo);
        for (Hbook hbook : categoryVo.getHbooks()) 
            System.out.println(hbook);
        
    

结果 :一本书对应多个书籍类别

技术图片

一个书籍类别对应多本书

技术图片

 

以上是关于mybatis关联关系的主要内容,如果未能解决你的问题,请参考以下文章

mybatis学习笔记三(关联关系)

MyBatis学习笔记 关联关系

mybatis xml数据层框架应用--Mybatis关系映射之一对多关系映射

Mybatis之关联关系(一对多多对多)

MyBatis的关联映射

mybatis xml数据层框架应用--Mybatis关系映射之一对一关系映射