基于xml文件实现:
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 1.开启包扫描 -->
<context:component-scan base-package="com.jt"/>
<!-- 2.引入外部的配置文件 -->
<!--
这个标签适合大部分文件引用
<context:property-placeholder location="classpath:/property/jdbc.properties"/>
-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations"><!-- Resource[] locations; -->
<list>
<value>classpath:/property/jdbc.properties</value>
</list>
</property>
</bean>
<!-- <util:property-path path="#"/> -->
<!-- 3.配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 4.开启事务策略 -->
<tx:annotation-driven/>
<!-- 5.声明式的事务处理 利用AOP原理实现
AOP:在不影响原有业务的前提下,进行方法进行扩展
注意事项:
1、切入点
2.需要设定通知
环绕通知/前置/后置/异常/最终/
-->
<!-- 5.1配置事务的管理器 主要作用 事务的提交/回滚/ -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 5.2编辑事务的通知 环绕通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事务策略
propagation="REQUIRED" 执行save方法时,必须添加一个事务
propagation="SUPPORTS" 表示事务支持的,原来有事务的添加事务
事务回滚策略:
1.运行时异常 事务自动回滚
2.编译异常 sql io 事务不负责回滚
rollback-for="" 指定异常类型 回滚
no-rollback-for="" 指定异常类型不回滚
-->
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/><!-- 查找 -->
<tx:method name="*" propagation="SUPPORTS" read-only="true"/><!--表示其他方法 -->
</tx:attributes>
</tx:advice>
<!-- 5.3配置事务的切面 Content Model (pointcut*, advisor*, aspect*)
表达式 :
execution(返回值类型 :包名 类名 方法名(参数类型))
execution(* com.jt.manage.service.*.*(..)) 参数类型用..
-->
<aop:config>
<aop:pointcut expression="execution(* com.jt.manage.service.*.*(..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
</beans>
基于注解的形式:
@Configuration
@ComponentScan(value="com.jt",
excludeFilters={@Filter(classes={Controller.class,ControllerAdvice.class})})
@PropertySource("classpath:configs.properties")
@MapperScan("com.jt.**.dao")//这个代替下边的 MapperScannerConfigurer的配置方法
@EnableAspectJAutoProxy //启用切点AOP
@EnableTransactionManagement //启用事务管理
public class AppRootConfig {
//整合Druid连接
@Bean(value="dataSource",initMethod="init",destroyMethod="close")
@Lazy(false)
public DataSource newDataSource(
@Value("${jdbcDriver}")String driver,
@Value("${jdbcUrl}")String url,
@Value("${jdbcUser}")String username,
@Value("${jdbcPassword}")String password
){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
/**
* 整合SqlSessionFactoryBean对象
* 通过此对象创建SqlSessionFactory
* @param dataSource
* @return
* @throws IOException
*/
@Bean("sqlSessionFactory")
public SqlSessionFactoryBean newSqlSessionFactoryBean(
@Autowired DataSource dataSource) throws IOException{
SqlSessionFactoryBean fBean = new SqlSessionFactoryBean();
fBean.setDataSource(dataSource);
Resource[] mapperLocations=
new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/sys/*Mapper.xml");
fBean.setMapperLocations(mapperLocations);
return fBean;
}
事务基于全注解实现:
/**
* Spring 中的声明式事务控制
*/
@Bean("txManager")
public DataSourceTransactionManager
newDataSourceTransactionManager(DataSource dataSource){
//创建事务管理对象
DataSourceTransactionManager tm=new DataSourceTransactionManager();
//注入数据源对象(Druid)
tm.setDataSource(dataSource);
return tm;
}