mybatis为何不像hibernate一样有 Transaction.begin() 只有commit 和rollback
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis为何不像hibernate一样有 Transaction.begin() 只有commit 和rollback相关的知识,希望对你有一定的参考价值。
因为mybatis把事务抽象到了TransactionManager中,而事务默认开启,所以我们只需要使用sqlSession.commit()和rollback()方法,因为在sqlSession中,实际上调用的是transactionManager来完成的具体的事务提交和回滚操作。而你说hibernate的beginTransaction,其实这个方法不是开启事务,因为hibernate默认事务也是开启的,但是hibernate有事务状态这个概念,beginTransaction方法仅仅是把session关联的事务置为活动状态而已,并不是真正开启一个事务。 参考技术A 因为hibernate在底层给封装了这样的方法,便于用户去调用,而mybatis没有,但过度的封装虽然方便了用户,却使得可扩展性降低,所以刚开始学习持久化,建议JDBC和mybatis,太高级的东西会掩盖底层的知识。满意的话请给好评谢谢 参考技术B hibernate已经对jdbc进行了封装Transaction.begin()是对事务处理进行封装,我们拿来用就行了。mybatis虽然也进行了部分的封装但是基本上还是sql为主。这种情况就要求我们自己主动进行事务处理,提交(commit)回滚(rollback),但是不同数据库还是有不同特性,比如说mysql他的擎InnoDB提供自动事务处理。
Mybatis关联关系
一对多
这里用到了两张表(订单表以及订单项表)
订单表:
订单项表:
一对多无非就是一个订单对应多个订单项
我们先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
<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.psy.model.vo; import com.psy.model.Order; import com.psy.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.psy.model.vo; import com.psy.model.Order; import com.psy.model.OrderItem; public class OrderItemVo extends OrderItem { private Order order; public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } }
OrderMapper:
OrderVo selectByOid(@Param("oid") Integer oid);
OrderMapper.xml(订单是一):
<resultMap id="OrderVoMap" type="com.psy.model.vo.OrderVo" > <result property="orderId" column="order_id"></result> <result property="orderNo" column="order_no"></result> <collection property="orderItems" ofType="com.psy.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 = #{oid}
</select>
OrderItemMapper:
OrderItemVo selectByOrderItemId(@Param("orderItemId") Integer orderItemId);
OrderItemMapper.xml (订单项是多):
<resultMap id="OrderItemVoMap" type="com.psy.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.psy.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.psy.service; import com.psy.model.vo.OrderItemVo; import com.psy.model.vo.OrderVo; public interface One2ManyService { OrderVo selectByOid(Integer oid); OrderItemVo selectByOrderItemId(Integer orderItemId); }
One2ManyServiceImpl:
package com.psy.service.impl; import com.psy.mapper.OrderItemMapper; import com.psy.mapper.OrderMapper; import com.psy.model.vo.OrderItemVo; import com.psy.model.vo.OrderVo; import com.psy.service.One2ManyService; 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 selectByOid(Integer oid) { return orderMapper.selectByOid(oid); } @Override public OrderItemVo selectByOrderItemId(Integer orderItemId) { return orderItemMapper.selectByOrderItemId(orderItemId); } }
创建测试类One2ManyServiceImplTest:
package com.psy.service.impl; import com.psy.SpringBaseTest; import com.psy.model.OrderItem; import com.psy.model.vo.OrderItemVo; import com.psy.model.vo.OrderVo; import com.psy.service.One2ManyService; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; 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()); } }
测试结果:
多对多
用到三张表(书籍表、书籍类别表、中间表)
多对多就是指一本书可以有多个类别,一个类别又可以对应多本书
同样的先使用逆向工程生成
<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>
HbookVo:
package com.psy.model.vo; import com.psy.model.Hbook; import java.util.ArrayList; import java.util.List; public class HbookVo extends Hbook { private List categories =new ArrayList<>(); public List getCategories() { return categories; } public void setCategories(List categories) { this.categories = categories; } }
CategoryVo:
package com.psy.model.vo; import com.psy.model.Category; import com.psy.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:
HbookVo queryByBid(@Param("bid")Integer bid); CategoryVo queryByCid(@Param("cid") Integer cid);
HbookCategoryMapper.xml:
<resultMap id="HbookVoMap" type="com.psy.model.vo.HbookVo" > <result property="bookId" column="book_id"></result> <result property="bookName" column="book_name"></result> <result property="price" column="price"></result> <collection property="category" ofType="com.psy.model.Category"> <result property="categoryId" column="category_id"></result> <result property="categoryName" column="category_name"></result> </collection> </resultMap> <resultMap id="CategoryVoMap" type="com.psy.model.vo.CategoryVo" > <result property="categoryId" column="category_id"></result> <result property="categoryName" column="category_name"></result> <collection property="hbooks" ofType="com.psy.model.Hbook"> <result property="bookId" column="book_id"></result> <result property="bookName" column="book_name"></result> <result property="price" column="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.psy.service; import com.psy.model.vo.CategoryVo; import com.psy.model.vo.HbookVo; public interface HbookCategoryService { HbookVo queryByBid(Integer bid); CategoryVo queryByCid(Integer cid); }
HbookCategoryServiceImpl:
package com.psy.service.impl; import com.psy.mapper.HbookCategoryMapper; import com.psy.model.vo.CategoryVo; import com.psy.model.vo.HbookVo; import com.psy.service.HbookCategoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @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.psy.service.impl; import com.psy.SpringBaseTest; import com.psy.model.Category; import com.psy.model.Hbook; import com.psy.model.vo.CategoryVo; import com.psy.model.vo.HbookVo; import com.psy.service.HbookCategoryService; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; public class HbookCategoryServiceImplTest extends SpringBaseTest { @Autowired private HbookCategoryService hbookCategoryService; @Test public void queryByBid() { HbookVo hbookVo = hbookCategoryService.queryByBid(1); System.out.println(hbookVo); for (Category category : hbookVo.getCategory()) { System.out.println(category); } } @Test public void queryByCid() { CategoryVo categoryVo = hbookCategoryService.queryByCid(1); System.out.println(categoryVo); for (Hbook hbook : categoryVo.getHbooks()) { System.out.println(hbook); } } }
测试结果:
以上是关于mybatis为何不像hibernate一样有 Transaction.begin() 只有commit 和rollback的主要内容,如果未能解决你的问题,请参考以下文章