Mybatis的关联关系
1、通过 mybatis-generator 插件生成dao、mapper、model
1.1 :配置 mybatis-generator 插件生成文件位置
1.2 :修改 generatorConfig.xml 配置文件的生成目录(mapper和model)及对应生成关系
2、修改 Customer、Order 实体类
2.1:实现序列化接口
2.2:建立实体映射关联关系(一对多、多对一)
#一对多:一个客户对应多个订单
private List<Order> orders=new ArrayList<Order>();
#多对一:多个订单对应一个客户(一个订单对应一个客户)
private Customer customer;
3、配置 mybatis 关联映射
一对多关联关系
一个订单对应多个订单项
先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item(逆向生成,Mybatis入门已经有讲到,点击传送)
OrderVo
package com.Tang.model.vo;
import com.Tang.model.Order;
import com.Tang.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
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.Tang.model.vo;
import com.Tang.model.Order;
import com.Tang.model.OrderItem;
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.Tang.model.vo.OrderVo">
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<collection property="orderItems" ofType="com.Tang.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>
<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 = #{orderId}
</select>
OrderMapper.java
package com.Tang.mapper;
import com.Tang.model.Order;
import com.Tang.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);
}
OrderItemMapper.xml
<resultMap id="orderItemVoMap" type="com.Tang.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.Tang.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>
OrderItemMapper.java
package com.Tang.mapper;
import com.Tang.model.OrderItem;
import com.Tang.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);
}
One2ManyService.java
package com.Tang.service;
import com.Tang.model.Order;
import com.Tang.model.vo.OrderItemVo;
import com.Tang.model.vo.OrderVo;
public interface One2ManyService {
OrderVo selectByOid(Integer oid);
OrderItemVo selectByOrderItemId(Integer orderItemId);
}
One2ManyServiceImpl
package com.Tang.service.impl;
import com.Tang.mapper.OrderItemMapper;
import com.Tang.mapper.OrderMapper;
import com.Tang.model.vo.OrderItemVo;
import com.Tang.model.vo.OrderVo;
import com.Tang.service.One2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class One2ManyServiceImpl implements One2ManyService {
@Autowired
private OrderMapper orderMapper;
private OrderItemMapper orderItemMapper;
@Override
public OrderVo selectByOid(Integer oid) {
return orderMapper.selectByOid(oid);
}
@Override
public OrderItemVo selectByOrderItemId(Integer orderItemId) {
return orderItemMapper.selectByOrderItemId(orderItemId);
}
}
One2ManyServiceImplTest.java
package com.Tang.service.impl;
import com.Tang.SpringJunitBaseTest;
import com.Tang.model.Order;
import com.Tang.model.OrderItem;
import com.Tang.model.vo.OrderItemVo;
import com.Tang.model.vo.OrderVo;
import com.Tang.service.One2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
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(书籍表、书籍类别表、中间表)
多对多就是指一本书可以有多个类别,一个类别又可以对应多本书
HbookVo
package com.Tang.model.vo;
import com.Tang.model.Category;
import com.Tang.model.Hbook;
import java.util.ArrayList;
import java.util.List;
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;
}
}
CategoryVo
package com.Tang.model.vo;
import com.Tang.model.Category;
import com.Tang.model.Hbook;
import java.util.ArrayList;
import java.util.List;
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.Tang.model.vo.HbookVo">
<result property="bookId" column="book_id"></result>
<result property="bookName" column="book_name"></result>
<collection property="categories" ofType="com.Tang.model.Category">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
</collection>
</resultMap>
<resultMap id="CategoryVoMap" type="com.Tang.model.vo.CategoryVo">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
<collection property="hbooks" ofType="com.Tang.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
package com.Tang.mapper;
import com.Tang.model.Category;
import com.Tang.model.Hbook;
import com.Tang.model.HbookCategory;
import com.Tang.model.vo.CategoryVo;
import com.Tang.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
package com.Tang.service;
import com.Tang.model.Category;
import com.Tang.model.Hbook;
import com.Tang.model.vo.CategoryVo;
import com.Tang.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
public interface Many2ManyService {
HbookVo queryByBookId(Integer bookId);
CategoryVo queryByCid(Integer cid);
}
Many2ManyServiceImpl
package com.Tang.service.impl;
import com.Tang.mapper.HbookCategoryMapper;
import com.Tang.model.Category;
import com.Tang.model.Hbook;
import com.Tang.model.vo.CategoryVo;
import com.Tang.model.vo.HbookVo;
import com.Tang.service.Many2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@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
package com.Tang.service.impl;
import com.Tang.SpringJunitBaseTest;
import com.Tang.model.Category;
import com.Tang.model.Hbook;
import com.dTangj.model.vo.CategoryVo;
import com.Tang.model.vo.HbookVo;
import com.Tang.service.Many2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
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);
}
}
}
上一篇: Netty源码分析之NioEventLoop执行流程
下一篇: nginx配置ssl双向验证