1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop.xsd 14 http://www.springframework.org/schema/tx 15 http://www.springframework.org/schema/tx/spring-tx.xsd 16 "> 17 <context:component-scan base-package="cn.smbms.service,cn.smbms.pojo"/> 18 <context:property-placeholder location="classpath:database.properties"/> 19 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 20 <property name="driverClassName" value="${driver}"/> 21 <property name="url" value="${url}"/> 22 <property name="username" value="${user}"/> 23 <property name="password" value="${password}"/> 24 <!--性能调优--> 25 <property name="initialSize" value="${initialSize}"/> 26 <property name="maxActive" value="${maxActive}"/> 27 <property name="maxIdle" value="${maxIdle}"/> 28 <property name="minIdle" value="${minIdle}"/> 29 <property name="maxWait" value="${maxWait}"/> 30 <property name="removeAbandoned" value="${removeAbandoned}"/> 31 <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/> 32 <property name="testOnBorrow" value="${testOnBorrow}"/> 33 <property name="testOnReturn" value="${testOnReturn}"/> 34 <property name="testWhileIdle" value="${testWhileIle}"/> 35 <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/> 36 <property name="validationQuery" value="${validationQuery}"/> 37 <property name="numTestsPerEvictionRun" value="${numTestsPerEvictionRun}"/> 38 39 </bean> 40 41 <!--拿到数据源,并且拿到mybatis配置文件--> 42 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" > 43 <property name="dataSource" ref="dataSource"/> 44 <property name="configLocation" value="classpath:mybatis-config.xml"/> 45 <property name="plugins"> 46 <array> 47 <bean class="com.github.pagehelper.PageInterceptor"> 48 <!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 --> 49 <property name="properties"> 50 <value> 51 helperDialect=mysql 52 reasonable=true 53 supportMethodsArguments=true 54 params=count=countSql 55 autoRuntimeDialect=true 56 </value> 57 </property> 58 </bean> 59 </array> 60 </property> 61 </bean> 62 63 <!-- 整合dao层,mapper.xml和接口--> 64 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 65 <property name="basePackage" value="cn.smbms.mapper"/> 66 </bean> 67 68 <!--配置事务管理器--> 69 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 70 <property name="dataSource" ref="dataSource"/> 71 72 </bean> 73 <!--事务注解的配置--> 74 <aop:aspectj-autoproxy/> 75 76 <tx:advice id="interceptor" transaction-manager="transactionManager"> 77 <tx:attributes> 78 <!-- 查询只读且不开启事务--> 79 <tx:method name="query*" read-only="true" propagation="SUPPORTS"/> 80 <!--下面给它默认开启事务--> 81 <tx:method name="add*" /> 82 <tx:method name="del*"/> 83 <tx:method name="update*"/> 84 <tx:method name="*" /> 85 </tx:attributes> 86 </tx:advice> 87 88 <aop:config proxy-target-class="true"> 89 <!--切点--> 90 <aop:pointcut id="transaction" expression="execution(* *cn.smbms.service..*.*(..))"/> 91 <aop:advisor advice-ref="interceptor" pointcut-ref="transaction"/> 92 </aop:config> 93 94 </beans>