一对多
首先先用逆向生成工具生成 t_hibernate_order、t_hibernate_order_item
在配置文件 generatorConfig.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>
为不同的表增加不同的属性
OrderVo
package com.lst.model.vo;
import com.lst.model.Order;
import com.lst.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
/**
* @create 2019-09-26 12:02
*/
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;
}
}
OrderItemVo
package com.lst.model.vo;
import com.lst.model.Order;
import com.lst.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
/**
* @create 2019-09-26 12:03
*/
public class OrderItemVo extends OrderItem {
private List<Order> order = new ArrayList<>();
public List<Order> getOrder() {
return order;
}
public void setOrders(List<Order> order) {
this.order = order;
}
}
配置映射文件
OrderMapper
加入 OrderVo selectByOid(@Param(“oid”) Integer oid);
package com.lst.mapper;
import com.lst.model.Order;
import com.lst.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@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);
OrderVo selectByOid(@Param("oid") Integer oid);
}
OrderMapper.xml
<resultMap id="OrderVoMap" type="com.lst.model.vo.OrderVo" >
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<collection property="orderItems" ofType="com.lst.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>
<select id="selectByOid" 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 = #{oid}
</select>
OrderItemMapper
加入OrderItemVo selectByOrderItemId(@Param(“orderItemId”) Integer orderItemId);
package com.lst.mapper;
import com.lst.model.OrderItem;
import com.lst.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@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);
OrderItemVo selectByOrderItemId(@Param("orderItemId") Integer orderItemId);
}
OrderItemMapper.xml
<resultMap id="OrderItemVoMap" type="com.lst.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.lst.model.Order">
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
</association>
</resultMap>
<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>
测试
One2ManyService
package com.lst.service;
import com.lst.model.vo.OrderItemVo;
import com.lst.model.vo.OrderVo;
/**
* @create 2019-09-26 22:09
*/
public interface One2ManyService {
OrderVo selectByOid(Integer oid);
OrderItemVo selectByOrderItemId(Integer orderItemId);
}
One2ManyServiceImpl
package com.lst.service.impl;
import com.lst.mapper.OrderItemMapper;
import com.lst.mapper.OrderMapper;
import com.lst.model.vo.OrderItemVo;
import com.lst.model.vo.OrderVo;
import com.lst.service.One2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @create 2019-09-26 22:10
*/
@Service
public class One2ManyServiceImpl implements One2ManyService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Override
public OrderVo selectByOid(Integer oid) {
return orderMapper.selectByOid(oid);
}
@Override
public OrderItemVo selectByOrderItemId(Integer orderItemId) {
return orderItemMapper.selectByOrderItemId(orderItemId);
}
}
One2ManyServiceImplTest
package com.lst.service.impl;
import com.lst.SpringBaseTest;
import com.lst.model.OrderItem;
import com.lst.model.vo.OrderItemVo;
import com.lst.model.vo.OrderVo;
import com.lst.service.One2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @create 2019-09-26 22:12
*/
public class One2ManyServiceImplTest extends SpringBaseTest {
@Autowired
private One2ManyService one2ManyService;
@Test
public void selectByOid() {
OrderVo orderVo = one2ManyService.selectByOid(1);
System.out.println(orderVo);
for (OrderItem orderItem : orderVo.getOrderItems()) {
System.out.println(orderItem);
}
}
@Test
public void selectByOrderItemId() {
OrderItemVo orderItemVo = one2ManyService.selectByOrderItemId(1);
System.out.println(orderItemVo);
System.out.println(orderItemVo.getOrder());
}
}
效果展示
selectByOid
selectByOrderItemId
多对多
首先先用逆向生成工具生成 t_hibernate_book、t_hibernate_book_category、t_hibernate_category
在配置文件 generatorConfig.xml 中添加以下代码生成实体类与映射文件
<table schema="" tableName="t_hibernate_book" domainObjectName="Hbook"
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>
<table schema="" tableName="t_hibernate_category" domainObjectName="Category"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
为不同的表增加不同的属性
CategoryVo
package com.lst.model.vo;
import com.lst.model.Category;
import com.lst.model.Hbook;
import java.util.ArrayList;
import java.util.List;
/**
* @create 2019-09-26 22:42
*/
public class CategoryVo extends Category {
List<Hbook> hbooks = new ArrayList<>();
public List<Hbook> getHbooks() {
return hbooks;
}
public void setHbooks(List<Hbook> hbooks) {
this.hbooks = hbooks;
}
}
HbookVo
package com.lst.model.vo;
import com.lst.model.Category;
import com.lst.model.Hbook;
import java.util.ArrayList;
import java.util.List;
/**
* @create 2019-09-26 22:41
*/
public class HbookVo extends Hbook {
List<Category> categories = new ArrayList<>();
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
}
配置映射文件
多对多的映射文件我们就放在中间表里,中间表的数据基本上不会动的,所以说是稳定的。
HbookCategoryMapper
加入HbookVo queryByBid(@Param(“bid”) Integer bid);
CategoryVo queryByCid(@Param(“cid”) Integer cid);
package com.lst.mapper;
import com.lst.model.HbookCategory;
import com.lst.model.vo.CategoryVo;
import com.lst.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface HbookCategoryMapper {
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 queryByBid(@Param("bid") Integer bid);
CategoryVo queryByCid(@Param("cid") Integer cid);
}
HbookCategoryMapper.xml
<resultMap id="HbookVoMap" type="com.lst.model.vo.HbookVo">
<result column="book_id" property="bookId" ></result>
<result column="book_name" property="bookName" ></result>
<result column="price" property="price" ></result>
<collection property="categories" ofType="com.lst.model.Category">
<result column="category_id" property="categoryId"></result>
<result column="category_name" property="categoryName"></result>
</collection>
</resultMap>
<resultMap id="CategoryVoMap" type="com.lst.model.vo.CategoryVo">
<result column="category_id" property="categoryId"></result>
<result column="category_name" property="categoryName"></result>
<collection property="hbooks" ofType="com.lst.model.Hbook">
<result column="book_id" property="bookId" ></result>
<result column="book_name" property="bookName" ></result>
<result column="price" property="price" ></result>
</collection>
</resultMap>
<select id="queryByBid" 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 = #{bid}
</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>
测试
HbookCategoryService
package com.lst.service;
import com.lst.model.vo.CategoryVo;
import com.lst.model.vo.HbookVo;
import org.springframework.stereotype.Repository;
/**
* @create 2019-09-26 22:48
*/
@Repository
public interface HbookCategoryService {
HbookVo queryByBid(Integer bid);
CategoryVo queryByCid(Integer cid);
}
HbookCategoryServiceImpl
package com.lst.service.impl;
import com.lst.mapper.HbookCategoryMapper;
import com.lst.model.vo.CategoryVo;
import com.lst.model.vo.HbookVo;
import com.lst.service.HbookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @create 2019-09-26 22:49
*/
@Service
public class HbookCategoryServiceImpl implements HbookCategoryService {
@Autowired
private HbookCategoryMapper hbookCategoryMapper;
@Override
public HbookVo queryByBid(Integer bid) {
return hbookCategoryMapper.queryByBid(bid);
}
@Override
public CategoryVo queryByCid(Integer cid) {
return hbookCategoryMapper.queryByCid(cid);
}
}
HbookCategoryServiceImplTest
package com.lst.service.impl;
import com.lst.SpringBaseTest;
import com.lst.model.Hbook;
import com.lst.model.vo.CategoryVo;
import com.lst.model.vo.HbookVo;
import com.lst.service.HbookCategoryService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @create 2019-09-26 22:53
*/
public class HbookCategoryServiceImplTest extends SpringBaseTest {
@Autowired
private HbookCategoryService hbookCategoryService;
@Test
public void queryByBid() {
HbookVo hbookVo = hbookCategoryService.queryByBid(4);
System.out.println(hbookVo);
for (Object c : hbookVo.getCategories()){
System.out.println(c);
}
}
@Test
public void queryByCid() {
CategoryVo categoryVo = hbookCategoryService.queryByCid(3);
System.out.println(categoryVo);
for (Hbook hbook : categoryVo.getHbooks()) {
System.out.println(hbook);
}
}
}
效果展示
queryByBid
queryByCid
over。。。