Mybatis 之 关联关系映射

帅比萌擦擦* 提交于 2019-12-04 04:21:55

Mybatis 之 关联关系映射

前言

1.将数据表导入数据库中

2.通过 mybatis-generator 插件生成dao、mapper、model

1)配置 mybatis-generator 插件生成文件位置

2)修改 generatorConfig.xml 配置文件的生成目录(mapper和model)及对应生成关系

3.修改 Customer、Order 实体类

1)实现序列化接口

2)建立实体映射关联关系(一对多、多对一)

   #一对多:一个客户对应多个订单
   private List<Order> orders=new ArrayList<Order>();

   #多对一:多个订单对应一个客户(一个订单对应一个客户)
   private Customer customer;

4.配置 mybatis 关联映射

4.1 一对多

	   <resultMap id="one2many" type="Customer">
	        <id column="customer_id" property="customerId"/>
	        <result column="customer_name" property="customerName"/>
	        <!-- 一对多的关系 -->
	        <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
	        <collection property="orders" ofType="Order">
	            <id column="order_id" property="orderId"/>
	            <result column="order_no" property="orderNo"/>
	        </collection>
	    </resultMap>

注意:使用左外连接而非内连接!!!

4.2 多对一

	   <resultMap id="many2one" type="Order">
	        <id column="order_id" property="orderId"/>
	        <result column="order_no" property="orderNo"/>
	        <result column="cid" property="cid"/>
	
	        <!-- 多对一的关系 -->
	        <!-- property: 指的是属性的值, javaType:指的是属性的类型-->
	        <association property="customer" javaType="Customer">
	            <id column="customer_id" property="customerId"/>
	            <result column="customer_name" property="customerName"/>
	        </association>
	   </resultMap>

一对多

用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper

OrderVo

package com.dj.model.vo;

import com.dj.model.Order;
import com.dj.model.OrderItem;

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

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.dj.model.vo;

import com.dj.model.Order;
import com.dj.model.OrderItem;

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.dj.model.vo.OrderVo" >
  <result property="orderId" column="order_id"></result>
  <result property="orderNo" column="order_no"></result>
  <collection property="orderItems" ofType="com.dj.model.OrderItem">
    <result property="orderItemId" column="order_item_id"></result>
    <result property="oid" column="oid"></result>
    <result property="productId" column="product_id"></result>
    <result property="quantity" column="quantity"></result>
  </collection>
</resultMap>

<select id="queryOrderVoByOrderId" 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 = #{orderId}
</select>

OrderMapper

package com.dj.mapper;

import com.dj.model.Order;
import com.dj.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 queryOrderVoByOrderId(@Param("orderId") Integer orderId);
}

OrderItemMapper.xml

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

<select id="queryOrderItemVoByOrderItemId" resultMap="OrderVoMap" 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

package com.dj.mapper;

import com.dj.model.OrderItem;
import com.dj.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 queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
}

One2ManyService

package com.dj.service;

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

import java.util.List;

public interface One2ManyService {
    OrderVo queryOrderVoByOrderId(Integer orderId);
    OrderItemVo queryOrderItemVoByOrderItemId(Integer OrderItemId);
}

One2ManyServiceImpl

package com.dj.service.impl;

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


@Service
public class One2ManyServiceImpl implements One2ManyService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public OrderVo queryOrderVoByOrderId(Integer orderId) {
        return orderMapper.queryOrderVoByOrderId(orderId);
    }

    @Override
    public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
        return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
    }
}

One2ManyServiceImplTest

package com.dj.service.impl;

import com.dj.SpringJunitBaseTest;
import com.dj.model.Order;
import com.dj.model.OrderItem;
import com.dj.model.vo.OrderItemVo;
import com.dj.model.vo.OrderVo;
import com.dj.service.One2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

public class One2ManyServiceImplTest extends SpringJunitBaseTest {
    @Autowired
    private One2ManyService one2ManyService;

    @Test
    public void queryOrderVoByOrderId() {
        OrderVo orderVo = this.one2ManyService.queryOrderVoByOrderId(10);
        System.out.println(orderVo);
        for (OrderItem orderItem : orderVo.getOrderItems()) {
            System.out.println(orderItem);
        }
    }

    @Test
    public void queryOrderItemVoByOrderItemId() {
        OrderItemVo orderItemVo = this.one2ManyService.queryOrderItemVoByOrderItemId(49);
        System.out.println(orderItemVo);
        System.out.println(orderItemVo.getOrder());
    }
}


多对多

首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,这两张表对应的model与mapper

HbookVo

package com.dj.model.vo;

import com.dj.model.Category;
import com.dj.model.Hbook;

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

	
public class HbookVo extends Hbook {
    private List<Category> categories = new ArrayList<>();

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }
}

CategoryVo

package com.dj.model.vo;

import com.dj.model.Category;
import com.dj.model.Hbook;

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

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.dj.model.vo.HbookVo">
    <result property="bookId" column="book_id"></result>
    <result property="bookName" column="book_name"></result>
    <collection property="categories" ofType="com.dj.model.Category">
        <result property="categoryId" column="category_id"></result>
        <result property="categoryName" column="category_name"></result>
    </collection>
</resultMap>

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

  <select id="queryByBookId" 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 = #{bookId}
 </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.dj.mapper;

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

import java.util.List;

@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 queryByBookId(@Param("bookId") Integer bookId);

    CategoryVo queryByCid(@Param("cid") Integer cid);
}

Many2ManyService

package com.dj.service;

import com.dj.model.Category;
import com.dj.model.Hbook;
import com.dj.model.vo.CategoryVo;
import com.dj.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;


public interface Many2ManyService {
    HbookVo queryByBookId(Integer bookId);

    CategoryVo queryByCid(Integer cid);
}

Many2ManyServiceImpl

package com.dj.service.impl;

import com.dj.mapper.HbookCategoryMapper;
import com.dj.model.Category;
import com.dj.model.Hbook;
import com.dj.model.vo.CategoryVo;
import com.dj.model.vo.HbookVo;
import com.dj.service.Many2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Many2ManyServiceImpl implements Many2ManyService {
    @Autowired
    private HbookCategoryMapper hbookCategoryMapper;
    @Override
    public HbookVo queryByBookId(Integer bookId) {
        return hbookCategoryMapper.queryByBookId(bookId);
    }

    @Override
    public CategoryVo queryByCid(Integer cid) {
        return hbookCategoryMapper.queryByCid(cid);
    }
}

Many2ManyServiceImplTest

package com.dj.service.impl;

import com.dj.SpringJunitBaseTest;
import com.dj.model.Category;
import com.dj.model.Hbook;
import com.dj.model.vo.CategoryVo;
import com.dj.model.vo.HbookVo;
import com.dj.service.Many2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

public class Many2ManyServiceImplTest extends SpringJunitBaseTest {
    @Autowired
    private Many2ManyService many2ManyService;

    @Test
    public void queryByBookId() {
        HbookVo hbookVo = many2ManyService.queryByBookId(8);
        System.out.println(hbookVo);
        for (Category category : hbookVo.getCategories()) {
            System.out.println(category);
        }
    }

    @Test
    public void queryByCid() {
        CategoryVo categoryVo = many2ManyService.queryByCid(8);
        System.out.println(categoryVo);
        for (Hbook hbook : categoryVo.getHbooks()) {
            System.out.println(hbook);
        }

    }

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!