<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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"> <!--自动给符合切面信息的目标对象织入增强,创建代理对象--> <aop:aspectj-autoproxy/> <!--配置扫描包下的注解,让注解发挥应用的作用--> <context:component-scan base-package="com.qf"/> <!--整合mybatis相关配置--> <!--1,加载jdbc.properties文件--> <context:property-placeholder location="classpath:dbconfig.properties"/> <!--2,配置数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--3,创建sqlSessionFactory工厂对象--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--注入数据源--> <property name="dataSource" ref="dataSource"/> <!--设置读取mapper.xml文件的路径--> <property name="mapperLocations"> <list> <value>classpath:com/qf/mapper/*.xml</value> </list> </property> <!--设置别名--> <property name="typeAliasesPackage" value="com.qf.entity"/> </bean> <!--4,配置mapper扫描--> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--配置mapper接口的包名--> <!--会把这个包下的所有接口都创建出接口代理对象,并把代理对象放到ioc容器中,名称是接口名称,首字母小写--> <property name="basePackage" value="com.qf.mapper"/> <!--配置工厂名称,只有一个工厂的话,其实可以省略不配置--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 创建一个bookServiceImpl实现类对象--> <bean id="bookServiceImpl" class="com.qf.service.impl.BookServiceImpl"> <!--给类中的mapper接口进行实例化--> <property name="booksMapper" ref="booksMapper"/> </bean> <!--Spring管理事务的配置--> <!--1,配置事务管理器--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--依赖数据源--> <property name="dataSource" ref="dataSource"/> </bean> <!--2,配置事务增强--> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!--配置查询方法的事务控制策略--> <tx:method name="query*" propagation="SUPPORTS" read-only="true"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <!--配置增删改方法的事务控制策略--> <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" read-only="false"/> </tx:attributes> </tx:advice> 3,配置事务的切面信息 <aop:config> <!--配置切点信息--> <aop:pointcut id="pt" expression="execution(* com.qf.service.impl.BookServiceImpl.*(..))"/> <!--配置切面信息--> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> <!--事务注解驱动,并配置使用的是哪个事务管理器--> <tx:annotation-driven transaction-manager="txManager"/> </beans>