目标
1、一对多关联关系
2、多对多关联关系
1、一对多关联关系
首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper
<table schema="" tableName="t_hibernate_order" domainObjectName="Order"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
<table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
<table schema="" tableName="t_hibernate_book" domainObjectName="Hbook"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
<table schema="" tableName="t_hibernate_book_category" domainObjectName="HbookCategory"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
<table schema="" tableName="t_hibernate_category" domainObjectName="Category"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
创建OrderVo类继承原有的实体类
package com.chenkang.model.vo;
import com.chenkang.model.Order;
import com.chenkang.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
/**
* @author陈康
* @site www.chenkang.com
* @company xxx公司
* @create 2019-10-23 18:41
*/
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.chenkang.model.vo;
import com.chenkang.model.Order;
import com.chenkang.model.OrderItem;
/**
* @author陈康
* @site www.chenkang.com
* @company xxx公司
* @create 2019-10-23 18:43
*/
public class OrderItemVo extends OrderItem {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
在OrderMapper中设置一个方法来查询两张表(一对多)
List<OrderVo>selectByOrderId(@Param("orderId") Integer orderId);
在OrderMapper.xml中配置(一对多)
<resultMap id="OrderVoMap" type="com.chenkang.model.vo.OrderVo">
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<collection property="orderItems" ofType="com.chenkang.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>
OrderItemMapper.java(多对一)
List<OrderItemVo>selectByOrderItemId(@Param("orderItemId") Integer orderItemId);
在OrderItemMapper.xml中配置(多对一)
<resultMap id="OrderItemVoMap" type="com.chenkang.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.chenkang.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>
service层
OneToManyService接口类
package com.chenkang.service;
import com.chenkang.model.vo.OrderItemVo;
import com.chenkang.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author陈康
* @site www.chenkang.com
* @company xxx公司
* @create 2019-10-23 19:07
*/
public interface OneToManyService {
List<OrderVo> selectByOrderId(Integer orderId);
List<OrderItemVo>selectByOrderItemId(Integer orderItemId);
}
OneToManyServiceImpl实现service接口
package com.chenkang.service.impl;
import com.chenkang.mapper.OrderItemMapper;
import com.chenkang.mapper.OrderMapper;
import com.chenkang.model.vo.OrderItemVo;
import com.chenkang.model.vo.OrderVo;
import com.chenkang.service.OneToManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author陈康
* @site www.chenkang.com
* @company xxx公司
* @create 2019-10-23 19:23
*/
@Service
public class OneToManyServiceImpl implements OneToManyService {
@Autowired
private OrderItemMapper orderItemMapper;
@Autowired
private OrderMapper orderMapper;
@Override
public List<OrderVo> selectByOrderId(Integer orderId) {
return orderMapper.selectByOrderId(orderId);
}
@Override
public List<OrderItemVo> selectByOrderItemId(Integer orderItemId) {
return orderItemMapper.selectByOrderItemId(orderItemId);
}
}
测试 :
OneToManyServiceImplTest
package com.chenkang.service.impl;
import com.chenkang.SpringBaseTest;
import com.chenkang.model.OrderItem;
import com.chenkang.model.vo.OrderItemVo;
import com.chenkang.model.vo.OrderVo;
import com.chenkang.service.OneToManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import static org.junit.Assert.*;
/**
* @author陈康
* @site www.chenkang.com
* @company xxx公司
* @create 2019-10-23 19:25
*/
public class OneToManyServiceImplTest extends SpringBaseTest {
@Autowired
private OneToManyService oneToManyService;
@Test
public void selectByOrderId() {
List<OrderVo> orderVos = oneToManyService.selectByOrderId(8);
OrderVo orderVo = orderVos.get(0);
System.out.println(orderVo);
List<OrderItem> orderItems = orderVo.getOrderItems();
System.out.println(orderItems);
}
@Test
public void selectByOrderItemId() {
List<OrderItemVo> orderItemVos = oneToManyService.selectByOrderItemId(36);
OrderItemVo orderItemVo = orderItemVos.get(0);
System.out.println(orderItemVo);
System.out.println(orderItemVo.getOrder());
}
}
2.多对多
HbookVo
package com.chenkang.model.vo;
import com.chenkang.model.Category;
import com.chenkang.model.Hbook;
import java.util.ArrayList;
import java.util.List;
/**
* @author陈康
* @site www.chenkang.com
* @company xxx公司
* @create 2019-10-23 19:39
*/
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.chenkang.model.vo;
import com.chenkang.model.Category;
import com.chenkang.model.Hbook;
import java.util.ArrayList;
import java.util.List;
/**
* @author陈康
* @site www.chenkang.com
* @company xxx公司
* @create 2019-10-23 19:41
*/
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;
}
}
BookCategoryMapper
在接口类中设置方法,以便后面测试
CategoryVo selectByCid(@Param("cid") Integer cid);
BookCategoryMapper.xml
<resultMap id="CategoryVoMap" type="com.chenkang.model.vo.CategoryVo">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
<collection property="hbooks" ofType="com.chenkang.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 and bc.cid=c.category_id
and c.category_id = #{cid}
</select>
HbookMapper
HbookVo selectByBid(@Param("bid") Integer bid);
HbookMapper.XML
<resultMap id="HbookVoMap" type="com.chenkang.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="categories" ofType="com.chenkang.model.Category">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
</collection>
<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 and bc.cid=c.category_id
and b.book_id = #{bid}
</select>
Service层
ManyToManyService
package com.chenkang.service;
import com.chenkang.model.Category;
import com.chenkang.model.Hbook;
import com.chenkang.model.vo.CategoryVo;
import com.chenkang.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
/**
* @author陈康
* @site www.chenkang.com
* @company xxx公司
* @create 2019-10-23 20:09
*/
public interface ManyToManyService {
HbookVo selectByBid(Integer bid);
CategoryVo selectByCid(Integer cid);
}
去实现接口中的方法
ManyToManyServiceimpl
package com.chenkang.service.impl;
import com.chenkang.mapper.CategoryMapper;
import com.chenkang.mapper.HbookMapper;
import com.chenkang.model.Category;
import com.chenkang.model.Hbook;
import com.chenkang.model.vo.CategoryVo;
import com.chenkang.model.vo.HbookVo;
import com.chenkang.service.ManyToManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author陈康
* @site www.chenkang.com
* @company xxx公司
* @create 2019-10-23 20:10
*/
@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);
}
}
测试:
package com.chenkang.service.impl;
import com.chenkang.SpringBaseTest;
import com.chenkang.model.Category;
import com.chenkang.model.Hbook;
import com.chenkang.model.vo.CategoryVo;
import com.chenkang.model.vo.HbookVo;
import com.chenkang.service.ManyToManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
/**
* @author陈康
* @site www.chenkang.com
* @company xxx公司
* @create 2019-10-23 20:13
*/
public class ManyToManyServiceImplTest extends SpringBaseTest {
@Autowired
private ManyToManyService manyToManyService;
@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 = manyToManyService.selectByCid(8);
System.out.println(categoryVo);
for (Hbook hbook : categoryVo.getHbooks()) {
System.out.println(hbook);
}
}
}