mybatis之关联关系映射
1.一对多
首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper
实体类:
package com.lihao.ssm.model.vo;
import com.lihao.ssm.model.Order;
import com.lihao.ssm.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
/**
* @author 浩梦溪
* @site www.lihao.com
* @company
* @create 2019-09-27 11:03
*/
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;
}
}
package com.lihao.ssm.model.vo;
import com.lihao.ssm.model.Order;
import com.lihao.ssm.model.OrderItem;
/**
* @author 浩梦溪
* @site www.lihao.com
* @company
* @create 2019-09-27 11:05
*/
public class OrderItemVo extends OrderItem {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
OrderMapper.xml:
<resultMap id="OrderVoMap" type="com.lihao.ssm.model.vo.OrderVo" >
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<collection property="orderItems" ofType="com.lihao.ssm.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="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>
OrderMapper.java:
package com.lihao.ssm.mapper;
import com.lihao.ssm.model.Order;
import com.lihao.ssm.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 queryOrderVoByOrderId(@Param("orderId") Integer orderId);
}
OrderItemMapper.xml:
<resultMap id="OrderVoMap" type="com.lihao.ssm.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.lihao.ssm.model.Order">
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
</association>
</resultMap>
<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>
OrderItemMapper.java:
package com.lihao.ssm.mapper;
import com.lihao.ssm.model.OrderItem;
import com.lihao.ssm.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 queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
}
One2ManyService.java:
package com.lihao.ssm.service;
import com.lihao.ssm.model.vo.OrderItemVo;
import com.lihao.ssm.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author 浩梦溪
* @site www.lihao.com
* @company
* @create 2019-09-27 11:10
*/
public interface One2ManyService {
OrderVo queryOrderVoByOrderId(Integer orderId);
OrderItemVo queryOrderItemVoByOrderItemId(Integer OrderItemId);
}
package com.lihao.ssm.service.impl;
import com.lihao.ssm.mapper.OrderItemMapper;
import com.lihao.ssm.mapper.OrderMapper;
import com.lihao.ssm.model.vo.OrderItemVo;
import com.lihao.ssm.model.vo.OrderVo;
import com.lihao.ssm.service.One2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author 浩梦溪
* @site www.lihao.com
* @company
* @create 2019-09-27 11:17
*/
@Service
public class One2ManyServiceImpl implements One2ManyService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Override
public OrderVo queryOrderVoByOrderId(Integer orderId) {
return orderMapper.queryOrderVoByOrderId(orderId);
}
@Override
public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
}
}
One2ManyServiceImplTest.java:
package com.lihao.ssm.service.impl;
import com.lihao.ssm.SpringJunitBaseTest;
import com.lihao.ssm.model.Order;
import com.lihao.ssm.model.OrderItem;
import com.lihao.ssm.model.vo.OrderItemVo;
import com.lihao.ssm.model.vo.OrderVo;
import com.lihao.ssm.service.One2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
/**
* @author 浩梦溪
* @site www.lihao.com
* @company
* @create 2019-09-27 11:20
*/
public class One2ManyServiceImplTest extends SpringJunitBaseTest {
@Autowired
private One2ManyService one2ManyService;
@Test
public void queryOrderVoByOrderId() {
OrderVo orderVo = this.one2ManyService.queryOrderVoByOrderId(10);
System.out.println(orderVo);
for (OrderItem orderItem : orderVo.getOrderItems()) {
System.out.println(orderItem);
}
}
@Test
public void queryOrderItemVoByOrderItemId() {
OrderItemVo orderItemVo = this.one2ManyService.queryOrderItemVoByOrderItemId(49);
System.out.println(orderItemVo);
System.out.println(orderItemVo.getOrder());
}
}
多对多
首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,这两张表对应的model与mapper
实体类:
package com.lihao.ssm.model.vo;
import com.lihao.ssm.model.Category;
import com.lihao.ssm.model.Hbook;
import java.util.ArrayList;
import java.util.List;
/**
* @author 浩梦溪
* @site www.lihao.com
* @company
* @create 2019-09-27 11:51
*/
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;
}
}
package com.lihao.ssm.model.vo;
import com.lihao.ssm.model.Category;
import com.lihao.ssm.model.Hbook;
import java.util.ArrayList;
import java.util.List;
/**
* @author 浩梦溪
* @site www.lihao.com
* @company
* @create 2019-09-27 11:52
*/
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;
}
}
HbookCategoryMapper.xml:
<resultMap id="HbookVoMap" type="com.lihao.ssm.model.vo.HbookVo">
<result property="bookId" column="book_id"></result>
<result property="bookName" column="book_name"></result>
<collection property="categories" ofType="com.lihao.ssm.model.Category">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
</collection>
</resultMap>
<resultMap id="CategoryVoMap" type="com.lihao.ssm.model.vo.CategoryVo">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
<collection property="hbooks" ofType="com.lihao.ssm.model.Hbook">
<result property="bookId" column="book_id"></result>
<result property="bookName" column="book_name"></result>
</collection>
</resultMap>
<select id="queryByBookId" 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 = #{bookId}
</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>
HbookCategoryMapper.java:
package com.lihao.ssm.mapper;
import com.lihao.ssm.model.Category;
import com.lihao.ssm.model.Hbook;
import com.lihao.ssm.model.HbookCategory;
import com.lihao.ssm.model.vo.CategoryVo;
import com.lihao.ssm.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@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 queryByBookId(@Param("bookId") Integer bookId);
CategoryVo queryByCid(@Param("cid") Integer cid);
}
Many2ManyService.java:
package com.lihao.ssm.service;
import com.lihao.ssm.model.Category;
import com.lihao.ssm.model.Hbook;
import com.lihao.ssm.model.vo.CategoryVo;
import com.lihao.ssm.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
/**
* @author 浩梦溪
* @site www.lihao.com
* @company
* @create 2019-09-27 11:59
*/
public interface Many2ManyService {
HbookVo queryByBookId(Integer bookId);
CategoryVo queryByCid(Integer cid);
}
package com.lihao.ssm.service.impl;
import com.lihao.ssm.mapper.HbookCategoryMapper;
import com.lihao.ssm.model.Category;
import com.lihao.ssm.model.Hbook;
import com.lihao.ssm.model.vo.CategoryVo;
import com.lihao.ssm.model.vo.HbookVo;
import com.lihao.ssm.service.Many2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author 浩梦溪
* @site www.lihao.com
* @company
* @create 2019-09-27 12:00
*/
@Service
public class Many2ManyServiceImpl implements Many2ManyService {
@Autowired
private HbookCategoryMapper hbookCategoryMapper;
@Override
public HbookVo queryByBookId(Integer bookId) {
return hbookCategoryMapper.queryByBookId(bookId);
}
@Override
public CategoryVo queryByCid(Integer cid) {
return hbookCategoryMapper.queryByCid(cid);
}
}
Many2ManyServiceImplTest.java:
package com.lihao.ssm.service.impl;
import com.lihao.ssm.SpringJunitBaseTest;
import com.lihao.ssm.model.Category;
import com.lihao.ssm.model.Hbook;
import com.lihao.ssm.model.vo.CategoryVo;
import com.lihao.ssm.model.vo.HbookVo;
import com.lihao.ssm.service.Many2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
/**
* @author 浩梦溪
* @site www.lihao.com
* @company
* @create 2019-09-27 12:01
*/
public class Many2ManyServiceImplTest extends SpringJunitBaseTest {
@Autowired
private Many2ManyService many2ManyService;
@Test
public void queryByBookId() {
HbookVo hbookVo = many2ManyService.queryByBookId(8);
System.out.println(hbookVo);
for (Category category : hbookVo.getCategories()) {
System.out.println(category);
}
}
@Test
public void queryByCid() {
CategoryVo categoryVo = many2ManyService.queryByCid(8);
System.out.println(categoryVo);
for (Hbook hbook : categoryVo.getHbooks()) {
System.out.println(hbook);
}
}
}