目录
什么叫一对多?
比如:一个订单对应多个订单项
1、先用逆向生成工具生成t_hibernate_order(订单)、t_hibernate_order_item(订单项)
generatorConfig.xml
- 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:\\maven\\maven_ localRepository\\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.cxy.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.cxy.mapper"
- targetProject="src/main/java">
-
- <property name="enableSubPackages" value="false"/>
- sqlMapGenerator>
-
-
-
-
-
- <javaClientGenerator targetPackage="com.cxy.mapper"
- targetProject="src/main/java" type="XMLMAPPER">
-
- <property name="enableSubPackages" value="false"/>
- javaClientGenerator>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <table schema="" tableName="t_mvc_book" domainObjectName="Book"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
- table>
-
- <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>
-
- context>
- generatorConfiguration>
OrderMapper.xml
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.cxy.mapper.OrderMapper" >
- <resultMap id="BaseResultMap" type="com.cxy.model.Order" >
- <constructor >
- <idArg column="order_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="order_no" jdbcType="VARCHAR" javaType="java.lang.String" />
- constructor>
- resultMap>
-
- <resultMap id="OrderVoMap" type="com.cxy.model.vo.OrderVo" >
- <result property="orderId" column="order_id">result>
- <result property="orderNo" column="order_no">result>
- <collection property="orderItems" ofType="com.cxy.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>
-
-
- <sql id="Base_Column_List" >
- order_id, order_no
- sql>
-
- <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>
-
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from t_hibernate_order
- where order_id = #{orderId,jdbcType=INTEGER}
- select>
-
-
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from t_hibernate_order
- where order_id = #{orderId,jdbcType=INTEGER}
- delete>
- <insert id="insert" parameterType="com.cxy.model.Order" >
- insert into t_hibernate_order (order_id, order_no)
- values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
- insert>
- <insert id="insertSelective" parameterType="com.cxy.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=INTEGER},
- if>
- <if test="orderNo != null" >
- #{orderNo,jdbcType=VARCHAR},
- if>
- trim>
- insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.cxy.model.Order" >
- update t_hibernate_order
- <set >
- <if test="orderNo != null" >
- order_no = #{orderNo,jdbcType=VARCHAR},
- if>
- set>
- where order_id = #{orderId,jdbcType=INTEGER}
- update>
- <update id="updateByPrimaryKey" parameterType="com.cxy.model.Order" >
- update t_hibernate_order
- set order_no = #{orderNo,jdbcType=VARCHAR}
- where order_id = #{orderId,jdbcType=INTEGER}
- update>
- mapper>
orderBiz.java
- package com.cxy.biz;
-
- import com.cxy.model.Order;
- import com.cxy.model.vo.OrderVo;
- import org.apache.ibatis.annotations.Param;
-
- public interface OrderBiz {
- 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);
- }
orderBizImpl.java
- package com.cxy.biz.impl;
-
- import com.cxy.biz.OrderBiz;
- import com.cxy.mapper.OrderMapper;
- import com.cxy.model.Order;
- import com.cxy.model.vo.OrderVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- @Service
- public class OrderBizImpl implements OrderBiz {
- @Autowired
- private OrderMapper orderMapper ;
-
- @Override
- public int deleteByPrimaryKey(Integer orderId) {
- return orderMapper.deleteByPrimaryKey(orderId);
- }
-
- @Override
- public int insert(Order record) {
- return orderMapper.insert(record);
- }
-
- @Override
- public int insertSelective(Order record) {
- return orderMapper.insertSelective(record);
- }
-
- @Override
- public Order selectByPrimaryKey(Integer orderId) {
- return orderMapper.selectByPrimaryKey(orderId);
- }
-
- @Override
- public int updateByPrimaryKeySelective(Order record) {
- return orderMapper.updateByPrimaryKeySelective(record);
- }
-
- @Override
- public int updateByPrimaryKey(Order record) {
- return orderMapper.updateByPrimaryKey(record);
- }
-
- @Override
- public OrderVo queryOrderVoByOrderId(Integer orderId) {
- return orderMapper.queryOrderVoByOrderId(orderId);
- }
- }
OrderitemMapper.xml
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.cxy.mapper.OrderItemMapper" >
- <resultMap id="BaseResultMap" type="com.cxy.model.OrderItem" >
- <constructor >
- <idArg column="order_item_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="product_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="quantity" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="oid" jdbcType="INTEGER" javaType="java.lang.Integer" />
- constructor>
- resultMap>
-
- <resultMap id="OrderVoMap" type="com.cxy.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.cxy.model.Order">
- <result property="orderId" column="order_id">result>
- <result property="orderNo" column="order_no">result>
- association>
- resultMap>
-
- <sql id="Base_Column_List" >
- order_item_id, product_id, quantity, oid
- sql>
-
- <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>
-
-
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from t_hibernate_order_item
- where order_item_id = #{orderItemId,jdbcType=INTEGER}
- select>
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from t_hibernate_order_item
- where order_item_id = #{orderItemId,jdbcType=INTEGER}
- delete>
- <insert id="insert" parameterType="com.cxy.model.OrderItem" >
- insert into t_hibernate_order_item (order_item_id, product_id, quantity,
- oid)
- values (#{orderItemId,jdbcType=INTEGER}, #{productId,jdbcType=INTEGER}, #{quantity,jdbcType=INTEGER},
- #{oid,jdbcType=INTEGER})
- insert>
- <insert id="insertSelective" parameterType="com.cxy.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=INTEGER},
- if>
- <if test="productId != null" >
- #{productId,jdbcType=INTEGER},
- if>
- <if test="quantity != null" >
- #{quantity,jdbcType=INTEGER},
- if>
- <if test="oid != null" >
- #{oid,jdbcType=INTEGER},
- if>
- trim>
- insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.cxy.model.OrderItem" >
- update t_hibernate_order_item
- <set >
- <if test="productId != null" >
- product_id = #{productId,jdbcType=INTEGER},
- if>
- <if test="quantity != null" >
- quantity = #{quantity,jdbcType=INTEGER},
- if>
- <if test="oid != null" >
- oid = #{oid,jdbcType=INTEGER},
- if>
- set>
- where order_item_id = #{orderItemId,jdbcType=INTEGER}
- update>
- <update id="updateByPrimaryKey" parameterType="com.cxy.model.OrderItem" >
- update t_hibernate_order_item
- set product_id = #{productId,jdbcType=INTEGER},
- quantity = #{quantity,jdbcType=INTEGER},
- oid = #{oid,jdbcType=INTEGER}
- where order_item_id = #{orderItemId,jdbcType=INTEGER}
- update>
- mapper>
orderitemBiz.java
- package com.cxy.biz;
-
- import com.cxy.model.OrderItem;
- import com.cxy.model.vo.OrderItemVo;
- import org.apache.ibatis.annotations.Param;
-
- public interface OrderItemBia {
- 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);
- }
orderitemBizImpl.java
- package com.cxy.biz.impl;
-
- import com.cxy.biz.OrderItemBia;
- import com.cxy.mapper.OrderItemMapper;
- import com.cxy.model.OrderItem;
- import com.cxy.model.vo.OrderItemVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- @Service
- public class OrderItemBizImpl implements OrderItemBia {
- @Autowired
- private OrderItemMapper orderItemMapper;
-
- @Override
- public int deleteByPrimaryKey(Integer orderItemId) {
- return orderItemMapper.deleteByPrimaryKey(orderItemId);
- }
-
- @Override
- public int insert(OrderItem record) {
- return orderItemMapper.insert(record);
- }
-
- @Override
- public int insertSelective(OrderItem record) {
- return orderItemMapper.insertSelective(record);
- }
-
- @Override
- public OrderItem selectByPrimaryKey(Integer orderItemId) {
- return orderItemMapper.selectByPrimaryKey(orderItemId);
- }
-
- @Override
- public int updateByPrimaryKeySelective(OrderItem record) {
- return orderItemMapper.updateByPrimaryKeySelective(record);
- }
-
- @Override
- public int updateByPrimaryKey(OrderItem record) {
- return orderItemMapper.updateByPrimaryKey(record);
- }
-
- @Override
- public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
- return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
- }
- }
- package com.cxy.biz.impl;
-
- import com.cxy.model.vo.OrderItemVo;
- 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.*;
-
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
-
- public class OrderItemBizImplTest {
-
- @Autowired
- private OrderItemBizImpl orderItemBizimpl;
-
- @Test
- public void queryOrderItemVoByOrderItemId() {
- OrderItemVo orderitemVo = orderItemBizimpl.queryOrderItemVoByOrderItemId(43);
- System.out.println(orderitemVo);
-
- }
- }
什么是多对多?
比如一本书会包含多种类型(言情,玄幻等),而类型里包含多本书籍
首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,这两张表对应的model与mapper
HbookMapper.xml
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.cxy.mapper.HbookMapper" >
- <resultMap id="BaseResultMap" type="com.cxy.model.Hbook" >
- <constructor >
- <idArg column="book_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="book_name" jdbcType="VARCHAR" javaType="java.lang.String" />
- <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
- constructor>
- resultMap>
- <sql id="Base_Column_List" >
- book_id, book_name, price
- sql>
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from t_hibernate_book
- where book_id = #{bookId,jdbcType=INTEGER}
- select>
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from t_hibernate_book
- where book_id = #{bookId,jdbcType=INTEGER}
- delete>
- <insert id="insert" parameterType="com.cxy.model.Hbook" >
- insert into t_hibernate_book (book_id, book_name, price
- )
- values (#{bookId,jdbcType=INTEGER}, #{bookName,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
- )
- insert>
- <insert id="insertSelective" parameterType="com.cxy.model.Hbook" >
- insert into t_hibernate_book
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="bookId != null" >
- book_id,
- if>
- <if test="bookName != null" >
- book_name,
- if>
- <if test="price != null" >
- price,
- if>
- trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="bookId != null" >
- #{bookId,jdbcType=INTEGER},
- if>
- <if test="bookName != null" >
- #{bookName,jdbcType=VARCHAR},
- if>
- <if test="price != null" >
- #{price,jdbcType=REAL},
- if>
- trim>
- insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.cxy.model.Hbook" >
- update t_hibernate_book
- <set >
- <if test="bookName != null" >
- book_name = #{bookName,jdbcType=VARCHAR},
- if>
- <if test="price != null" >
- price = #{price,jdbcType=REAL},
- if>
- set>
- where book_id = #{bookId,jdbcType=INTEGER}
- update>
- <update id="updateByPrimaryKey" parameterType="com.cxy.model.Hbook" >
- update t_hibernate_book
- set book_name = #{bookName,jdbcType=VARCHAR},
- price = #{price,jdbcType=REAL}
- where book_id = #{bookId,jdbcType=INTEGER}
- update>
- mapper>
HbookCategoryBiz.java
- package com.cxy.biz;
-
- import com.cxy.model.HbookCategory;
- import com.cxy.model.vo.CategoryVo;
- import com.cxy.model.vo.HbookVo;
-
- public interface HbookCategoryBiz {
-
- int deleteByPrimaryKey(Integer bcid);
-
- int insert(HbookCategory record);
-
- int insertSelective(HbookCategory record);
-
- HbookCategory selectByPrimaryKey(Integer bcid);
-
- int updateByPrimaryKeySelective(HbookCategory record);
-
- int updateByPrimaryKey(HbookCategory record);
-
- HbookVo queryByBookId( Integer bookId);
-
- CategoryVo queryByCid( Integer cid);
-
- }
HbookCategoryBizImpl .java
- package com.cxy.biz.impl;
-
- import com.cxy.biz.HbookCategoryBiz;
- import com.cxy.mapper.HbookCategoryMapper;
- import com.cxy.model.HbookCategory;
- import com.cxy.model.vo.CategoryVo;
- import com.cxy.model.vo.HbookVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- @Service
- public class HbookCategoryBizImpl implements HbookCategoryBiz {
- @Autowired
- private HbookCategoryMapper hbookCategoryMapper;
-
- @Override
- public HbookVo queryByBookId(Integer bookId) {
- return hbookCategoryMapper.queryByBookId(bookId);
- }
-
- @Override
- public CategoryVo queryByCid(Integer cid) {
- return hbookCategoryMapper.queryByCid(cid);
- }
- }
- package com.cxy.biz.impl;
-
- import com.cxy.biz.HbookCategoryBiz;
- import com.cxy.model.vo.CategoryVo;
- import com.cxy.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.*;
-
-
- @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);
- hbookVo.getCategories().forEach(System.out::println);
- }
-
- @Test
- public void queryByCid() {
- CategoryVo categoryVo = hbookCategoryBiz.queryByCid(8);
- System.out.println(categoryVo);
- categoryVo.getHbooks().forEach(System.out::println);
-
- }
- }