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:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/beans/spring-context.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx.xsd"> 15 16 <!--配置属性文件--> 17 <context:property-placeholder location="classpath:database.properties"/> 18 <!--配置数据源--> 19 <bean id="dataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource"> 20 <property name="driver" value="${jdbc.driver}"/> 21 <property name="url" value="${jdbc.url}"/> 22 <property name="username" value="${jdbc.username}"/> 23 <property name="password" value="${jdbc.password}"/> 24 </bean> 25 <!--配置SqlSessionFactory--> 26 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 27 <!--配置数据源--> 28 <property name="dataSource" ref="dataSource"/> 29 <!--配置mybatis配置文件路径--> 30 <property name="configLocation" value="mybatis/mybatis-config.xml"/> 31 <!--配置mapper.xml路径--> 32 <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/> 33 </bean> 34 <!--配置mapper包的扫描--> 35 <bean id="mapperScannerConfigurer" class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> 36 <!--配置sqlSessionFactoryBeanName 只能用value --> 37 <!--如果用ref引用sqlSessionFactory会导致初始化的时候数据加载不进来就开始加载sqlSessionFactory--> 38 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 39 <!--配置mapper包的路径--> 40 <property name="basePackage" value="com.xxx.mapper"/> 41 </bean> 42 43 44 <!--配置数据源事务管理--> 45 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 46 <property name="dataSource" ref="dataSource"/> 47 </bean> 48 49 <!--配置扫描的包--> 50 <context:component-scan base-package="com.xxx"></context:component-scan> 51 <!--配置aop自动代理--> 52 <aop:aspectj-autoproxy/> 53 <!--配置事物注解驱动--> 54 <!--<tx:annotation-driven transaction-manager="transactionManager"/>--> 55 <!--配置事物--> 56 <!--事物传播特性--> 57 <tx:advice id="transactionInterceptor"> 58 <tx:attributes> 59 <tx:method name="save" propagation="REQUIRED" rollback-for="Exception"/> 60 <tx:method name="delete" propagation="REQUIRED" rollback-for="Exception"/> 61 <tx:method name="update" propagation="REQUIRED" rollback-for="Exception"/> 62 <tx:method name="list*" propagation="REQUIRED" read-only="true" timeout="4000"/> 63 <tx:method name="get*" propagation="REQUIRED" read-only="true" timeout="4000"/> 64 <tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/> 65 </tx:attributes> 66 </tx:advice> 67 68 <!--配置参与事物的类--> 69 <aop:config> 70 <aop:pointcut id="pointCut" expression="execution(* com..*.*(..))"/> 71 <aop:advisor advice-ref="transactionInterceptor" pointcut-ref="pointCut"/> 72 </aop:config> 73 </beans>
17行 需要提前创建一个jdbc的属性文件
19行 可以更换数据源 属性要对应
30行 需要提前创建好一个空的mybatis配置文件
32行 表示把所有mapper.xml文件都加载到mybatis配置中
40行 填写自己的mapper路径(或者dao路径 )
50行 填写需要扫描的包一般情况下是包路径
54行 是用注解实现事务回滚(需要每个类都写注解 )
57行 是用配置文件方式实现事务回滚(推荐 配置一次自动实现事务回滚, 最后<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/> 一定要配置 前面匹配不到这个能匹配到控制事务回滚
70行 代表的是配置切点 用来实现aop
架包依赖
spring-context spring-jdbc spring-webmvc mysql-connector-java(数据库) aspectjweaver mybatis mybatis-spring(整合spring和mybatis的架包)
原文:https://www.cnblogs.com/liljoker/p/13042068.html