##持久层配置文件
1、引入外部配置文件 例如db.properties
2、配置数据源 例如org.apache.commons.dbcp2.BasicDataSource
3、配置SqlSessionFactoryBean,用于创建SqlSessionFactory的
4、配置MapperScannerConfigurer 用于扫描mapper接口所在包,创建Mapper 代理对象
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置内容:
1.数据源,引入的外部属性配置文件
2.SqlSessionFactoryBean
3.MapperScannerConfigurer
-->
<!-- 配置引入外部的属性配置文件 -->
<context:property-placeholder location="classpath:mybatis/db.properties"/>
<!-- 1.配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 还可以有数据源的其他配置 -->
</bean>
<!-- 2.配置SqlSessionFactoryBean,用于创建SqlSessionFactory的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入相关属性 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 注入使用的别名 所在package ,当有多个package需要配置时,使用分隔符 , -->
<property name="typeAliasesPackage" value="com.geng.pojo"></property>
<!-- 注入 映射文件所在路径 -->
<property name="mapperLocations" value="classpath:com/geng/mapper/*Mapper.xml"></property>
<!-- 注入configLocation 属性,绑定mybatis的配置文件 -->
</bean>
<!-- 3.配置MapperScannerConfigurer 用于扫描mapper接口所在包,创建Mapper代理对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入basePackage mapper接口所在包 -->
<property name="basePackage" value="com.geng.mapper" />
<!-- 一定要注入 sqlSessionFactoryBeanName -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
业务层配置
1、配置事务管理器,关联DataSource
2、配置事务通知
3、使用aop 配置 那些类使用事务管理
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.geng.service"></context:component-scan>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.geng.service.*.*(..))" id="pointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>
</beans>