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){},否则影响事务的原子性。

  • 相关阅读:
    Access sql语句创建表及字段类型
    30条HTML代码编写指南 for入门者
    21 个HTML网页转RSS Feeds的工具
    51 个漂亮的电子商务网站设计分享
    如何更改列表项前的New标记的天数设置(daystoshownewicon )
    如何使Layouts里的页面应用站点母板页
    SPCAMLEditor使用系列(2)利用SPCAMLEditor,实现列表顺序号。
    在SharePoint中使用自定义的服务器控件(Web Control)
    开发支持三级目录的导航菜单
    CAML查询时用户类型字段的处理
  • 原文地址:https://www.cnblogs.com/Anders888/p/7208447.html
Copyright © 2011-2022 走看看