1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx.xsd 13 http://www.springframework.org/schema/context 14 http://www.springframework.org/schema/context/spring-context.xsd"> 15 <!-- 扫描注解 --> 16 <context:component-scan base-package="cn.leon"></context:component-scan> 17 <!-- 数据源 --> 18 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 19 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 20 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/user1"></property> 21 <property name="user" value="root"></property> 22 <property name="password" value="root"></property> 23 </bean> 24 <!-- sqlSessionFactory,引入数据源,配置映射文件位置 --> 25 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 26 <property name="dataSource" ref="dataSource"></property> 27 <property name="mapperLocations" value="classpath:cn/leon/mapper/*Mapper.xml"></property> 28 </bean> 29 <!-- mapperScannerConfigurer,生成代理,名称为接口名第一个字母小写 ,引入sqlsessionfactory--> 30 <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 31 <!-- 指定基础包的位置 --> 32 <property name="basePackage" value="cn.leon.mapper"></property> 33 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> 34 </bean> 35 <!-- 声明式事务,引入数据源 --> 36 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 37 <property name="dataSource" ref="dataSource"></property> 38 </bean> 39 <!-- 开启通知,引入声明式事务 --> 40 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 41 <!-- 传播行为 --> 42 <tx:attributes> 43 <tx:method name="add*" propagation="REQUIRED"/> 44 <tx:method name="delete*" propagation="REQUIRED"/> 45 <tx:method name="update*" propagation="REQUIRED"/> 46 <tx:method name="*" propagation="SUPPORTS"/> 47 </tx:attributes> 48 </tx:advice> 49 <!-- 切面 --> 50 <aop:config> 51 <aop:pointcut expression="execution(* cn.leon.service.*.*(..))" id="serviceMethod"/> 52 <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/> 53 </aop:config> 54 </beans>