MyBatis关联关系

别等时光非礼了梦想. 提交于 2019-12-04 04:25:52

1. 一对多关联关系

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

实体类配置

OrderVo

package com.lin.model.vo;

import com.lin.model.Order;
import com.lin.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.lin.model.vo;

import com.lin.model.Order;
import com.lin.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="OrderVo" type="com.lin.ssm.model.vo.OrderVo" >
    <id column="order_id" property="orderId"/>
    <result column="order_no" property="orderNo"/>
    <!-- 一对多的关系 -->
    <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
    <collection property="orderItems" ofType="com.lin.ssm.model.OrderItem">
        <id column="order_item_id" property="orderItemId"></id>
        <result column="product_id" property="productId"/>
        <result column="customer_name" property="customerName"/>
        <result column="quantity" property="quantity"/>
        <result column="oid" property="oid"/>
    </collection>
  </resultMap>
  <!-- 一对多查询语句-->
  <select id="queryOrderVoByOrderId" resultMap="OrderVo" parameterType="java.lang.Integer">
         select * from `order` o,order_item oi where o.order_id = oi.oid and o.order_id = #{orderId}
  </select>

OrderItemMapper.xml

  <!--配置多对一关联关系映射-->
  <resultMap id="OrderItemOv" type="com.lin.ssm.model.vo.OrderItemVo" >
    <id column="order_item_id" property="orderItemId"></id>
    <result column="product_id" property="productId"/>
    <result column="customer_name" property="customerName"/>
    <result column="quantity" property="quantity"/>
    <result column="oid" property="oid"/>
    <!-- 一对多的关系 -->
    <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
    <association  property="order" javaType="com.lin.ssm.model.Order">
        <id column="order_id" property="orderId"/>
        <result column="order_no" property="orderNo"/>
    </association >
  </resultMap>
  
  <select id="queryOrderItemVoByOrderItemId" resultMap="OrderItemOv" parameterType="java.lang.Integer">
        select * from `order` o, order_item oi where o.order_id = oi.oid and oi.order_item_id = #{orderItemId}
  </select>

引用sql语句到java类

OrderMapper.java

package com.lin.ssm.mapper;

import com.lin.ssm.model.Order;
import com.lin.ssm.model.vo.OrderVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
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.java

package com.lin.ssm.mapper;

import com.lin.ssm.model.OrderItem;
import com.lin.ssm.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
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);
}

service测试类

OneToManyService

package com.lin.service;

import com.lin.model.vo.OrderItemVo;
import com.lin.model.vo.OrderVo;

public interface OneToManyService {

    OrderVo selectByOid(Integer oid);
    OrderItemVo selectByOrderItemId(Integer orderItemId);

}

OneToManyServiceImpl

package com.lin.service.impl;

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


@Service
public class OneToManyServiceImpl implements OneToManyService {
    @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.selectByIrderItemId(orderItemId);
    }
}

测试结果代码

OneToManyServiceImplTest

package com.lin.service;

import com.lin.SpringBaseTest;
import com.lin.model.OrderItem;
import com.lin.model.vo.OrderVo;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;


public class OneToManyServiceImplTest extends SpringBaseTest {
    @Autowired
    private  OneToManyService oneToManyService;

    @Test
    public void selectByOid() {
        OrderVo orderVo = oneToManyService.selectByOid(1);
        System.out.println(orderVo);
        for (OrderItem item : orderVo.getOrderItems()) {
            System.out.println(item);
        }
    }

}

在这里插入图片描述

2. 多对多关联关系

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

实体类配置

**HbookVo **

package com.lin.model.vo;

import com.lin.model.Category;
import com.lin.model.Hbook;

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

public class HbookVo extends Hbook {

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

    public List getCategorys() {
        return categorys;
    }

    public void setCategorys(List categorys) {
        this.categorys = categorys;
    }

}

**CategoryVo **

package com.lin.model.vo;

import com.lin.model.Category;
import com.lin.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;
    }
}

配置多对多关联关系映射

  <resultMap id="bookVoMap" type="com.lin.ssm.model.vo.BookVo">
    <result property="bookId" column="book_id"></result>
    <result property="bookName" column="book_name"></result>
    <result property="price" column="price"></result>
    <collection property="categories" ofType="com.lin.ssm.model.Category">
      <result property="categoryId" column="category_id"></result>
      <result property="categoryName" column="category_name"></result>
    </collection>
  </resultMap>

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


  <!--多对多-->
  <select id="queryByBookId" resultMap="bookVoMap" 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>

引用sql语句到java类

HbookCategoryMapper.java

package com.lin.ssm.mapper;

import com.lin.ssm.model.BookCategory;
import com.lin.ssm.model.vo.BookVo;
import com.lin.ssm.model.vo.CategoryVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface HbookCategoryMapper{
    int deleteByPrimaryKey(Integer bcid);

    int insert(BookCategory record);

    int insertSelective(BookCategory record);

    BookCategory selectByPrimaryKey(Integer bcid);

    int updateByPrimaryKeySelective(BookCategory record);

    int updateByPrimaryKey(BookCategory record);


    BookVo queryByBookId(@Param("bookId") Integer bookId);

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

}

service测试类

HbookCategoryService


package com.lin.service;

import com.lin.model.vo.CategoryVo;
import com.lin.model.vo.HbookVo;


public interface HbookCategoryService {

    HbookVo queryByBid(Integer bid);

    CategoryVo queryByCid( Integer cid);
}

HbookCategoryServiceImpl

package com.lin.service.impl;

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

@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.lin.service.impl;

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

public class HbookCategoryServiceImplTest extends SpringBaseTest {
    @Autowired
    private HbookCategoryService hbookCategoryService;

    @Test
    public void queryByBid() {
        HbookVo hbookVo = hbookCategoryService.queryByBid(1);
        System.out.println(hbookVo);
        for (Object category : hbookVo.getCategorys()) {
            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);
        }
    }
}

在这里插入图片描述

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