标签:array efi main tor parameter count map etc his
1.通过generatorConfig.xml中首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper
<table schema="" tableName="t_hibernate_Book" domainObjectName="HBook" 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> <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="BookCategory" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> </table>
Order.java(订单实体)
package com.hmc.ssm.model;
public class Order {
private Integer orderId;
private String orderNo;
public Order(Integer orderId, String orderNo) {
this.orderId = orderId;
this.orderNo = orderNo;
}
public Order() {
super();
}
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
@Override
public String toString() {
return "Order{" +
"orderId=" + orderId +
", orderNo=‘" + orderNo + ‘\‘‘ +
‘}‘;
}
}
OrderMapper
@Repository public interface OrderMapper { int deleteByPrimaryKey(Integer orderId); int insert(Order record); int insertSelective(Order record); Order selectByPrimaryKey(Integer orderId); int updateByPrimaryKeySelective(Order record); int updateByPrimaryKey(Order record); List<OrderVo> selectByOrderId(@Param("orderId") Integer orderId); }
OrderMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.ningjie.ssm.mapper.OrderMapper" > <resultMap id="BaseResultMap" type="com.hmc.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.hmc.ssm.model.vo.OrderVo"> <result property="orderId" column="order_id"></result> <result property="orderNo" column="order_no"></result> <collection property="orderItems" ofType="com.hmc.ssm.model.OrderItem"> <result property="orderItemId" column="order_item_id"></result> <result property="productId" column="product_id"></result> <result property="quantity" column="quantity"></result> <result property="oid" column="oid"></result> </collection> </resultMap> <sql id="Base_Column_List" > order_id, order_no </sql> <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.hmc.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.hmc.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.hmc.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.hmc.ssm.model.Order" > update t_hibernate_order set order_no = #{orderNo,jdbcType=VARCHAR} where order_id = #{orderId,jdbcType=INTEGER} </update> <select id="selectByOrderId" 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.oid=#{orderId} </select> </mapper>
用下面的OrderVo类继承订单
用一个list集合来放置多方(
OrderItem
)
public class OrderVo extends Order { private List<OrderItem> orderItems=new ArrayList<>(); public List<OrderItem> getOrderItems() { return orderItems; } public void setOrderItems(List<OrderItem> orderItems) { this.orderItems = orderItems; } }
OrderItem
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;
}
@Override
public String toString() {
return "OrderItem{" +
"orderItemId=" + orderItemId +
", productId=" + productId +
", quantity=" + quantity +
", oid=" + oid +
‘}‘;
}
}
OrderItemVo.java
public class OrderItemVo extends OrderItem {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
OrderItemMapper
@Repository public interface OrderItemMapper { int deleteByPrimaryKey(Integer orderItemId); int insert(OrderItem record); int insertSelective(OrderItem record); OrderItem selectByPrimaryKey(Integer orderItemId); int updateByPrimaryKeySelective(OrderItem record); int updateByPrimaryKey(OrderItem record); List<OrderItemVo> selectByOrderItemId(@Param("orderItemId") Integer orderItemId); }
OrderMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.hmc.ssm.mapper.OrderItemMapper" > <resultMap id="BaseResultMap" type="com.hmc.ssm.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="orderItemVoMap" type="com.hmc.ssm.model.vo.OrderItemVo"> <result property="orderItemId" column="order_item_id"></result> <result property="productId" column="product_id"></result> <result property="quantity" column="quantity"></result> <result property="oid" column="oid"></result> <association property="order" javaType="com.hmc.ssm.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="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.hmc.ssm.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.hmc.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=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.hmc.ssm.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.hmc.ssm.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> <select id="selectByOrderItemId" 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> </mapper>
OneToManyService
public interface OneToManyService { List<OrderItemVo> selectByOrderItemId(Integer orderItemId); List<OrderVo> selectByOrderId(Integer orderId); }
OneToManyServiceImpl
@Service public class OneToManyServiceImpl implements OneToManyService { @Autowired private OrderMapper orderMapper; @Autowired private OrderItemMapper orderItemMapper; @Override public List<OrderItemVo> selectByOrderItemId(Integer orderItemId) { return orderItemMapper.selectByOrderItemId(orderItemId); } @Override public List<OrderVo> selectByOrderId(Integer orderId) { return orderMapper.selectByOrderId(orderId); } }
OneToManyServiceImplTest
public class OneToManyServiceImplTest extends SpringBaseTest { @Autowired private OneToManyService oneToManyService; @Test public void selectByOrderItemId() { List<OrderItemVo> orderItemVos = oneToManyService.selectByOrderItemId(36); OrderItemVo orderItemVo = orderItemVos.get(0); System.out.println(orderItemVo); System.out.println(orderItemVo.getOrder()); } @Test public void selectByOrderId() { List<OrderVo> orderVos = oneToManyService.selectByOrderId(8); OrderVo orderVo = orderVos.get(0); System.out.println(orderVo); for (OrderItem orderItem : orderVo.getOrderItems()) { System.out.println(orderItem); } } }
首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,这两张表对应的model与mapper
Hbook
public class HBook {
private Integer bookId;
private String bookName;
private Float price;
public HBook(Integer bookId, String bookName, Float price) {
this.bookId = bookId;
this.bookName = bookName;
this.price = price;
}
public HBook() {
super();
}
public Integer getBookId() {
return bookId;
}
public void setBookId(Integer bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
@Override
public String toString() {
return "HBook{" +
"bookId=" + bookId +
", bookName=‘" + bookName + ‘\‘‘ +
", price=" + price +
‘}‘;
}
}
HbookVo
public class HbookVo extends HBook { private List<Category> categories=new ArrayList<>(); public List<Category> getCategories() { return categories; } public void setCategories(List<Category> categories) { this.categories = categories; } }
HbookMapper
@Repository
public interface HBookMapper {
int deleteByPrimaryKey(Integer bookId);
int insert(HBook record);
int insertSelective(HBook record);
HBook selectByPrimaryKey(Integer bookId);
int updateByPrimaryKeySelective(HBook record);
int updateByPrimaryKey(HBook record);
HbookVo selectByBid(@Param("bid") Integer bid);
}
HbookMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.ningjie.ssm.mapper.HBookMapper" > <resultMap id="BaseResultMap" type="com.hmc.ssm.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> <resultMap id="HbookVoMap" type="com.hmc.ssm.model.vo.HbookVo"> <result property="bookId" column="book_id"></result> <result property="bookName" column="book_name"></result> <result property="price" column="price"></result> <collection property="categories" ofType="com.hmc.ssm.model.Category"> <result property="categoryId" column="category_id"></result> <result property="categoryName" column="category_Name"></result> </collection> </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.hmc.ssm.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.hmc.ssm.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.hmc.ssm.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.hmc.ssm.model.HBook" > update t_hibernate_book set book_name = #{bookName,jdbcType=VARCHAR}, price = #{price,jdbcType=REAL} where book_id = #{bookId,jdbcType=INTEGER} </update> <select id="selectByBid" 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.bid=c.category_id and b.book_id=#{bid} </select> </mapper>
Category
public class Category {
private Integer categoryId;
private String categoryName;
public Category(Integer categoryId, String categoryName) {
this.categoryId = categoryId;
this.categoryName = categoryName;
}
public Category() {
super();
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
@Override
public String toString() {
return "Category{" +
"categoryId=" + categoryId +
", categoryName=‘" + categoryName + ‘\‘‘ +
‘}‘;
}
}
CategoryVo
public class CategoryVo extends Category { private List<HBook> hBooks=new ArrayList<>(); public List<HBook> gethBooks() { return hBooks; } public void sethBooks(List<HBook> hBooks) { this.hBooks = hBooks; } }
CategoryMapper
@Repository
public interface CategoryMapper {
int deleteByPrimaryKey(Integer categoryId);
int insert(Category record);
int insertSelective(Category record);
Category selectByPrimaryKey(Integer categoryId);
int updateByPrimaryKeySelective(Category record);
int updateByPrimaryKey(Category record);
CategoryVo selectByCid(@Param("cid") Integer cid);
}
CategoryMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.ningjie.ssm.mapper.CategoryMapper" > <resultMap id="BaseResultMap" type="com.hmc.ssm.model.Category" > <constructor > <idArg column="category_id" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="category_name" jdbcType="VARCHAR" javaType="java.lang.String" /> </constructor> </resultMap> <resultMap id="CategoryVoMap" type="com.hmc.ssm.model.vo.CategoryVo"> <result property="categoryId" column="category_id"></result> <result property="categoryName" column="category_Name"></result> <collection property="hBooks" ofType="com.hmc.ssm.model.HBook"> <result property="bookId" column="book_id"></result> <result property="bookName" column="book_name"></result> <result property="price" column="price"></result> </collection> </resultMap> <sql id="Base_Column_List" > category_id, category_name </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from t_hibernate_category where category_id = #{categoryId,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from t_hibernate_category where category_id = #{categoryId,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.hmc.ssm.model.Category" > insert into t_hibernate_category (category_id, category_name) values (#{categoryId,jdbcType=INTEGER}, #{categoryName,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="com.hmc.ssm.model.Category" > insert into t_hibernate_category <trim prefix="(" suffix=")" suffixOverrides="," > <if test="categoryId != null" > category_id, </if> <if test="categoryName != null" > category_name, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="categoryId != null" > #{categoryId,jdbcType=INTEGER}, </if> <if test="categoryName != null" > #{categoryName,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.hmc.ssm.model.Category" > update t_hibernate_category <set > <if test="categoryName != null" > category_name = #{categoryName,jdbcType=VARCHAR}, </if> </set> where category_id = #{categoryId,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.hmc.ssm.model.Category" > update t_hibernate_category set category_name = #{categoryName,jdbcType=VARCHAR} where category_id = #{categoryId,jdbcType=INTEGER} </update> <select id="selectByCid" 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.bid=c.category_id and c.category_id=#{cid} </select> </mapper>
ManyToManyService
public interface ManyToManyService {
CategoryVo selectByCid(Integer cid);
HbookVo selectByBid(Integer bid);
}
ManyToManyServiceImpl
@Service
public class ManyToManyServiceImpl implements ManyToManyService {
@Autowired
private HBookMapper hBookMapper;
@Autowired
private CategoryMapper categoryMapper;
@Override
public CategoryVo selectByCid(Integer cid) {
return categoryMapper.selectByCid(cid);
}
@Override
public HbookVo selectByBid(Integer bid) {
return hBookMapper.selectByBid(bid);
}
}
ManyToManyServiceImplTest
public class ManyToManyServiceImplTest extends SpringBaseTest {
@Autowired
private ManyToManyService manyToManyService;
@Test
public void selectByCid() {
CategoryVo categoryVo = this.manyToManyService.selectByCid(8);
System.out.println(categoryVo);
for (HBook gethBook : categoryVo.gethBooks()) {
System.out.println(gethBook);
}
}
@Test
public void selectByBid() {
HbookVo hbookVo = manyToManyService.selectByBid(9);
System.out.println(hbookVo);
for (Category category : hbookVo.getCategories()) {
System.out.println(category);
}
}
}
标签:array efi main tor parameter count map etc his
原文地址:https://www.cnblogs.com/xmf3628/p/11728965.html