1、先用逆向生成工具生成t_hibernate_order(订单)、t_hibernate_order_item(订单项)
generatorConfig.xml
OrderMapper.xml
order_id, order_no
delete from t_hibernate_order
where order_id = #{orderId,jdbcType=INTEGER}
insert into t_hibernate_order (order_id, order_no)
values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
insert into t_hibernate_order
order_id,
order_no,
#{orderId,jdbcType=INTEGER},
#{orderNo,jdbcType=VARCHAR},
update t_hibernate_order
order_no = #{orderNo,jdbcType=VARCHAR},
where order_id = #{orderId,jdbcType=INTEGER}
update t_hibernate_order
set order_no = #{orderNo,jdbcType=VARCHAR}
where order_id = #{orderId,jdbcType=INTEGER}
orderBiz.java
package com.zhouzhou.biz;
import com.zhouzhou.model.Order;
import com.zhouzhou.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.zhouzhou.biz.impl;
import com.zhouzhou.biz.OrderBiz;
import com.zhouzhou.mapper.OrderMapper;
import com.zhouzhou.model.Order;
import com.zhouzhou.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
order_item_id, product_id, quantity, oid
delete from t_hibernate_order_item
where order_item_id = #{orderItemId,jdbcType=INTEGER}
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 into t_hibernate_order_item
order_item_id,
product_id,
quantity,
oid,
#{orderItemId,jdbcType=INTEGER},
#{productId,jdbcType=INTEGER},
#{quantity,jdbcType=INTEGER},
#{oid,jdbcType=INTEGER},
update t_hibernate_order_item
product_id = #{productId,jdbcType=INTEGER},
quantity = #{quantity,jdbcType=INTEGER},
oid = #{oid,jdbcType=INTEGER},
where order_item_id = #{orderItemId,jdbcType=INTEGER}
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}
orderitemBiz.java
package com.zhouzhou.biz;
import com.zhouzhou.model.OrderItem;
import com.zhouzhou.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);
}
首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,这两张表对应的model与mapper
HbookMapper.xml
book_id, book_name, price
delete from t_hibernate_book
where book_id = #{bookId,jdbcType=INTEGER}
insert into t_hibernate_book (book_id, book_name, price
)
values (#{bookId,jdbcType=INTEGER}, #{bookName,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
insert into t_hibernate_book
book_id,
book_name,
price,
#{bookId,jdbcType=INTEGER},
#{bookName,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
update t_hibernate_book
book_name = #{bookName,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
where book_id = #{bookId,jdbcType=INTEGER}
update t_hibernate_book
set book_name = #{bookName,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where book_id = #{bookId,jdbcType=INTEGER}
HbookCategoryBiz.java
package com.zhouzhou.biz;
import com.zhouzhou.model.HbookCategory;
import com.zhouzhou.model.vo.CategoryVo;
import com.zhouzhou.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.zhouzhou.biz.impl;
import com.zhouzhou.biz.HbookCategoryBiz;
import com.zhouzhou.mapper.HbookCategoryMapper;
import com.zhouzhou.model.HbookCategory;
import com.zhouzhou.model.vo.CategoryVo;
import com.zhouzhou.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.zhouzhou.biz.impl;
import com.zhouzhou.biz.HbookCategoryBiz;
import com.zhouzhou.model.vo.CategoryVo;
import com.zhouzhou.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);
}
}