zoukankan      html  css  js  c++  java
  • spring、mybatis事务配置和控制

    springmybatis.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                            http://www.springframework.org/schema/context  
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                            http://www.springframework.org/schema/mvc  
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                            http://www.springframework.org/schema/tx 
                         http://www.springframework.org/schema/tx/spring-tx.xsd 
                         http://www.springframework.org/schema/aop 
                         http://www.springframework.org/schema/aop/spring-aop.xsd">
        <context:component-scan base-package="com.scsmsjk"></context:component-scan>
    
        <!-- 引入配置文件 -->
        <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:jdbc.properties" />
        </bean>
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="${driver}" />
            <property name="url" value="${url}" />
            <property name="username" value="${username}" />
            <property name="password" value="${password}" />
            <!-- 初始化连接大小 连接oracle数据库无需以下配置-->
            <property name="initialSize" value="${initialSize}" />
            <!-- 连接池最大数量 -->
            <property name="maxActive" value="${maxActive}" />
            <!-- 连接池最大空闲 -->
            <property name="maxIdle" value="${maxIdle}" />
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="${minIdle}" />
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="${maxWait}" />
        </bean>
    
        <!-- sqlSessionFactory配置 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mapperLocations" value="classpath:com/scsmsjk/mapper/*.xml"></property>
            <property name="configLocation" value="classpath:sqlMapConfig.xml" />
            <property name="typeAliasesPackage" value="com.nhinter.entity"/>
            <!-- <property name="plugins">
                <array>
                    分页插件配置
                    <bean id="paginationInterceptor" class="com.xyb2c.plugin.PaginationInterceptor"/>
                </array>
            </property> -->
        </bean>
    
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
            <property name="basePackage" value="com.scsmsjk.dao"></property>
        </bean>
        
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
        
        <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>

    service层

    package com.scsmsjk.serviceImp;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Isolation;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.scsmsjk.dao.TsmsHassentMapper;
    import com.scsmsjk.dao.TsmsSendingMapper;
    import com.scsmsjk.entity.TsmsSending;
    import com.scsmsjk.service.SmsService;
    
    
    @Service
    @Transactional(rollbackFor=Exception.class)
    public class SmsServiceImp implements SmsService {
    	
    	@Autowired
    	TsmsSendingMapper tsmssendDao;
    	
    	@Autowired
    	TsmsHassentMapper tsmshassentDao;
    	
    	public List<TsmsSending>  selectAll(){
    		return tsmssendDao.selectAll();
    	}
    	
    	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, readOnly=false)
    	public int deleteByPrimaryKey(Integer fId){
    		int aa=tsmshassentDao.deleteByPrimaryKey(19);
    		int cc=aa/0;
    		return tsmssendDao.deleteByPrimaryKey(fId);
    	}
    
    }
    

      注意:千万不可加try{} catch(Exception erro){},否则影响事务的原子性。

  • 相关阅读:
    禁止 git 自动转换换行符
    一个单元测试问题的解决
    关于脏读、幻象读、不可重复读的理解
    PKCS7 的 attached 和 detached 方式的数字签名
    关于DES加密中的 DESede/CBC/PKCS5Padding
    解决grep的结果无法显示文件名的问题
    解决64位操作系统下运行psql的问题
    一个用于将sql脚本转换成实体类的js代码
    批量将代码中的 get_XXX 替换成 XXX
    关于数据库中密码的存储
  • 原文地址:https://www.cnblogs.com/Anders888/p/7208447.html
Copyright © 2011-2022 走看看