一对多 & 多对一
这是就那订单和订单项来做演示,一个订单对应多个订单项。一个订单项对应一个订单。
。
订单表:
订单项表:
首先先用逆向生成工具生成order
、order_item
这两张表对应的model与mapper。这里为了节省版面就一笔带过了。我们看主要的就好。
首先给 Order
和 OrderItem
这个两个实体类建立对应的 OrderVo
和 OrderItem
OrderVo
package com.cpc.ssm.model.vo;
import com.cpc.ssm.model.Order;
import com.cpc.ssm.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
/**
* @author cpc
* @site www.cpc.com
* @company xxx公司
* @create 2019-09-26 10:49
*/
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.cpc.ssm.model.vo;
import com.cpc.ssm.model.Order;
import com.cpc.ssm.model.OrderItem;
/**
* @author cpc
* @site www.cpc.com
* @company xxx公司
* @create 2019-09-26 10:49
*/
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.cpc.ssm.model.vo.OrderVo" >
<id column="order_id" property="orderId"/>
<result column="order_no" property="orderNo"/>
<!-- 一对多的关系 -->
<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<collection property="orderItems" ofType="com.cpc.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>
OrderMapper.java
package com.cpc.ssm.mapper;
import com.cpc.ssm.model.Order;
import com.cpc.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.xml
<!--配置多对一关联关系映射-->
<resultMap id="OrderItemOv" type="com.cpc.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.cpc.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>
OrderItemMapper
package com.cpc.ssm.mapper;
import com.cpc.ssm.model.OrderItem;
import com.cpc.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);
}
One2ManyService.java
package com.cpc.ssm.service;
import com.cpc.ssm.model.vo.OrderItemVo;
import com.cpc.ssm.model.vo.OrderVo;
/**
* @author cpc
* @site www.cpc.com
* @company xxx公司
* @create 2019-09-26 11:35
*/
public interface One2ManyService {
OrderVo queryOrderVoByOrderId(Integer orderId);
OrderItemVo queryOrderItemVoByOrderItemId(Integer OrderItemId);
}
One2ManyServiceImpl
package com.cpc.ssm.service.impl;
import com.cpc.ssm.mapper.OrderItemMapper;
import com.cpc.ssm.mapper.OrderMapper;
import com.cpc.ssm.model.vo.OrderItemVo;
import com.cpc.ssm.model.vo.OrderVo;
import com.cpc.ssm.service.One2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author cpc
* @site www.cpc.com
* @company xxx公司
* @create 2019-09-26 11:36
*/
@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);
}
}
BaseTestCase
package com.cpc.ssm;
import com.cpc.ssm.model.User;
import com.cpc.ssm.util.PageBean;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author cpc
* @site www.cpc.com
* @company xxx公司
* @create 2019-09-24 11:07
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class BaseTestCase {
protected User user;
protected PageBean pageBean;
@Before
public void init(){
user = new User();
pageBean = new PageBean();
}
}
One2ManyServiceImplTest
测试:
package com.cpc.ssm;
import com.cpc.ssm.model.OrderItem;
import com.cpc.ssm.model.vo.OrderItemVo;
import com.cpc.ssm.model.vo.OrderVo;
import com.cpc.ssm.service.One2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author cpc
* @site www.cpc.com
* @company xxx公司
* @create 2019-09-26 11:38
*/
public class One2ManyServiceImplTest extends BaseTestCase {
@Autowired
private One2ManyService one2ManyService;
/**
* 测试一对多
*/
@Test
public void queryOrderVoByOrderId() {
OrderVo orderVo = this.one2ManyService.queryOrderVoByOrderId(1);
System.out.println("我是订单:" + orderVo);
for (OrderItem orderItem : orderVo.getOrderItems()) {
System.out.println("---------我是订单项:"+orderItem);
}
}
/**
* 测试多对一定
*/
@Test
public void queryOrderItemVoByOrderItemId() {
OrderItemVo orderItemVo = this.one2ManyService.queryOrderItemVoByOrderItemId(1);
System.out.println("我是订单项:"+orderItemVo);
System.out.println("我是订单项对应的订单:"+orderItemVo.getOrder());
}
}
运行结果:
多对多
多对多这里就使用书籍和数据类别来举个例子,一本书有多个类别,一个类别对应多本书。中间通过关联表进行关联。
book 书籍表
category 类别表
book_category 数据类被关联表:
首先用逆向生成工具生成上面表对应的代码
创建对应的Vo类,这里只创建数据和类被。中间表就没必要:
BookVo
package com.cpc.ssm.model.vo;
import com.cpc.ssm.model.Book;
import com.cpc.ssm.model.Category;
import java.util.ArrayList;
import java.util.List;
/**
* @author cpc
* @site www.cpc.com
* @company xxx公司
* @create 2019-09-26 11:53
*/
public class BookVo extends Book {
private List<Category> categories = new ArrayList<>();
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
}
CategoryVo
package com.cpc.ssm.model.vo;
import com.cpc.ssm.model.Book;
import com.cpc.ssm.model.Category;
import java.util.ArrayList;
import java.util.List;
/**
* @author cpc
* @site www.cpc.com
* @company xxx公司
* @create 2019-09-26 11:54
*/
public class CategoryVo extends Category{
private List<Book> books = new ArrayList<>();
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}
BookCategoryMapper.xml
<resultMap id="bookVoMap" type="com.cpc.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.cpc.ssm.model.Category">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
</collection>
</resultMap>
<resultMap id="CategoryVoMap" type="com.cpc.ssm.model.vo.CategoryVo">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
<collection property="books" ofType="com.cpc.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>
BookCategoryMapper
package com.cpc.ssm.mapper;
import com.cpc.ssm.model.BookCategory;
import com.cpc.ssm.model.vo.BookVo;
import com.cpc.ssm.model.vo.CategoryVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface BookCategoryMapper {
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);
}
Many2ManyService
package com.cpc.ssm.service;
import com.cpc.ssm.model.vo.BookVo;
import com.cpc.ssm.model.vo.CategoryVo;
/**
* @author cpc
* @site www.cpc.com
* @company xxx公司
* @create 2019-09-26 12:01
*/
public interface Many2ManyService {
BookVo queryByBookId(Integer bookId);
CategoryVo queryByCid(Integer cid);
}
Many2ManyServiceImpl
package com.cpc.ssm.service.impl;
import com.cpc.ssm.mapper.BookCategoryMapper;
import com.cpc.ssm.model.vo.BookVo;
import com.cpc.ssm.model.vo.CategoryVo;
import com.cpc.ssm.service.Many2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author cpc
* @site www.cpc.com
* @company xxx公司
* @create 2019-09-26 12:01
*/
@Service
public class Many2ManyServiceImpl implements Many2ManyService {
@Autowired
private BookCategoryMapper bookCategoryMapper;
@Override
public BookVo queryByBookId(Integer bookId) {
return bookCategoryMapper.queryByBookId(bookId);
}
@Override
public CategoryVo queryByCid(Integer cid) {
return bookCategoryMapper.queryByCid(cid);
}
}
Many2ManyServiceImplTest
package com.cpc.ssm;
import com.cpc.ssm.model.Book;
import com.cpc.ssm.model.Category;
import com.cpc.ssm.model.vo.BookVo;
import com.cpc.ssm.model.vo.CategoryVo;
import com.cpc.ssm.service.Many2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author cpc
* @site www.cpc.com
* @company xxx公司
* @create 2019-09-26 12:02
*/
public class Many2ManyServiceImplTest extends BaseTestCase {
@Autowired
private Many2ManyService many2ManyService;
@Test
public void queryByBookId() {
BookVo bookVo = many2ManyService.queryByBookId(1);
System.out.println("查询出的书籍:"+bookVo);
for (Category category : bookVo.getCategories()) {
System.out.println("----------- 此书籍对应的类别:"+category);
}
}
@Test
public void queryByCid() {
CategoryVo categoryVo = many2ManyService.queryByCid(1);
System.out.println("查询出的类别:"+categoryVo);
for (Book book : categoryVo.getBooks()) {
System.out.println("----------- 此类别对应的书籍:" + book);
}
}
}
测试结果: