您的位置:首页技术文章
文章详情页

java - mybatis怎么处理事务问题

【字号: 日期:2024-01-21 11:38:58浏览:34作者:猪猪

问题描述

我用的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容器会自动管理事务!java - mybatis怎么处理事务问题

回答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并加上注解即可,发生异常则此方法的事务会回滚

标签: java
相关文章: