<?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: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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 扫描增加注解的类,在指定包中... -->
<context:component-scan base-package="com.lsy.*">
<!-- Controller注解是 SpringMVC 框架完成,所以Spring排除 Controller注解的扫描 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/atcrowdfunding-v?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8" />
<property name="user" value="root" />
<property name="password" value="123456" />
</bean>
<!-- Mybatis 的核心对象 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation"
value="classpath:mybatis/config.xml" />
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations">
<list>
<value>classpath*:mybatis/mapper-*.xml</value>
</list>
</property>
</bean>
<bean id="mapperScannerConfigurer"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 代理 dao 接口来和数据库进行交互 -->
<property name="basePackage"
value="com.lsy.atcrowdfunding.**.dao" />
</bean>
<!-- 事物处理,多个数据库的操作当成一个整体,要么完全成功要么完全失败 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="transactionAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<!-- Spring 框架只支持 方法的连接点操作,只能在方法中增加事物功能 -->
<!-- 事物的传播行为使用【REQUIRED】 ,隔离级别【isolation="DEFAULT"】采用数据库本身的隔离(可重复读)-->
<tx:method name="*" propagation="REQUIRED"
isolation="DEFAULT" rollback-for="java.lang.Exception" />
<!-- query开头的查询方法,使用只读事物,提升访问效率 -->
<tx:method name="query*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切入点表达式,将事物应用到 业务当中 -->
<aop:config>
<aop:advisor advice-ref="transactionAdvice"
pointcut="execution(* com.lsy..*Service.*(..))" />
</aop:config>
</beans>
网友评论