目录
首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper
①Order
- package com.chenchen.ssm.model;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- //t_hibernate_order
- //相当于添加了set/get方法,同时添加tostring
- //@NoArgsConstructor
- //@AllArgsConstructor
- @Data
- public class Order {
- private Integer orderId;
-
- private String orderNo;
-
- }
- package com.chenchen.ssm.model;
-
- import lombok.Data;
- import lombok.ToString;
-
- @ToString
- public class OrderItem {
- private Integer orderItemId;
-
- private Integer productId;
-
- private Integer quantity;
-
- private Integer oid;
-
- public OrderItem(Integer orderItemId, Integer productId, Integer quantity, Integer oid) {
- this.orderItemId = orderItemId;
- this.productId = productId;
- this.quantity = quantity;
- this.oid = oid;
- }
-
- public OrderItem() {
- super();
- }
-
- public Integer getOrderItemId() {
- return orderItemId;
- }
-
- public void setOrderItemId(Integer orderItemId) {
- this.orderItemId = orderItemId;
- }
-
- public Integer getProductId() {
- return productId;
- }
-
- public void setProductId(Integer productId) {
- this.productId = productId;
- }
-
- public Integer getQuantity() {
- return quantity;
- }
-
- public void setQuantity(Integer quantity) {
- this.quantity = quantity;
- }
-
- public Integer getOid() {
- return oid;
- }
-
- public void setOid(Integer oid) {
- this.oid = oid;
- }
- }
- package com.chenchen.ssm.biz;
-
- import com.chenchen.ssm.model.Book;
- import com.chenchen.ssm.model.BookVo;
- import com.chenchen.ssm.model.Order;
- import com.chenchen.ssm.model.vo.OrderVo;
- import com.chen.ssm.util.PageBean;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
- import java.util.Map;
-
- public interface OrderBiz {
- int deleteByPrimaryKey(Integer orderId);
-
- int insert(Order record);
-
- int insertSelective(Order record);
-
- Order selectByPrimaryKey(Integer orderId);
-
- OrderVo queryOrderVoByOrderId(Integer orderId);
-
- int updateByPrimaryKeySelective(Order record);
-
- int updateByPrimaryKey(Order record);
- }
-
- package com.chenchen.ssm.biz.impl;
-
- import com.chenchen.ssm.biz.OrderBiz;
- import com.chenchen.ssm.mapper.OrderMapper;
- import com.chenchen.ssm.model.Order;
- import com.chenchen.ssm.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 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 OrderVo 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.chenchen.ssm.biz.impl;
-
- import com.chenchen.ssm.biz.OrderItemBiz;
- import com.chenchen.ssm.mapper.OrderItemMapper;
- import com.chenchen.ssm.model.OrderItem;
- import com.chenchen.ssm.model.vo.OrderItemVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- @Service
- public class OrderItemBizImpl implements OrderItemBiz {
- @Autowired
- private OrderItemMapper orderItemMapper;
- @Override
- public int deleteByPrimaryKey(Integer orderItemId) {
- return 0;
- }
-
- @Override
- public int insert(OrderItem record) {
- return 0;
- }
-
- @Override
- public int insertSelective(OrderItem record) {
- return 0;
- }
-
- @Override
- public OrderItem selectByPrimaryKey(Integer orderItemId) {
- return null;
- }
-
- @Override
- public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
- return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
- }
-
- @Override
- public int updateByPrimaryKeySelective(OrderItem record) {
- return 0;
- }
-
- @Override
- public int updateByPrimaryKey(OrderItem record) {
- return 0;
- }
- }
-
- package com.chenchen.ssm.mapper;
-
- import com.chenchen.ssm.model.Order;
- import com.chenchen.ssm.model.vo.OrderVo;
- import org.apache.ibatis.annotations.Param;
-
- public interface OrderMapper {
- int deleteByPrimaryKey(Integer orderId);
-
- int insert(Order record);
-
- int insertSelective(Order record);
-
- Order selectByPrimaryKey(Integer orderId);
-
- OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);
-
- int updateByPrimaryKeySelective(Order record);
-
- int updateByPrimaryKey(Order record);
- }
- "1.0" encoding="UTF-8" ?>
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.chenchen.ssm.mapper.OrderMapper" >
- <resultMap id="BaseResultMap" type="com.chenchen.ssm.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.chenchen.ssm.model.vo.OrderVo" >
- <result column="order_id" property="orderId">result>
- <result column="order_no" property="orderNo">result>
- <collection property="orderItems" ofType="com.chenchen.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>
-
- <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.chenchen.ssm.model.Order" >
- insert into t_hibernate_order (order_id, order_no)
- values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
- insert>
- <insert id="insertSelective" parameterType="com.chenchen.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=INTEGER},
- if>
- <if test="orderNo != null" >
- #{orderNo,jdbcType=VARCHAR},
- if>
- trim>
- insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.chenchen.ssm.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.chenchen.ssm.model.Order" >
- update t_hibernate_order
- set order_no = #{orderNo,jdbcType=VARCHAR}
- where order_id = #{orderId,jdbcType=INTEGER}
- update>
- mapper>
- package com.chenchen.ssm.mapper;
-
- import com.chenchen.ssm.model.OrderItem;
- import com.chenchen.ssm.model.vo.OrderItemVo;
- import org.apache.ibatis.annotations.Param;
-
- public interface OrderItemMapper {
- int deleteByPrimaryKey(Integer orderItemId);
-
- int insert(OrderItem record);
-
- int insertSelective(OrderItem record);
-
- OrderItem selectByPrimaryKey(Integer orderItemId);
-
- OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
-
- int updateByPrimaryKeySelective(OrderItem record);
-
- int updateByPrimaryKey(OrderItem record);
- }
queryByBookId运行效果:
queryByCid运行效果: