目录
前言:
一对多是什么
例:一个老师教多个班级
连表查询,可以拿到每个班级的详细信息以及对应的授课老师
select * from teacher right join class on t.id=c.tid where c.id=666
多对多是什么:
比如一个书籍对应多种类别或者一个类别对应了多本书,而中间必定会有一个书籍类别中间表
以下就用订单表以及订单项两个表作为案例来给大家讲解以下:
数据库展示:
订单表:
订单项表:
用代码生成器生成对应的mapper和model
generatorConfig.xml:
-
-
"" tableName="t_hibernate_order" domainObjectName="Order"- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
-
-
- <table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
- table>
成功生成:
然后我们根据两个需求来完成这一期的展示
需求一:
通过订单号查询本次订单的详细信息以及对应的所属订单项信息
select * from t_hibernate_order o , t_hibernate_order_item oi
where o.order_id=oi.oid and o.order_id=9;
需求二:
通过订单项的id查询出订单项详细信息及所有订单
select * from t_hibernate_order o , t_hibernate_order_item oi
where o.order_id=oi.oid and oi.order_ item_id=43;
orderMapper.xml:
- <resultMap id="OrderVoMap" type="com.javaxl.ssm.model.vo.OrderVo" >
- <result property="orderId" column="order_id">result>
- <result property="orderNo" column="order_no">result>
- <collection property="orderItems" ofType="com.javaxl.ssm.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>
orderItemMpper.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>
在对应的OrderMapper.java和OrderItemsMapper.java中配置出对应的方法:
OrderMapper.java:
OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);
OrderItemsMapper.java:
OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
之后到service层中接口和实现类中写好方法
两个表之间的关系:
1、一个订单有多个订单详情表
2、一个订单详情只有一个订单
OrderVo :
- package com.ljj.vo;
-
- import com.ljj.model.Order;
- import com.ljj.model.OrderItem;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class OrderVo extends Order {
- private List
orderItems = new ArrayList<>(); -
- public List
getOrderItems() { - return orderItems;
- }
-
- public void setOrderItems(List
orderItems) { - this.orderItems = orderItems;
- }
-
-
- @Override
- public String toString() {
- return "OrderVo{" +
- "orderItems=" + orderItems +
- '}';
- }
- }
OrderItemVo :
- package com.ljj.vo;
-
- import com.ljj.model.Order;
- import com.ljj.model.OrderItem;
-
- public class OrderItemVo extends OrderItem {
- private Order order;
-
- public Order getOrder() {
- return order;
- }
-
- public void setOrder(Order order) {
- this.order = order;
- }
-
-
- @Override
- public String toString() {
- return "OrderItemVo{" +
- "order=" + order +
- '}';
- }
- }
效果展示:
一个订单项
一笔订单:
1、大同小异,可以看成两个一对多关系。
2、多对多关系的配置的步骤和一对多关系配置是一样的
首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,这两张表对应的model与mapper
-
"" tableName="t_hibernate_book" domainObjectName="HBook"- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
-
-
-
-
-
-
-
- <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>
在对应的HbookCategoryMapper.xml配置resultMap节点以及对应的方法:(为什么只在HbookCategoryMapper.xml中进行配置,因为只是一个中间表,可以起到一个连接的动作):
- <resultMap id="HbookVoMap" type="com.ljj.www.vo.HBookVo">
- <result property="bookId" column="book_id">result>
- <result property="bookName" column="book_name">result>
- <collection property="categories" ofType="com.ljj.www.model.HCate">
- <result property="categoryId" column="category_id">result>
- <result property="categoryName" column="category_name">result>
- collection>
- resultMap>
-
-
- <resultMap id="CategoryVoMap" type="com.ljj.www.vo.HCateVo">
- <result property="categoryId" column="category_id">result>
- <result property="categoryName" column="category_name">result>
- <collection property="hbooks" ofType="com.ljj.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.ljj.mapper;
-
- import com.ljj.model.HBookC;
- import com.ljj.vo.HBookVo;
- import com.ljj.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);
-
-
- }
展示效果: