SpringMybatis配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop.xsd">
	
	 <!-- 开启事务注解驱动 --> 
	<tx:annotation-driven transaction-manager="transactionManager" />
	
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass">
            <value>${jdbc.driverClassName}</value>
        </property>
        <property name="jdbcUrl">
            <value>${jdbc.databaseurl}</value>
        </property>
        <property name="user">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.pwd}</value>
        </property>
        <!-- 最大连接数 -->
        <property name="maxPoolSize" value="${maxPoolSize}" />
        <!-- 最小连接数 -->
        <property name="minPoolSize" value="${minPoolSize}" />
        <!-- 初始化连接数 -->
        <property name="initialPoolSize" value="${initialPoolSize}" />
        <!-- 最大空闲时间 ,120秒内未使用则连接被丢弃。若为0则永不丢弃 -->
        <property name="maxIdleTime" value="${maxIdleTime}" />
        <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
        <property name="acquireIncrement" value="${acquireIncrement}" />
        <!-- 最大的PreparedStatement的数量 -->
        <property name="maxStatements" value="${maxStatements}" />
        <!-- 每隔120秒检查连接池里的空闲连接 -->
        <property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}" />
        <!-- 如果设为true那么在取得连接的同时将校验连接的有效性 。Default: false -->
        <property name="testConnectionOnCheckin" value="${testConnectionOnCheckin}" />
        <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts" value="${acquireRetryAttempts}" />
        <!--连接关闭时默认将所有未提交的操作回滚。Default: false -->
        <property name="autoCommitOnClose" value="${autoCommitOnClose}" />
        <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。 
            如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
        <property name="breakAfterAcquireFailure"
            value="${breakAfterAcquireFailure}" />
        <!--c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能通过多线程实现多个操作同时被执行。Default: 3 -->
        <property name="numHelperThreads" value="${numHelperThreads}" />
        <!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。Default:  0 -->
        <property name="checkoutTimeout" value="${checkoutTimeout}" />
    </bean>
    
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mapper/mybatis.cfg.xml" />
		<property name="mapperLocations" value="classpath:mapper/*Mapper.xml" />
	</bean>
	
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">  
  		<constructor-arg index="0" ref="sessionFactory" />  
	</bean> 
	 
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
			<tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
			<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
			<tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
			<tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
			<tx:method name="cancel*" 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="*" read-only="true" /> -->
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:advisor pointcut="execution(public * com.pingan.insurance..*Service*.*(..))" advice-ref="txAdvice" order="1" />
	</aop:config>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sessionFactory"/> 
		<property name="basePackage" value="com.pingan.insurance.roadassistant.dao" />
	</bean>
</beans>

 

转载于:https://my.oschina.net/u/3278373/blog/849268

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值