spring、mybatis事務配置和控制
阿新 • • 發佈:2017-07-20
數據庫 one ons str size 連接池 action b2c drive
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); } }
spring、mybatis事務配置和控制