java - mybatis怎么处理事务问题
问题描述
我用的mybatis是基于SqlSessionTemplate实现的,如下:
@Repository('daoSupport')public class DaoSupport implements BaseDAO { @Resource(name = 'sqlSessionTemplate') private SqlSessionTemplate sqlSessionTemplate;/** * 保存对象 * @param str * @param obj * @return * @throws Exception */ public Object save(String str, Object obj){return sqlSessionTemplate.insert(str, obj); }/** * 批量更新 * @param str * @param obj * @return * @throws Exception */ public Object batchSave(String str, List objs){return sqlSessionTemplate.insert(str, objs); }/** * 修改对象 * @param str * @param obj * @return * @throws Exception */ public Object update(String str, Object obj){Integer count=sqlSessionTemplate.update(str, obj);
@Service
public class OrderInfoService { @Resource(name = 'daoSupport') private DaoSupport dao;@SuppressWarnings('unchecked') public List<PageData> getPerson() { List<PageData> a =(List<PageData>)dao.findAllList( 'OrderInfoMapper.getOrderInfo'); return a; } public void save(PageData pd){dao.save('OrderInfoMapper.saveOrderInfo', pd); } public void update(PageData pd){dao.update('OrderInfoMapper.updateOrder', pd); }@SuppressWarnings('unchecked') public List<PageData> list(PageData pd){return (List<PageData>)dao.findForList('OrderInfoMapper.getOrderInfo', pd); }
在xml文件里面写对应sql的Mapper,我想在调用service层的方法操作数据库时,几条sql语句同时commit,其中有一条失败则全部不执行,就和jdbc那样先设置不自动commit,然后在commit那种,要在哪里配置或调用啥方法实现?
问题解答
回答1:请百度spring的事务管理
第一种,配置事务管理的规则,具体怎么写可以再百度,一两句说不清
<bean class='org.springframework.jdbc.datasource.DataSourceTransactionManager'><property name='dataSource' ref='dataSource'/> </bean> <aop:config><aop:pointcut expression='execution(* com.web..*.services..*Impl.*(..))'/><aop:advisor advice-ref='txAdvice' pointcut-ref='interceptorPointCuts'/> </aop:config> <tx:advice transaction-manager='transactionManager'><tx:attributes> <tx:method name='save*' propagation='REQUIRED' rollback-for='java.lang.Exception'/> <tx:method name='update*' propagation='REQUIRED' rollback-for='java.lang.Exception'/> <tx:method name='insert*' propagation='REQUIRED' rollback-for='java.lang.Exception'/> <tx:method name='delete*' propagation='REQUIRED' rollback-for='java.lang.Exception'/>for='java.lang.Exception'/> --></tx:attributes> </tx:advice>
第二种使用注解@Transactional
<bean class='org.springframework.jdbc.datasource.DataSourceTransactionManager'><property name='dataSource' ref='dataSource'/> </bean> <!--声明使用注解式事务--> <tx:annotation-driven transaction-manager='transactionManager'/>回答2:
Programmatic Transaction Management MyBatis SqlSession provides youwith specific methods to handle transactions programmatically. Butwhen using MyBatis-Spring your beans will be injected with a Springmanaged SqlSession or a Spring managed mapper. That means that Springwill always handle your transactions.
You cannot call SqlSession.commit(), SqlSession.rollback() orSqlSession.close() over a Spring managed SqlSession. If you try to doso, a UnsupportedOperationException exception will be thrown. Notethese methods are not exposed in injected mapper classes.
Regardless of your JDBC connection’s autocommit setting, any executionof a SqlSession data method or any call to a mapper method outside aSpring transaction will be automatically committed.
If you want to control your transactions programmatically please referto chapter 10.6 of the Spring reference manual. This code shows how tohandle a transaction manually using the PlatformTransactionManagerdescribed in section 10.6.2.
mybatis-spring
回答3:Spring好像不允许手动提交事务,Spring容器会自动管理事务!
回答4://使用事务处理,订单及货物订单必须同时插进2个表里面@Transactional(rollbackFor=Exception.class)public void save(PageData pd,Map<String,Integer> map,String uuid,int Uid){ dao.save('OrderInfoMapper.saveOrderInfo', pd); OrderGood orderGood=new OrderGood(); for(String gid:map.keySet()){Goods good=goodService.findOne(gid);orderGood.setGname(good.getGname());//Integer a=map.get(gid);orderGood.setOgamount(map.get(gid));orderGood.setGid(Integer.parseInt(gid));orderGood.setOgtotalprize(map.get(gid)*good.getGprice());orderGood.setUid(Uid);orderGood.setOid(uuid);dao.save('OrderInfoMapper.saveOrderGood', orderGood);} }使用@Transactional 注解则成功解决问题,此注解是spring 的事务管理,使用前先配置如下:
<bean class='org.springframework.jdbc.datasource.DataSourceTransactionManager'><property name='dataSource' ref='dataSource'/> </bean> <!--声明使用注解式事务--> <tx:annotation-driven transaction-manager='transactionManager'/> 只需要在service层方法中执行多条mybatis的mapper并加上注解即可,发生异常则此方法的事务会回滚
相关文章:
1. css3动画 - 实现css3推倒动画2. android - 求 360浏览器 百度浏览器 搜狗浏览器的最新启动类名3. html按键开关如何提交我想需要的值到数据库4. html5 - HTML代码中的文字乱码是怎么回事?5. javascript - 微信小程序 wx.downloadFile下载文件大小有限制吗6. css - 前端flex布局嵌套内层的布局不起作用?7. css - ul ol前边的标记如何调整样式呢8. javascript - 如何通过参数获取Object的子节点?9. css3 - css字体样式加填充色而不是背景色10. python3.5 urllib.parse.unquote 乱码