1、首先使用Mybatis逆向工程生成t_hibernate_order、t_hibernate_order_item两张表的代码
model层和Mapper层
2、建立vo类,vo类能够定义t_hibernate_order表里所没有的属性
vo类不仅拿到当前表的所有信息,还能拿到相关联表的所有信息
两个表之间的关系:
1、一个订单有多个订单详情表
2、一个订单详情只有一个订单
OrderVo:
- package com.zjy.model;
-
- import java.util.List;
-
- /**
- * @author小朱
- * @site Bi8boYin
- * @company xxx公司
- * @create 2022-08-15 19:27
- */
-
-
- //vo类不仅拿到当前表的所有信息,还能拿到相关联表的所有信息
- public class OrderVo extends Order {
-
- //通过订单号查询订单的详细信息以及所有订单项的信息
-
- private List
orderItems; -
-
- public List
getOrderItems() { - return orderItems;
- }
-
- public void setOrderItems(List
orderItems) { - this.orderItems = orderItems;
- }
- }
OrderItemVo:
- package com.zjy.model;
-
- /**
- * @author小朱
- * @site Bi8boYin
- * @company xxx公司
- * @create 2022-08-15 19:35
- */
-
-
- public class OrderItemVo extends OrderItem{
-
- private Order order;
-
-
- public Order getOrder() {
- return order;
- }
-
- public void setOrder(Order order) {
- this.order = order;
- }
- }
3、在OderMapper.xml中映射实体类resultMap以及对应的方法
- <resultMap id="OrderVoMap" type="com.zjy.model.OrderVo" >
-
- <result property="orderId" column="order_id">result>
- <result property="orderNo" column="order_no">result>
- <collection property="orderItems" ofType="com.zjy.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>
OrderItemMapper.xml
- <resultMap id="OrderVoMap" type="com.javaxl.ssm.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.javaxl.ssm.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>
4、在对应的OrderMapper.java和OrderItemsMapper.java中配置出对应的方法:
注意:该方法的的返回值一定是vo类,因为我们在xml中resultMap节点里包含了数据库表里不包含的字段
OrderMapper.java
OrderVo queryOrderItemVoByOrderItemId(Integer orderItemId);
OrderItemsMapper.java
OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId);
OrderBiz
- package com.zjy.biz;
-
- import com.zjy.model.Order;
- import org.apache.ibatis.annotations.Param;
-
- /**
- * @author小朱
- * @site Bi8boYin
- * @company xxx公司
- * @create 2022-08-15 19:58
- */
- public interface OrderBiz {
- int deleteByPrimaryKey(Integer orderId);
-
- int insert(Order record);
-
- int insertSelective(Order record);
-
- Order selectByPrimaryKey(Integer orderId);
-
- Order queryOrderVoByOrderId(@Param("orderId") Integer orderId);
-
- int updateByPrimaryKeySelective(Order record);
-
- int updateByPrimaryKey(Order record);
- }
OrderBizImpl
- package com.zjy.biz.impl;
-
- import com.zjy.biz.OrderBiz;
- import com.zjy.mapper.OrderMapper;
- import com.zjy.model.Order;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- /**
- * @author小朱
- * @site Bi8boYin
- * @company xxx公司
- * @create 2022-08-15 20:00
- */
-
- @Service
- public class OrderBizImpl implements OrderBiz {
- @Autowired
- private OrderMapper orderMapper;
- @Override
- public int deleteByPrimaryKey(Integer orderId) {
- return 0;
- }
-
- @Override
- public int insert(Order record) {
- return 0;
- }
-
- @Override
- public int insertSelective(Order record) {
- return 0;
- }
-
- @Override
- public Order selectByPrimaryKey(Integer orderId) {
- return null;
- }
-
- @Override
- public Order queryOrderVoByOrderId(Integer orderId) {
- return orderMapper.queryOrderVoByOrderId(orderId);
- }
-
- @Override
- public int updateByPrimaryKeySelective(Order record) {
- return 0;
- }
-
- @Override
- public int updateByPrimaryKey(Order record) {
- return 0;
- }
- }
- package com.zjy.biz.impl;
-
- import com.zjy.biz.OrderBiz;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
- /**
- * @author小朱
- * @site Bi8boYin
- * @company xxx公司
- * @create 2022-08-15 20:09
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
- public class OrderBizImplTest {
- @Autowired
- private OrderBiz orderBiz;
-
-
-
-
- @Test
- public void queryOrderVoByOrderId() {
- orderBiz.queryOrderVoByOrderId(9);
- }
-
-
- }
两张表所对应的关系:通过订单id能够查询出订单详情,通过订单详情也能找到该条订单的id
- <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="HBookC"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
-
-
-
-
- table>
-
-
-
- <table schema="" tableName="t_hibernate_category" domainObjectName="HCate"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
-
-
-
-
- table>
2.2在对应的HbookCategoryMapper.xml配置resultMap节点以及对应的方法:(为什么只在HbookCategoryMapper.xml中进行配置,因为只是一个中间表,可以起到一个连接的动作):
- <resultMap id="HbookVoMap" type="com.zjy.vo.HBookVo">
- <result property="bookId" column="book_id">result>
- <result property="bookName" column="book_name">result>
- <collection property="categories" ofType="com.zj.www.model.HCate">
- <result property="categoryId" column="category_id">result>
- <result property="categoryName" column="category_name">result>
- collection>
- resultMap>
-
-
- <resultMap id="CategoryVoMap" type="com.zjy.vo.HCateVo">
- <result property="categoryId" column="category_id">result>
- <result property="categoryName" column="category_name">result>
- <collection property="hbooks" ofType="com.zj.www.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>
- package com.zjy.mapper;
-
- import com.zjy.model.HBookC;
- import com.zjy.vo.HBookVo;
- import com.zjy.vo.HCateVo;
- import org.apache.ibatis.annotations.Param;
-
- public interface HBookCMapper {
- int deleteByPrimaryKey(Integer bcid);
-
- int insert(HBookC record);
-
- int insertSelective(HBookC record);
-
- HBookC selectByPrimaryKey(Integer bcid);
-
- int updateByPrimaryKeySelective(HBookC record);
-
- int updateByPrimaryKey(HBookC record);
-
-
- HBookVo queryByBookId(@Param("bookId") Integer bookId);
-
- HCateVo queryByCid(@Param("cid") Integer cid);
-
-
- }
Many2ManyService:
- package com.zjy.www.service;
-
- import com.zjy.www.vo.HBookVo;
- import com.zjy.www.vo.HCateVo;
-
- public interface Many2ManyService {
-
- HBookVo queryByBookId(Integer bookId);
-
- HCateVo queryByCid(Integer cid);
-
- }
Many2ManyServiceImpl:
- package com.zjy.www.service.impl;
-
- import com.zjy.www.mapper.HBookCMapper;
- import com.zjy.www.service.Many2ManyService;
- import com.zjy.www.vo.HBookVo;
- import com.zjy.www.vo.HCateVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- @Service
- public class Many2ManyServiceImpl implements Many2ManyService {
- @Autowired
- private HBookCMapper hBookCMapper;
- @Override
- public HBookVo queryByBookId(Integer bookId) {
- return hBookCMapper.queryByBookId(bookId);
- }
-
- @Override
- public HCateVo queryByCid(Integer cid) {
- return hBookCMapper.queryByCid(cid);
- }
- }
HBookVo:
- package com.zjy.www.vo;
-
- import com.zjy.www.model.HBook;
- import com.zjy.www.model.HBookC;
- import com.zjy.www.model.HCate;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class HBookVo extends HBook {
-
-
- private List<HCate> categories = new ArrayList<>();
-
- public List<HCate> getCategories() {
- return categories;
- }
-
- public void setCategories(List<HCate> categories) {
- this.categories = categories;
- }
-
-
- }
HBookC:
- package com.zjy.www.vo;
-
- import com.zjy.www.model.HBook;
- import com.zjy.www.model.HCate;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class HCateVo extends HCate {
-
-
- private List
hbooks = new ArrayList<>(); -
- public List
getHbooks() { - return hbooks;
- }
-
- public void setHbooks(List
hbooks) { - this.hbooks = hbooks;
- }
-
-
- @Override
- public String toString() {
- return "HCateVo{" +
- "hbooks=" + hbooks +
- '}';
- }
- }
- package com.zjy.www.service.impl;
-
- import com.zjy.www.model.HBook;
- import com.zjy.www.model.HCate;
- import com.zjy.www.service.Many2ManyService;
- import com.zjy.www.vo.HBookVo;
- import com.zjy.www.vo.HCateVo;
- import junit.framework.TestCase;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext.xml"})
- public class Many2ManyServiceImplTest extends TestCase {
-
- @Autowired
- private Many2ManyService many2ManyService;
-
- @Test
- public void queryByBookId() {
- HBookVo hbookVo = many2ManyService.queryByBookId(8);
- System.out.println(hbookVo);
- for (HCate category : hbookVo.getCategories()) {
- System.out.println(category);
- }
- }
-
- @Test
- public void queryByCid() {
- HCateVo categoryVo = many2ManyService.queryByCid(8);
- System.out.println(categoryVo);
- for (HBook hbook : categoryVo.getHbooks()) {
- System.out.println(hbook);
- }
-
- }
- }