欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mybatis关联关系映射

程序员文章站 2024-03-04 08:10:05
...

目标
1、一对多关联关系
2、多对多关联关系

一对多

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

OrderVo

package com.cgl.ssm.model.vo;

import com.cgl.ssm.model.Order;
import com.cgl.ssm.model.OrderItem;

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

/**
 * @authorlyl
 * @site
 * @company
 * @create  2019-10-23 11:06
 */
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.cgl.ssm.model.vo;

import com.cgl.ssm.model.Order;
import com.cgl.ssm.model.OrderItem;

/**
 * @authorlyl
 * @site
 * @company
 * @create  2019-10-23 11:07
 */
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.cgl.ssm.model.vo.OrderVo">
    <result property="orderId" column="order_id"></result>
    <result property="orderNo" column="order_no"></result>
    <!--<result property="orderItems"></result>-->
    <collection property="orderItems" ofType="com.cgl.ssm.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="selectByOrderId" 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.oid = #{orderId}
  </select>

OrderMapper.java

package com.cgl.ssm.mapper;

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

import java.util.List;

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

    List<OrderVo> selectByOrderId(@Param("orderId") Integer orderId);
}

OrderItemMapper.xml

 <resultMap id="OrderItemVoMap" type="com.cgl.ssm.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>
    <!--<result property="orderItems"></result>-->
    <association property="order" javaType="com.cgl.ssm.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.cgl.ssm.mapper;

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

import java.util.List;

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

    List<OrderItemVo> selectByOrderItemId(@Param("orderItemId") Integer orderItemId);
}

OneToManyService

package com.cgl.ssm.service;

import com.cgl.ssm.model.vo.OrderItemVo;
import com.cgl.ssm.model.vo.OrderVo;

import java.util.List;

/**
 * @author caoguangli
 * @site www.xiaomage.com
 * @company
 * @create  2019-10-23 19:26
 */
public interface OneToManyService {
    List<OrderVo> selectByOrderId(Integer orderId);
    List<OrderItemVo> selectByOrderItemId(Integer orderItemId);

}

OneToManyServiceImpl

package com.cgl.ssm.service.impl;

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

import java.util.List;

/**
 * @author caoguangli
 * @site www.xiaomage.com
 * @company
 * @create  2019-10-23 19:26
 */

@Service
public class OneToManyServiceImpl implements OneToManyService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Override
    public List<OrderVo> selectByOrderId(Integer orderId) {
        return orderMapper.selectByOrderId(orderId);
    }

    @Override
    public List<OrderItemVo> selectByOrderItemId(Integer orderItemId) {
        return orderItemMapper.selectByOrderItemId(orderItemId);
    }
}

One2ManyServiceImplTest

package com.cgl.ssm.service.impl;

import com.cgl.ssm.model.OrderItem;
import com.cgl.ssm.model.vo.OrderItemVo;
import com.cgl.ssm.model.vo.OrderVo;
import com.cgl.ssm.service.OneToManyService;
import com.cgl.ssm.test.SpringJunitBaseTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author caoguangli
 * @site www.xiaomage.com
 * @company
 * @create  2019-10-23 19:33
 */
public class OneToManyServiceImplTest extends SpringJunitBaseTest {

    @Autowired
    private OneToManyService oneToManyService;
    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void selectByOrderId() {
        List<OrderVo> orderVos = oneToManyService.selectByOrderId(8);
        OrderVo orderVo = orderVos.get(0);
        System.out.println(orderVo);
        for (OrderItem orderItem : orderVo.getOrderItems()) {
            System.out.println(orderItem);
        }
    }

    @Test
    public void selectByOrderItemId() {
        List<OrderItemVo> orderItemVos = oneToManyService.selectByOrderItemId(36);
        OrderItemVo orderItemVo = orderItemVos.get(0);
        System.out.println(orderItemVo);
        System.out.println(orderItemVo.getOrder());
    }
}

运行结果:
Mybatis关联关系映射

多对多

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

实体类

HbookVo

package com.cgl.ssm.model.vo;

import com.cgl.ssm.model.Category;
import com.cgl.ssm.model.Hbook;

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

/**
 * @author caoguangli
 * @site www.xiaomage.com
 * @company
 * @create  2019-10-23 19:42
 */
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.cgl.ssm.model.vo;

import com.cgl.ssm.model.Category;
import com.cgl.ssm.model.Hbook;

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

/**
 * @author caoguangli
 * @site www.xiaomage.com
 * @company
 * @create  2019-10-23 19:44
 */
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;
    }
}

HbookMapper.xml

<resultMap id="HbookVoMap" type="com.cgl.ssm.model.vo.HbookVo">
    <result property="bookId" column="book_id"></result>
    <result property="bookName" column="book_name"></result>
    <result property="price" column="price"></result>
    <!--<result property="orderItems"></result>-->
    <collection property="categories" ofType="com.cgl.ssm.model.Category">
      <result property="categoryId" column="category_id"></result>
      <result property="categoryName" column="category_name"></result>
    </collection>
  </resultMap>
  
 <select id="selectByBid" 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>

HbookMapper.java

package com.cgl.ssm.mapper;

import com.cgl.ssm.model.Hbook;
import com.cgl.ssm.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface HbookMapper {
    int deleteByPrimaryKey(Integer bookId);

    int insert(Hbook record);

    int insertSelective(Hbook record);

    Hbook selectByPrimaryKey(Integer bookId);

    int updateByPrimaryKeySelective(Hbook record);

    int updateByPrimaryKey(Hbook record);

    HbookVo selectByBid(@Param("bid") Integer bid);
}

CategoryVoMapper.xml

 <resultMap id="CategoryVoMap" type="com.cgl.ssm.model.vo.CategoryVo">
    <result property="categoryId" column="category_id"></result>
    <result property="categoryName" column="category_name"></result>
    <!--<result property="orderItems"></result>-->
    <collection property="hbooks" ofType="com.cgl.ssm.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="selectByCid" 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>

CategoryVoMapper.java

package com.cgl.ssm.mapper;

import com.cgl.ssm.model.Category;
import com.cgl.ssm.model.vo.CategoryVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface CategoryMapper {
    int deleteByPrimaryKey(Integer categoryId);

    int insert(Category record);

    int insertSelective(Category record);

    Category selectByPrimaryKey(Integer categoryId);

    int updateByPrimaryKeySelective(Category record);

    int updateByPrimaryKey(Category record);

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

ManyToManyService.java

package com.cgl.ssm.service;

import com.cgl.ssm.model.vo.CategoryVo;
import com.cgl.ssm.model.vo.HbookVo;

/**
 * @author caoguangli
 * @site www.xiaomage.com
 * @company
 * @create  2019-10-23 20:15
 */
public interface ManyToManyService {

    HbookVo selectByBid(Integer bid);
    CategoryVo selectByCid(Integer cid);
}

ManyToManyServiceImpl

package com.cgl.ssm.service.impl;

import com.cgl.ssm.mapper.CategoryMapper;
import com.cgl.ssm.mapper.HbookMapper;
import com.cgl.ssm.model.vo.CategoryVo;
import com.cgl.ssm.model.vo.HbookVo;
import com.cgl.ssm.service.ManyToManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author caoguangli
 * @site www.xiaomage.com
 * @company
 * @create  2019-10-23 20:16
 */

@Service
public class ManyToManyServiceImpl implements ManyToManyService {

    @Autowired
    private HbookMapper hbookMapper;

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public HbookVo selectByBid(Integer bid) {
        return hbookMapper.selectByBid(bid);
    }

    @Override
    public CategoryVo selectByCid(Integer cid) {
        return categoryMapper.selectByCid(cid);
    }
}

ManyToManyServiceImplTest

package com.cgl.ssm.service.impl;

import com.cgl.ssm.model.Category;
import com.cgl.ssm.model.Hbook;
import com.cgl.ssm.model.vo.CategoryVo;
import com.cgl.ssm.model.vo.HbookVo;
import com.cgl.ssm.service.ManyToManyService;
import com.cgl.ssm.test.SpringJunitBaseTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

/**
 * @author caoguangli
 * @site www.xiaomage.com
 * @company
 * @create  2019-10-23 20:18
 */
public class ManyToManyServiceImplTest extends SpringJunitBaseTest {

    @Autowired private ManyToManyService manyToManyService;
    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

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

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

    }
}

运行结果:
Mybatis关联关系映射