spring配置c3p0连接池、spring的声明式事务管理

spring配置c3p0连接池、spring的声明式事务管理

一、spring配置c3p0连接池:

1、导入maven依赖:

<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>

2、在spring配置文件中配置连接池:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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">

	<!-- spring配置c3p0连接池 start -->
	<!-- 1.配置连接池 -->
	<!-- 1.1创建连接池对象,dataSource的名字不能改变 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 1.2设置连接池的属性 -->
		<property name="driverClass" value="com.mysql/jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///test-ssm"></property>
		<property name="user" value="root"></property>
		<property name="password" value="admin"></property>
	</bean>
	<!-- 2.创建jdbcTemplate对象,并注入连接池对象 -->
	<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 3.创建userDao对象,并注入jdbctemplate对象 -->
	<bean id="userDao" class="com.zwp.dao.UserDao">
		<property name="jdbcTemplate" ref="jdbctemplate"></property>
	</bean>
	<!-- 4.创建userService对象,并注入userDao对象 -->
	<bean id="userService" class="com.zwp.service.UserService">
		<property name="userDao" ref="userDao"></property>
	</bean>
	<!-- spring配置c3p0连接池 end -->
</beans>

3、相关类的代码:

public class UserDao {
	
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public void add(){
		System.out.println("UserDao调用JdbcTemplate..");
		String sql="insert into user values(?,?)";
		jdbcTemplate.update(sql,"小张","666");
	}
}
public class UserService {
	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void add(){
		System.out.println("service调用dao...");
		userDao.add();
	}
}

4、测试类:

public class Test2 {
	@Test
	public void test6(){
		ApplicationContext context=
				new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		
		UserService userService = (UserService) context.getBean("userService");
		System.out.println("调用service...");
		userService.add();
	}
}

5、运行结果:

spring配置c3p0连接池、spring的声明式事务管理

二、spring的声明式事务管理:

spring的声明式事务管理有两种实现:

(1)基于xml配置文件的实现;

(2)基于注解方式的实现;

1、基于xml配置文件实现:

步骤:第一步:配置连接池;

第二步:配置事务管理器;

第三步:配置事务增强;

第四步:配置切面;

spring配置文件配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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">


	<!-- 声明式事务管理-基于xml配置文件实现start -->
	<!-- 1.配置连接池 -->
	<!-- 1.1创建连接池对象 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 1.2设置连接池的属性 -->
		<property name="driverClass" value="com.mysql/jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///test-ssm"></property>
		<property name="user" value="root"></property>
		<property name="password" value="admin"></property>
	</bean>
	<!-- 5.配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 6.配置事务增强 -->
	<tx:advice id="txadvice" transaction-manager="transactionManager">
		<!-- 6.1 做事务操作 -->
		<tx:attributes>
			<!-- 6.2 设置进行事务操作的方法匹配规则 -->
			<tx:method name="account" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	<!-- 7.配置切面 -->
	<aop:config>
		<!-- 7.1 配置切入点 -->
		<aop:pointcut expression="execution(* com.zwp.service.UserService.*(..))" id="pointcut1"/>
		<!-- 7.2 配置切面 -->
		<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
	</aop:config>	
	<!-- 声明式事务管理-基于xml配置文件实现end -->
</beans>
public class UserService {
	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void account(){
		//小明增加1000
		userDao.increase();
		
		//出现异常:
		int i=10/0;
		
		//小李减少1000
		userDao.decrease();
	}
}
public class Test2 {

	@Test
	public void test6(){
		ApplicationContext context=
				new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		
		UserService userService = (UserService) context.getBean("userService");
		System.out.println("调用service...");
		userService.account();;
	}
}

运行结果:在执行account()方法是,出现异常,但是发生异常前的数据库操作没有被保存到数据库,说明配置成功了。

2、基于注解方式的实现:

步骤:第一步:配置连接池;

第二步:配置事务管理器;

第三步:开启事务注解;

第四步:在要使用事务的方法所在类上面添加注解@Transactional。

spring配置文件配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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">



	<!-- spring配置c3p0连接池 start -->
	<!-- 1.配置连接池 -->
	<!-- 1.1创建连接池对象 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 1.2设置连接池的属性 -->
		<property name="driverClass" value="com.mysql/jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///test-ssm"></property>
		<property name="user" value="root"></property>
		<property name="password" value="admin"></property>
	</bean>
	<!-- spring配置c3p0连接池 end -->
	
	<!-- 5.配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>
//第四步:在要使用事务的方法所在类上面添加注解@Transactional
@Transactional
public class UserService {
	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void account(){
		//小明增加1000
		userDao.increase();
		
		//出现异常:
		int i=10/0;
		
		//小李减少1000
		userDao.decrease();
	}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/114730.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • spring boot 测试的几种写法

    spring boot 测试的几种写法

    2021年5月12日
  • 免费google代理服务器_google服务器ip地址

    免费google代理服务器_google服务器ip地址有些朋友在登陆谷歌时遇到异常活动而出验证,但是无论怎么输入手机号谷歌都提示此手机号无法用于验证,这种问题大多人都会出现,滥用代理基本都会出现异常验证活动的.谷歌过程中过不了手机号验证,但是在网络上搜一大堆教程也没有用,说什么用QQ邮箱注册、网易邮箱注册等等,这些方法都烂大街了,滥用这些APP接口注册都会被谷歌封号处理的,请大家不要被网上诸多教程误解…

    2022年9月29日
  • Google Borg论文[通俗易懂]

    Google Borg论文[通俗易懂]Borg的论文逐字翻译,拒绝机器翻译,有一些自己的理解,不一定对,作为参考就行

    2022年10月22日
  • 如何彻底卸载清理MySQL

    如何彻底卸载清理MySQL我们因为各种各样的原因可能需要卸载MySQL或者卸载重装,但是如果MySQL不能清理干净的话是很容易出现问题的。本文就讲讲如何彻底的卸载MySQL,将MySQL从我们的电脑上清理干净。大体上分为三个步骤:卸载软件本身。清除残留文件(☆☆☆☆☆)。清理注册表。1.卸载软件本身首先就是把MySQL软件本身卸载掉,卸载的方式有很多种。可以通过各种软件管家卸载掉,也可以通过计算机自带的控…

    2022年6月25日
  • CSS样式表的引入方式

    CSS样式表的引入方式CSS初识CSS(CascadingStyleSheets)美化样式CSS通常称为CSS样式表或层叠样式表(级联样式表),主要用于设置HTML页面中的文本内容(字体、大小、对齐方式等)、图片的外形(宽高、边框样式、边距等)以及版面的布局等外观显示样式。CSS以HTML为基础,提供了丰富的功能,如字体、颜色、背景的控制及整体排版等CSS的优点1.内容与表现分离。2.网页的表现统一,容易修改。3.丰富的样式,使得页面布局更加灵活4.减少网页的代码量,增加网页的浏览速度。5.运用独立于页面

    2022年7月14日
  • pycharm terminal 进入虚拟环境_pycharm failed to create virtual

    pycharm terminal 进入虚拟环境_pycharm failed to create virtualPycharmterminal激活虚拟环境,首先需要保证系统完成了conda的安装,并在Powershell中完成虚拟环境的创建(操作创建的虚拟环境名称为deep_pool,这个虚拟环境在接下来的操作中会被提及到)。如果不会创建虚拟环境,可以参考下面这个流程:Ubuntu20.4安装Anaconda以及过程中遇到的问题(已解决)_qq_53258482的博客-CSDN博客在虚拟环境创建完成后,在powershell中输入命令Set-ExecutionPolicy-ScopeCurrentUse

    2022年8月25日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号