首先将mybatis的关联关系与hibernate的关联关系做对比:
hibernate的关联关系:
mybatis的关联关系比hibernate的关联关系简单些,就看下面的对比吧
今天需要使用的表有:订单表,订单项表,书籍表,书籍类别表,书籍类别中间表
一、一对多关联关系
1、逆向生成工具生成 t_hibernate_order、t_hibernate_order_item、t_hibernate_book、t_hibernate_category、t_hibernate_book_category
①、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>
<table schema="" tableName="t_hibernate_book" domainObjectName="Hbook"
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="HbookCategory"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
②、在generatorConfig.xml页面运行上方mybatis-generator
生成成功后:
2、创建实体类/映射/mapper接口
①、OrderVo 继承了Order的所有属性,最终查询的结果封装到OrderVo 里面
package com.mwy.vo;
import com.mwy.model.Order;
import com.mwy.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 继承了OrderItem的所有属性,最终查询的结果封装到OrderItemVo 里面
package com.mwy.vo;
import com.mwy.model.Order;
import com.mwy.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
<!--映射-->
<!--property:实体类的属性 column:表字段-->
<resultMap id="OrderVoMap" type="com.mwy.vo.OrderVo" >
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<collection property="orderItems" ofType="com.mwy.model.OrderItem">
<result property="orderItemId" column="order_item_id"/>
<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>
(1)做OrderVoMap映射:
(2) 将查询语句放入对应的select里面
④、OrderItemMapper.xml
<resultMap id="OrderVoMap" type="com.mwy.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.mwy.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>
(1)做OrderVoMap映射:
(2) 将查询语句放入对应的select里面
⑤、写mapper接口
(1)OrderMapper.java
OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);
(2)OrderItemMapper.java
OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
3、做测试
①、One2ManyService.java
package com.mwy.service;
import com.mwy.vo.OrderItemVo;
import com.mwy.vo.OrderVo;
public interface One2ManyService {
//通过查询订单关联查询出对应的订单项信息
OrderVo queryOrderVoByOrderId(Integer orderId);
//查询订单项,关联查询出所属的订单
OrderItemVo queryOrderItemVoByOrderItemId(Integer OrderItemId);
}
生成实现接口类(点击One2ManyService ,然后Alt+Enter):
②、One2ManyServiceImpl:
package com.mwy.service.impl;
import com.mwy.mapper.OrderItemMapper;
import com.mwy.mapper.OrderMapper;
import com.mwy.service.One2ManyService;
import com.mwy.vo.OrderItemVo;
import com.mwy.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@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);
}
}
生成测试类(点击One2ManyServiceImpl ,然后Alt+Enter):
③、One2ManyServiceImplTest
与SpringJUnit整合到一起
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"})
给order.java增加toString方法
加入注解,不然service没值:
package com.mwy.service.impl;
import com.mwy.model.OrderItem;
import com.mwy.service.One2ManyService;
import com.mwy.vo.OrderItemVo;
import com.mwy.vo.OrderVo;
import junit.framework.TestCase;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class One2ManyServiceImplTest extends TestCase {
@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());
}
}
查询订单为10,并且显示其订单项的结果:
查询订单项为49,并且显示其订单的结果:
二、 多对多关联关系
逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,
1、实体类/映射/mapper接口
①、实体类
(1)HbookVo
package com.mwy.vo;
import com.mwy.model.Category;
import com.mwy.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;
}
}
(2)、CategoryVo
package com.mwy.vo;
import com.mwy.model.Category;
import com.mwy.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;
}
}
②、映射、查询
使用了两个一对多,与实体类并没有强制关联性
(1)HbookCategoryMapper.xml
<resultMap id="HbookVoMap" type="com.mwy.vo.HbookVo">
<result property="bookId" column="book_id"></result>
<result property="bookName" column="book_name"></result>
<collection property="categories" ofType="com.mwy.model.Category">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
</collection>
</resultMap>
<resultMap id="CategoryVoMap" type="com.mwy.vo.CategoryVo">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
<collection property="hbooks" ofType="com.mwy.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:
HbookVo queryByBookId(@Param("bookId") Integer bookId); CategoryVo queryByCid(@Param("cid") Integer cid);
2、做测试
①、Many2ManyService .java
package com.mwy.service;
import com.mwy.vo.CategoryVo;
import com.mwy.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
public interface Many2ManyService {
HbookVo queryByBookId(Integer bookId);
CategoryVo queryByCid(Integer cid);
}
生成实现接口类(点击Many2ManyService ,然后Alt+Enter):
②、Many2ManyServiceImpl :交给Service进行管理
package com.mwy.service.impl;
import com.mwy.mapper.HbookCategoryMapper;
import com.mwy.service.Many2ManyService;
import com.mwy.vo.CategoryVo;
import com.mwy.vo.HbookVo;
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);
}
}
生成测试类(点击Many2ManyServiceImpl ,然后Alt+Enter):
③、Many2ManyServiceImplTest
测试类与SpringJUnit整合到一起
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"})
package com.mwy.service.impl;
import com.mwy.model.Category;
import com.mwy.model.Hbook;
import com.mwy.service.Many2ManyService;
import com.mwy.vo.CategoryVo;
import com.mwy.vo.HbookVo;
import junit.framework.TestCase;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class Many2ManyServiceImplTest extends TestCase {
@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);
}
}
}
记得给实体类加入toString
结果:
结束!!!