springmybatis事务配置和控制

Posted 来自于四明山的码农

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springmybatis事务配置和控制相关的知识,希望对你有一定的参考价值。

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);
	}

}

  

以上是关于springmybatis事务配置和控制的主要内容,如果未能解决你的问题,请参考以下文章

常用的两种springmybatis 配置方式

spring,mybatis 整合 启用tomcat 报错

阶段3 2.Spring_10.Spring中事务控制_5 spring事务控制的代码准备

MyBatis框架—动态 SQL配置文件事务

片段事务中的实例化错误

全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段