目录
1、首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
2、 编写一对多一方面OrderMapper.xml配置文件
7、编写一对多多方面OrderItemMapper.xml配置文件
- <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>
逆向生成
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.zking.ssm.mapper.OrderMapper" >
- <resultMap id="BaseResultMap" type="com.zking.ssm.model.Order" >
- <constructor >
- <arg column="order_id" jdbcType="VARCHAR" javaType="java.lang.String" />
- <arg column="order_no" jdbcType="VARCHAR" javaType="java.lang.String" />
- constructor>
- resultMap>
-
- <resultMap id="OrdervVoMap" type="com.zking.ssm.model.vo.OrdervVo" >
- <result column="order_id" property="orderId">result>
- <result column="order_no" property="orderNo">result>
- <collection property="orderItems" ofType="com.zking.ssm.model.OrderItem">
- <result column="order_item_id" property="orderItemId">result>
- <result column="product_id" property="productId">result>
- <result column="quantity" property="quantity">result>
- <result column="oid" property="oid">result>
- collection>
- resultMap>
-
- <select id="queryOrderVoByOrderId" resultMap="OrdervVoMap" 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>
-
- <insert id="insert" parameterType="com.zking.ssm.model.Order" >
- insert into t_hibernate_order (order_id, order_no)
- values (#{orderId,jdbcType=VARCHAR}, #{orderNo,jdbcType=VARCHAR})
- insert>
- <insert id="insertSelective" parameterType="com.zking.ssm.model.Order" >
- insert into t_hibernate_order
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="orderId != null" >
- order_id,
- if>
- <if test="orderNo != null" >
- order_no,
- if>
- trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="orderId != null" >
- #{orderId,jdbcType=VARCHAR},
- if>
- <if test="orderNo != null" >
- #{orderNo,jdbcType=VARCHAR},
- if>
- trim>
- insert>
- mapper>
- package com.zking.ssm.model.vo;
-
- import com.zking.ssm.model.Order;
- import com.zking.ssm.model.OrderItem;
-
- import java.util.List;
-
- /**
- * @author 白未
- * @site 3185579318
- * @company xxx公司
- * @create 2022-08-16 9:21
- *
- * vo类不仅仅包含当前表的信息还能包含相关联表的信息
- *
- * 当前订单会有一个或者多个订单项,多个订单项必须要集合进行接收
- */
- public class OrdervVo extends Order {
- //通过订单号查询本次订单的详细信息及对应的所有订单项信息
- private List
orderItems; -
- public List
getOrderItems() { - return orderItems;
- }
-
- public void setOrderItems(List
orderItems) { - this.orderItems = orderItems;
- }
- }
-
给实体类添加注解@Data(相当于添加了set/get方法,同时添加了tostring方法)
- package com.zking.ssm.model;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- //t_hibernate_order
- //相当于添加了set/get方法,同时添加了tostring方法
- //@NoArgsConstructor
- //@AllArgsConstructor
- @Data
- public class Order {
- private String orderId;
-
- private String orderNo;
-
- }
- package com.zking.ssm.biz;
-
- import com.zking.ssm.model.Order;
- import com.zking.ssm.model.vo.OrdervVo;
- import org.apache.ibatis.annotations.Param;
-
- public interface OrderBiz {
- int insert(Order record);
-
- int insertSelective(Order record);
-
-
- OrdervVo queryOrderVoByOrderId(Integer orderId);
- }
在impl里添加@Service注解
- package com.zking.ssm.biz.impl;
-
- import com.zking.ssm.biz.OrderBiz;
- import com.zking.ssm.mapper.OrderMapper;
- import com.zking.ssm.model.Order;
- import com.zking.ssm.model.vo.OrdervVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- /**
- * @author 白未
- * @site 3185579318
- * @company xxx公司
- * @create 2022-08-16 9:55
- */
- @Service
- public class OrderBizImpl implements OrderBiz {
- @Autowired
- private OrderMapper orderMapper;
- @Override
- public int insert(Order record) {
- return 0;
- }
-
- @Override
- public int insertSelective(Order record) {
- return 0;
- }
-
- @Override
- public OrdervVo queryOrderVoByOrderId(Integer orderId) {
- return orderMapper.queryOrderVoByOrderId(orderId);
- }
- }
- package com.zking.ssm.biz.impl;
-
- import com.zking.ssm.biz.OrderBiz;
- import com.zking.ssm.model.vo.OrdervVo;
- import org.apache.logging.log4j.core.config.Order;
- 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;
-
- import static org.junit.Assert.*;
-
- /**
- * @author 白未
- * @site 3185579318
- * @company xxx公司
- * @create 2022-08-16 9:58
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
- public class OrderBizImplTest {
- @Autowired
- private OrderBiz orderBiz;
-
-
- @Test
- public void queryOrderVoByOrderId() {
- OrdervVo order = orderBiz.queryOrderVoByOrderId(9);
- System.out.println(order);
- order.getOrderItems().forEach(System.out::println);
- }
- }
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.zking.ssm.mapper.OrderItemMapper" >
- <resultMap id="BaseResultMap" type="com.zking.ssm.model.OrderItem" >
- <constructor >
- <arg column="order_item_id" jdbcType="VARCHAR" javaType="java.lang.String" />
- <arg column="product_id" jdbcType="VARCHAR" javaType="java.lang.String" />
- <arg column="quantity" jdbcType="VARCHAR" javaType="java.lang.String" />
- <arg column="oid" jdbcType="VARCHAR" javaType="java.lang.String" />
- constructor>
- resultMap>
-
- <resultMap id="OrderItemVoMap" type="com.zking.ssm.model.vo.OrderItemVo" >
- <result column="order_item_id" property="orderItemId">result>
- <result column="product_id" property="productId">result>
- <result column="quantity" property="quantity">result>
- <result column="oid" property="oid">result>
- <association property="order" javaType="com.zking.ssm.model.Order">
- <result column="order_id" property="orderId">result>
- <result column="order_no" property="orderNo">result>
- association>
- resultMap>
-
- <select id="queryOrderItemVoByOrderItemId" 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>
-
- <insert id="insert" parameterType="com.zking.ssm.model.OrderItem" >
- insert into t_hibernate_order_item (order_item_id, product_id, quantity,
- oid)
- values (#{orderItemId,jdbcType=VARCHAR}, #{productId,jdbcType=VARCHAR}, #{quantity,jdbcType=VARCHAR},
- #{oid,jdbcType=VARCHAR})
- insert>
- <insert id="insertSelective" parameterType="com.zking.ssm.model.OrderItem" >
- insert into t_hibernate_order_item
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="orderItemId != null" >
- order_item_id,
- if>
- <if test="productId != null" >
- product_id,
- if>
- <if test="quantity != null" >
- quantity,
- if>
- <if test="oid != null" >
- oid,
- if>
- trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="orderItemId != null" >
- #{orderItemId,jdbcType=VARCHAR},
- if>
- <if test="productId != null" >
- #{productId,jdbcType=VARCHAR},
- if>
- <if test="quantity != null" >
- #{quantity,jdbcType=VARCHAR},
- if>
- <if test="oid != null" >
- #{oid,jdbcType=VARCHAR},
- if>
- trim>
- insert>
- mapper>
以下步骤与一方法相似就不一一展示了
- generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
- <generatorConfiguration>
-
- <properties resource="jdbc.properties"/>
-
-
- <classPathEntry location="E:\\AnZhuangBao\\Maven\\mvn_repository\\mysql\\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar"/>
-
-
- <context id="infoGuardian">
-
- <commentGenerator>
- <property name="suppressAllComments" value="true"/>
- <property name="suppressDate" value="true"/>
- commentGenerator>
-
-
- <jdbcConnection driverClass="${jdbc.driver}"
- connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>
-
-
- <javaTypeResolver>
-
- <property name="forceBigDecimals" value="false"/>
- javaTypeResolver>
-
-
-
-
- <javaModelGenerator targetPackage="com.leaf.ssm.model"
- targetProject="src/main/java">
-
- <property name="enableSubPackages" value="false"/>
-
- <property name="constructorBased" value="true"/>
-
- <property name="trimStrings" value="false"/>
-
- <property name="immutable" value="false"/>
- javaModelGenerator>
-
-
- <sqlMapGenerator targetPackage="com.leaf.ssm.mapper"
- targetProject="src/main/java">
-
- <property name="enableSubPackages" value="false"/>
- sqlMapGenerator>
-
-
-
-
-
- <javaClientGenerator targetPackage="com.leaf.ssm.mapper"
- targetProject="src/main/java" type="XMLMAPPER">
-
- <property name="enableSubPackages" value="false"/>
- javaClientGenerator>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <table schema="" tableName="t_hibernate_book" domainObjectName="Hbook"
- 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>
-
- <table schema="" tableName="t_hibernate_book_category" domainObjectName="HbookCategory"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
- table>
-
- context>
- generatorConfiguration>
生成成功
- package com.zking.ssm.model.vo;
-
- import com.zking.ssm.model.Category;
- import com.zking.ssm.model.Hbook;
- import lombok.Data;
- import lombok.ToString;
-
- import java.util.List;
-
- /**
- * @author 白未
- * @site 3185579318
- * @company xx公司
- * @create 2022-08-16 11:36
- */
- @Data
- public class CategoryVo extends Category {
- private List
hbooks; -
- public List
getHbooks() { - return hbooks;
- }
-
- public void setHbooks(List
hbooks) { - this.hbooks = hbooks;
- }
- }
- package com.zking.ssm.model.vo;
-
- import com.zking.ssm.model.Category;
- import com.zking.ssm.model.Hbook;
- import lombok.Data;
-
- import java.util.List;
-
- /**
- * @author 白未
- * @site 3185579318
- * @company xx公司
- * @create 2022-08-16 11:36
- */
- @Data
- public class HbookVo extends Hbook {
- private List
categories; -
- public List
getCategories() { - return categories;
- }
-
- public void setCategories(List
categories) { - this.categories = categories;
- }
- }
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.zking.ssm.mapper.HbookCategoryMapper" >
- <resultMap id="BaseResultMap" type="com.zking.ssm.model.HbookCategory" >
- <constructor >
- <idArg column="bcid" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="cid" jdbcType="INTEGER" javaType="java.lang.Integer" />
- constructor>
- resultMap>
- <sql id="Base_Column_List" >
- bcid, bid, cid
- sql>
-
- <resultMap id="HbookVoMap" type="com.zking.ssm.model.vo.HbookVo">
- <result property="bookId" column="book_id">result>
- <result property="bookName" column="book_name">result>
- <collection property="categories" ofType="com.zking.ssm.model.Category">
- <result property="categoryId" column="category_id">result>
- <result property="categoryName" column="category_name">result>
- collection>
- resultMap>
-
- <resultMap id="CategoryVoMap" type="com.zking.ssm.model.vo.CategoryVo">
- <result property="categoryId" column="category_id">result>
- <result property="categoryName" column="category_name">result>
- <collection property="hbooks" ofType="com.zking.ssm.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>
-
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from t_hibernate_book_category
- where bcid = #{bcid,jdbcType=INTEGER}
- select>
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from t_hibernate_book_category
- where bcid = #{bcid,jdbcType=INTEGER}
- delete>
- <insert id="insert" parameterType="com.leaf.ssm.model.HbookCategory" >
- insert into t_hibernate_book_category (bcid, bid, cid
- )
- values (#{bcid,jdbcType=INTEGER}, #{bid,jdbcType=INTEGER}, #{cid,jdbcType=INTEGER}
- )
- insert>
- <insert id="insertSelective" parameterType="com.zking.ssm.model.HbookCategory" >
- insert into t_hibernate_book_category
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="bcid != null" >
- bcid,
- if>
- <if test="bid != null" >
- bid,
- if>
- <if test="cid != null" >
- cid,
- if>
- trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="bcid != null" >
- #{bcid,jdbcType=INTEGER},
- if>
- <if test="bid != null" >
- #{bid,jdbcType=INTEGER},
- if>
- <if test="cid != null" >
- #{cid,jdbcType=INTEGER},
- if>
- trim>
- insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.zking.ssm.model.HbookCategory" >
- update t_hibernate_book_category
- <set >
- <if test="bid != null" >
- bid = #{bid,jdbcType=INTEGER},
- if>
- <if test="cid != null" >
- cid = #{cid,jdbcType=INTEGER},
- if>
- set>
- where bcid = #{bcid,jdbcType=INTEGER}
- update>
- <update id="updateByPrimaryKey" parameterType="com.zking.ssm.model.HbookCategory" >
- update t_hibernate_book_category
- set bid = #{bid,jdbcType=INTEGER},
- cid = #{cid,jdbcType=INTEGER}
- where bcid = #{bcid,jdbcType=INTEGER}
- update>
- mapper>
- package com.zking.ssm.biz.impl;
-
- import com.zking.ssm.biz.HbookCategoryBiz;
- import com.zking.ssm.model.vo.CategoryVo;
- import com.zking.ssm.model.vo.HbookVo;
- 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;
-
- import static org.junit.Assert.*;
-
- /**
- * @author zking
- * @site 3185579318
- * @company xx公司
- * @create 2022-08-16 11:43
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
- public class HbookCategoryBizImplTest {
-
- @Autowired
- private HbookCategoryBiz hbookCategoryBiz;
-
- @Test
- public void queryByBookId() {
- HbookVo hbookVo = hbookCategoryBiz.queryByBookId(8);
- System.out.println(hbookVo);
- }
-
- @Test
- public void queryByCid() {
- CategoryVo categoryVo = hbookCategoryBiz.queryByCid(8);
- System.out.println(categoryVo);
- }
- }