Spring声明式事物DataSourceTransactionManager的使用与jdbcTemplate的使用_datasourcetransactionmanager 使用-程序员宅基地

技术标签: 声明式事物  java  Spring声明式事物  jdbcTemplate的使用  Spring  DataSourceTransactionManager  

1.配置事物 applicationContext-tran.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           
">
	<!-- 将事务管理器作为一个bean配置到spring的容器的 -->
	<!-- 使用的是jdbc所以使用DataSourceTransactionManager -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 事务管理器需要注入一个dataSource -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 第二步:定义通知,通知中要处理的就是事务 -->
	<!-- 配置事务的隔离性和传播特性 -->
	<!-- 相当于一个AOP的切面 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 配置事务的属性定义 -->
		<tx:attributes>
			<!-- 配置具体的方法的事务属性 
				isolation//事务的隔离级别,默认是按数据库的隔离级别来 
				propagation//事务的传播行为,默认是同一个事务 
				timeout="-1":事务的超时时间,默认值使用数据库的超时时间。 
				read-only="false":事务是否只读,默认可读写。 
				rollback-for:遇到哪些异常就回滚,其他的都不回滚 
				no-rollback-for:遇到哪些异常不回滚,其他的都回滚。和上面互斥的 -->
			<tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED"
				timeout="-1" read-only="false" />

			<!-- 支持通配符 要求service中 方法名字必须符合下面的规则 -->
			<tx:method name="save*" />
			<tx:method name="update*" />
			<tx:method name="delete*" />
			<tx:method name="insert*" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="select*" read-only="true" />
			<tx:method name="get*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- 第三步:配置切入点,让通知关联切入点,即事务控制业务层的方法 -->
	<aop:config>
		<!-- 切入点 -->
		<aop:pointcut expression="bean(*Service)" id="txPointcut"/>
		<!-- 切面 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
	</aop:config>
	
</beans>

2.jdbc配置applicationContext-jdbc

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop" 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/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
">

	<!-- 加载资源文件 -->
	<context:property-placeholder location="classpath:/db.properties"/>

	<!-- 开启扫描 -->
	<context:component-scan base-package="com.igeek.crm"/>

	<!-- 配置DataSource -->
	<!-- 使用德鲁伊连接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="url" value="${jdbc.url}"/>
		<property name="driverClassName" value="${jdbc.driverClass}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
		<property name="maxActive" value="${jdbc.maxActive}"/>
	</bean>
	
	<!-- jdbc的模板类 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 通过import引入事务 -->
	<import resource="classpath:/applicationContext-tran.xml"/>
</beans>

db.properties配置:

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/igeekspring
jdbc.username=root
jdbc.password=root
jdbc.maxActive=20

 

Dao类:

package com.igeek.crm.dao;

import java.util.List;

import com.igeek.crm.entity.Book;

/**
 * 
 * TODO
 *
 * 2018年10月13日上午9:12:40
 */
public interface BookDAO {
	public int save(Book book);
	public int delete(int id);
	public int update(Book book);
	public Book queryById(int id);
	public List<Book> queryAll();
}
package com.igeek.crm.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.igeek.crm.dao.BookDAO;
import com.igeek.crm.entity.Book;

/**
 * 
 *         TODO
 *
 *         2018年10月13日上午9:14:05
 */
@Repository
public class BookDAOImpl extends JdbcDaoSupport implements BookDAO {

	@Autowired
	public void setDataSource1(DataSource dataSource){
		super.setDataSource(dataSource);
	}

	@Override
	public int save(Book book) {
		String sql = "INSERT INTO book(name,price) VALUES(?,?)";
		return getJdbcTemplate().update(sql, book.getName(), book.getPrice());
	}

	@Override
	public int delete(int id) {
		return getJdbcTemplate().update("DELETE FROM book WHERE id=?", id);
	}

	@Override
	public int update(Book book) {
		String sql = "UPDATE book SET name=?,price=? WHERE id=?";
		return getJdbcTemplate().update(sql, book.getName(), book.getPrice(), book.getId());

	}

	@Override
	public Book queryById(int id) {
		return getJdbcTemplate().queryForObject("SELECT * FROM book WHERE id = ?", new BookRowMapper(), id);
	}

