赞
踩
目录
1、Mybatis的关联关系和Hibernate的关联关系大同小异。
3、关于Mybatis一对多的关系配置步骤:(以Order和OrderItem为例)
3.1:先将对应的表建立好,并且用代码生成器生成对应的mapper和model(注意不要生成重复的mapper和model)
3.2:在对应的orderMpper.xml和orderItemMpper.xml中配置resultMap节点以及对应的方法:
3.3:在对应的OrderMapper.java和OrderItemsMapper.java中配置出对应的方法:
1、多对多关系就是和一对多的关系的大同小异,只是多对多的关系可以看成两个一对多关系。
2、多对多关系的配置的步骤和一对多关系配置是一样的:(以书籍表、类别表以及书籍类别表为例)
2.1:首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,这两张表对应的model与mapper
2.3:在对应的HbookCategoryMapper.java文件中写好方法:
mybatis的Hibernate的不同就在于Hibernate代码是自动生成的,而Mybatis的代码可以手动写,可以手动改变。
先来回归一下Hibernate中关联关系:在Hibernate中,一对多和多对多的关系处理如下:
1、一对多:(以订单表和订单详情表为例)
一方order类,
定义一个引用对象list<orderItems> orderItems
order.hbm.xml配置:
one-to-many id=“orderItems”
配置多方的实体类路径
多方orderItem类:
定义一个引用对象list<order> order
orderItem.hbm.xml配置:
many-to-one id=“order”
配置一方的实体类路径
2、多对多:(以书籍表和类别表为例,其中书籍类别表为中间表)
1方Book:
定义一个引用对象list<category> categorys
book.hbm.xml配置:
many -to-many id=“categorys”
配置多方的实体类路径
1方category:
定义一个引用对象list<book> books
category.hbm.xml配置:
many-to-manyid=“books”
配置一方的实体类路径
- <table schema="" tableName="t_hibernate_order" domainObjectName="Order"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
- <!-- 忽略列,不生成bean 字段 -->
- <!-- <ignoreColumn column="FRED" /> -->
- <!-- 指定列的java数据类型 -->
- <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
- </table>
-
- <table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
- <!-- 忽略列,不生成bean 字段 -->
- <!-- <ignoreColumn column="FRED" /> -->
- <!-- 指定列的java数据类型 -->
- <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
- </table>
-
orderMpper.xml:
- <resultMap id="OrderVoMap" type="com.javaxl.ssm.model.vo.OrderVo" >
- <result property="orderId" column="order_id"></result>
- <result property="orderNo" column="order_no"></result>
- <collection property="orderItems" ofType="com.javaxl.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>
orderItemMpper.xml:
- <resultMap id="OrderVoMap" type="com.javaxl.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.javaxl.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>
OrderMapper.java:
OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);
OrderItemsMapper.java:
OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
One2ManyService.java:
- package com.zj.www.service;
-
- import com.zj.www.vo.OrderItemVo;
- import com.zj.www.vo.OrderVo;
-
- public interface One2ManyService {
- OrderVo queryOrderVoByOrderId(Integer orderId);
- OrderItemVo queryOrderItemVoByOrderItemId(Integer OrderItemId);
- }
One2ManyServiceImpl:
- package com.zj.www.service.impl;
-
- import com.zj.www.mapper.OrderItemMapper;
- import com.zj.www.mapper.OrderMapper;
- import com.zj.www.service.One2ManyService;
- import com.zj.www.vo.OrderItemVo;
- import com.zj.www.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);
- }
- }
两个表之间的关系:
1、一个订单有多个订单详情表
2、一个订单详情只有一个订单
ordervo:
- package com.zj.www.vo;
-
- import com.zj.www.model.Order;
- import com.zj.www.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;
- }
-
-
- @Override
- public String toString() {
- return "OrderVo{" +
- "orderItems=" + orderItems +
- '}';
- }
- }
orderItemVo:
- package com.zj.www.vo;
-
- import com.zj.www.model.Order;
- import com.zj.www.model.OrderItem;
-
- public class OrderItemVo extends OrderItem {
- private Order order;
-
- public Order getOrder() {
- return order;
- }
-
- public void setOrder(Order order) {
- this.order = order;
- }
-
-
- @Override
- public String toString() {
- return "OrderItemVo{" +
- "order=" + order +
- '}';
- }
- }
One2ManyService:
- package com.zj.www.service.impl;
-
- import com.zj.www.model.OrderItem;
- import com.zj.www.service.One2ManyService;
- import com.zj.www.vo.OrderItemVo;
- import com.zj.www.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(8);
- 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());
- }
- }
运行结果:
通过订单id产查找订单详情:
通过订单详情id查找对应的订单:
表结构:
订单详情表:
订单表:
- <table schema="" tableName="t_hibernate_book" domainObjectName="HBook"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
- <!-- 忽略列,不生成bean 字段 -->
- <!-- <ignoreColumn column="FRED" /> -->
- <!-- 指定列的java数据类型 -->
- <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
- </table>
-
-
- <table schema="" tableName="t_hibernate_book_category" domainObjectName="HBookC"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
- <!-- 忽略列,不生成bean 字段 -->
- <!-- <ignoreColumn column="FRED" /> -->
- <!-- 指定列的java数据类型 -->
- <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
- </table>
-
-
-
- <table schema="" tableName="t_hibernate_category" domainObjectName="HCate"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
- <!-- 忽略列,不生成bean 字段 -->
- <!-- <ignoreColumn column="FRED" /> -->
- <!-- 指定列的java数据类型 -->
- <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
- </table>
- <resultMap id="HbookVoMap" type="com.zj.www.vo.HBookVo">
- <result property="bookId" column="book_id"></result>
- <result property="bookName" column="book_name"></result>
- <collection property="categories" ofType="com.zj.www.model.HCate">
- <result property="categoryId" column="category_id"></result>
- <result property="categoryName" column="category_name"></result>
- </collection>
- </resultMap>
-
-
- <resultMap id="CategoryVoMap" type="com.zj.www.vo.HCateVo">
- <result property="categoryId" column="category_id"></result>
- <result property="categoryName" column="category_name"></result>
- <collection property="hbooks" ofType="com.zj.www.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>
- package com.zj.www.mapper;
-
- import com.zj.www.model.HBookC;
- import com.zj.www.vo.HBookVo;
- import com.zj.www.vo.HCateVo;
- import org.apache.ibatis.annotations.Param;
-
- public interface HBookCMapper {
- int deleteByPrimaryKey(Integer bcid);
-
- int insert(HBookC record);
-
- int insertSelective(HBookC record);
-
- HBookC selectByPrimaryKey(Integer bcid);
-
- int updateByPrimaryKeySelective(HBookC record);
-
- int updateByPrimaryKey(HBookC record);
-
-
- HBookVo queryByBookId(@Param("bookId") Integer bookId);
-
- HCateVo queryByCid(@Param("cid") Integer cid);
-
-
- }
Many2ManyService:
- package com.zj.www.service;
-
- import com.zj.www.vo.HBookVo;
- import com.zj.www.vo.HCateVo;
-
- public interface Many2ManyService {
-
- HBookVo queryByBookId(Integer bookId);
-
- HCateVo queryByCid(Integer cid);
-
- }
Many2ManyServiceImpl:
- package com.zj.www.service.impl;
-
- import com.zj.www.mapper.HBookCMapper;
- import com.zj.www.service.Many2ManyService;
- import com.zj.www.vo.HBookVo;
- import com.zj.www.vo.HCateVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- @Service
- public class Many2ManyServiceImpl implements Many2ManyService {
- @Autowired
- private HBookCMapper hBookCMapper;
- @Override
- public HBookVo queryByBookId(Integer bookId) {
- return hBookCMapper.queryByBookId(bookId);
- }
-
- @Override
- public HCateVo queryByCid(Integer cid) {
- return hBookCMapper.queryByCid(cid);
- }
- }
HBookVo:
- package com.zj.www.vo;
-
- import com.zj.www.model.HBook;
- import com.zj.www.model.HBookC;
- import com.zj.www.model.HCate;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class HBookVo extends HBook {
-
-
- private List<HCate> categories = new ArrayList<>();
-
- public List<HCate> getCategories() {
- return categories;
- }
-
- public void setCategories(List<HCate> categories) {
- this.categories = categories;
- }
-
-
- }
HBookC:
- package com.zj.www.vo;
-
- import com.zj.www.model.HBook;
- import com.zj.www.model.HCate;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class HCateVo extends HCate {
-
-
- private List<HBook> hbooks = new ArrayList<>();
-
- public List<HBook> getHbooks() {
- return hbooks;
- }
-
- public void setHbooks(List<HBook> hbooks) {
- this.hbooks = hbooks;
- }
-
-
- @Override
- public String toString() {
- return "HCateVo{" +
- "hbooks=" + hbooks +
- '}';
- }
- }
- package com.zj.www.service.impl;
-
- import com.zj.www.model.HBook;
- import com.zj.www.model.HCate;
- import com.zj.www.service.Many2ManyService;
- import com.zj.www.vo.HBookVo;
- import com.zj.www.vo.HCateVo;
- 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 (HCate category : hbookVo.getCategories()) {
- System.out.println(category);
- }
- }
-
- @Test
- public void queryByCid() {
- HCateVo categoryVo = many2ManyService.queryByCid(8);
- System.out.println(categoryVo);
- for (HBook hbook : categoryVo.getHbooks()) {
- System.out.println(hbook);
- }
-
- }
- }
运行结果:
1、通过书籍id找出对应的类别名称:
sql语句:
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 = ?
书籍表:
书籍类别表:
类别表:
2、通过类别id查找出对应的书籍名称:
运行结果:
sql语句:
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=?
逻辑推理:
类别表:
书籍类别表:
书籍表:
mybatis的关联关系和Hibernate的关联关系大同小异
其中要注意的是:配置关系Vo类和在xxxMap.xml中的配置:
里面用的是collection--->oftype
<collection property="categories" ofType="com.zj.www.model.HCate">association--->javaType
<association property="order" javaType="com.zj.www.model.Order">
今天的分享就到这里了,希望能够帮助到你!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。