	@Override
	public List<Book> queryAll() {
		return getJdbcTemplate().query("SELECT * FROM book", new BookRowMapper());
	}

	// 自定义的手动装配的类
	class BookRowMapper implements RowMapper<Book> {
		// 参数1:自动将查询出来的结果集传进来
		// 返回是:封装好的数据对象
		public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
			Book book = new Book();
			// 取当前指针的结果集
			book.setId(rs.getInt(1));
			book.setName(rs.getString(2));
			book.setPrice(rs.getDouble(3));
			return book;
		}
	}

}

 expression切面配置: 

任意公共方法的执行:
execution(public * *(..))
任何一个以“set”开始的方法的执行:
execution(* set*(..))
AccountService 接口的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
定义在service包里的任意方法的执行:
execution(* com.xyz.service.*.*(..))
定义在service包和所有子包里的任意类的任意方法的执行:
execution(* com.xyz.service..*.*(..))
定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
***> 最靠近(..)的为方法名,靠近.*(..))的为类名或者接口名,如上例的JoinPointObjP2.*(..))
 
pointcutexp包里的任意类.
within(com.test.spring.aop.pointcutexp.*)
pointcutexp包和所有子包里的任意类.
within(com.test.spring.aop.pointcutexp..*)
实现了MyInterface接口的所有类,如果MyInterface不是接口,限定MyInterface单个类.
this(com.test.spring.aop.pointcutexp.MyInterface)
***> 当一个实现了接口的类被AOP的时候,用getBean方法必须cast为接口类型,不能为该类的类型.
 
带有@MyTypeAnnotation标注的所有类的任意方法.
@within(com.elong.annotation.MyTypeAnnotation)
@target(com.elong.annotation.MyTypeAnnotation)
带有@MyTypeAnnotation标注的任意方法.
@annotation(com.elong.annotation.MyTypeAnnotation)
***> @within和@target针对类的注解,@annotation是针对方法的注解
 
参数带有@MyMethodAnnotation标注的方法.
@args(com.elong.annotation.MyMethodAnnotation)
参数为String类型(运行是决定)的方法.
args(String)
 

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_15204179/article/details/83050229

智能推荐

JVM内存模型&&垃圾回收_java 堆和 native 堆之间来回复制数据-程序员宅基地

文章浏览阅读189次。概述:虚拟机自动内存管理机制下,不再需要像C/C++程序开发程序员这样为内一个new 操作去写对应的delete/free操作,不容易出现内存泄漏和内存溢出问题。正是因为Java程序员把内存控制权利交给Java虚拟机,一旦出现内存泄漏和溢出方面的问题,如果不了解虚拟机是怎样使用内存的,那么排查错误将会是一个非常艰巨的任务运行时数据区域:Java虚拟机在执行Java程序的过程中会把它管理的内存..._java 堆和 native 堆之间来回复制数据

linux 开启审计服务,Linux审计服务Auditd systemctl重启问题解决-程序员宅基地

文章浏览阅读1.5k次。在RHEL7&&CentOS7时代,默认的服务通过systemd控制,并通过systemctl命令完成启停。但是并不是所有的服务都可以完美的通过systemctl来控制,比如今天要提到的Auditd编辑audit.rules添加规则后,当然要通过restart服务来重启生效,但是通过systemctl restart auditd就会报如下错误:[root@abc]# syste..._failed to restart auditd.service: operation refused, unit auditd.service may

设计模式17 - 中介者模式-程序员宅基地

文章浏览阅读259次。作者:billy版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处中介者模式使用场景优缺点注意事项UML结构图代码实现..._中介者模式

【剑指offer】面试题18:树的子结构_{ *t=(bitree )malloc(sizeof(bitnode)); if(!*t) exi-程序员宅基地

文章浏览阅读552次。题目描述:输入两颗二叉树A,B,判断B是不是A的子结构。题目解析:_{ *t=(bitree )malloc(sizeof(bitnode)); if(!*t) exit(-1); (*t)->data

IIS占用CPU%问题排查方案 _排查iis消耗资源语句-程序员宅基地

文章浏览阅读2.6k次。有时候辛辛苦苦写个网站,挂到服务器上一看,CPU给百分百了,这种问题百分之八九十都是因为代码写有问题,而不是因为系统设置的问题,这种问题也比较难排查。但是结合一些工具也可以找到原因的,关于windbg的使用,好像有本关于.net调试的书,大家有兴趣可以看看,网上也有一些帖子,但关于ANTS Profiler的帖子就比较少了,大家也可以下载试用版来帮助解决问题1 准备日志1.1 _排查iis消耗资源语句

Springboot 项目导出word文档(文档内容包括数据以及服务器图片)_项目实体类批量导出到word-程序员宅基地

文章浏览阅读1.4w次。Springboot 项目freemarker导出word文档(文档内容包括数据以及服务器图片)前些天有需求要完成导出word文档功能,基础数据导出word文档,网上也能搜到很多源代码,但是我这边要求是服务器上的图片(只给出服务器图片路径,从服务器得到图片),前前后后加起来就不好内容了,网上并没有找到处理这种的代码和解决方式,只好自己写了,弄完了来记录下,防止以后再用.首先动手之前整理下思..._项目实体类批量导出到word

随便推点

SparkSql中的repartition 与 coalesce_spark sql repartition-程序员宅基地

文章浏览阅读3.9k次。SparkSql的repartition和coalesceSparkSql 写hive小文件后记repartition(numPartitions:Int)和coalesce(numPartitions:Int,shuffle:Boolean=false)作用:对RDD的分区进行重新划分,repartition内部调用了coalesce,参数shuffle为true例:RDD有N个分区,需要..._spark sql repartition

【Android】帧动画(Drawable Animation)_帧动画 一个drawable-程序员宅基地

文章浏览阅读650次。今天总结下帧动画 原理: 对几张图片按照顺序一张一张进行播放,视觉上感觉是连续播放的动画效果。 一.步骤: 1.在res/drawable放入几张图片 2.在res/drawable下新建一个drawable resource file,比如命名为gril_animation.xml,根节点选择animation-list,点击ok 3.编辑gril_animation.xml文件_帧动画 一个drawable

树莓派python交互界面实例_树莓派综合项目2:智能小车(二)tkinter图形界面控制...-程序员宅基地

文章浏览阅读858次。一、介绍树莓派综合项目2:智能小车(一)四轮驱动中,实现了代码输入对四个电机的简单控制,本章将使用Python 的图形开发界面的库——Tkinter 模块(Tk 接口),编写本地运行的图形界面,控制小车的前进后退、转向和原地转圈。Tkinter是Python的标准GUI库,Python使用Tkinter可以快速的创建 GUI 应用程序。由于 Tkinter 是内置到 python 的安装包中、只要..._树莓派 例子 交互

Yarn install报错 【错误: 找不到或无法加载主类 install】_yarn install error: could not find or load main cl-程序员宅基地

文章浏览阅读6.5k次,点赞4次,收藏12次。今天要运行一个项目,首先在初始配置阶段要执行 yarn install 结果出现了下面的报错: 执行 yarn --version报错如下: 执行 yarn version结果如下: 网上查资料的时候看到一篇相关的文章,可以参考解决问题: 这也是我尝试解决问题的思路,这里给出来的说法是,原始的java安装环境里面已经有yarn了,新安装的yarn的环境变量需要放在它前面。 接下来下载yarn,我是在..._yarn install error: could not find or load main class install

CSS实现div竖向排版_div竖着排列-程序员宅基地

文章浏览阅读2.3k次。1.最多显示四行,自动换列 2.均分成3列_div竖着排列

数学系教材推荐-程序员宅基地

文章浏览阅读323次。 解析几何解析几何有被代数吃掉的趋势,不过就数学系的学生而言,还是应该好好学一下,我大一没有好好学,后来学别的课时总感觉哪里有些不太对劲,后来才发现是自己的数学功底尤其是几何得功底没有打好。1吴光磊《解析几何简明教程》高等教育出版社写的简单明了,我基础没有打好,快速翻了一下这本书收获还是不少的。不过打基础的时候还是从下面三本选一本看,把这本当参考书。2《解析几何》丘维声,北京大...

推荐文章

热门文章

相关标签