spring配置c3p0连接池、spring的声明式事务管理
生活随笔
收集整理的這篇文章主要介紹了
spring配置c3p0连接池、spring的声明式事务管理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、spring配置c3p0連接池:
1、導(dǎo)入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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- spring配置c3p0連接池 start --><!-- 1.配置連接池 --><!-- 1.1創(chuàng)建連接池對象,dataSource的名字不能改變 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!-- 1.2設(shè)置連接池的屬性 --><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.創(chuàng)建jdbcTemplate對象,并注入連接池對象 --><bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 3.創(chuàng)建userDao對象,并注入jdbctemplate對象 --><bean id="userDao" class="com.zwp.dao.UserDao"><property name="jdbcTemplate" ref="jdbctemplate"></property></bean><!-- 4.創(chuàng)建userService對象,并注入userDao對象 --><bean id="userService" class="com.zwp.service.UserService"><property name="userDao" ref="userDao"></property></bean><!-- spring配置c3p0連接池 end --> </beans>3、相關(guān)類的代碼:
public class UserDao {private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}public void add(){System.out.println("UserDao調(diào)用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調(diào)用dao...");userDao.add();} }4、測試類:
public class Test2 {@Testpublic void test6(){ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");UserService userService = (UserService) context.getBean("userService");System.out.println("調(diào)用service...");userService.add();} }5、運(yùn)行結(jié)果:
二、spring的聲明式事務(wù)管理:
spring的聲明式事務(wù)管理有兩種實(shí)現(xiàn):
(1)基于xml配置文件的實(shí)現(xiàn);
(2)基于注解方式的實(shí)現(xiàn);
1、基于xml配置文件實(shí)現(xiàn):
步驟:第一步:配置連接池;
第二步:配置事務(wù)管理器;
第三步:配置事務(wù)增強(qiáng);
第四步:配置切面;
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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 聲明式事務(wù)管理-基于xml配置文件實(shí)現(xiàn)start --><!-- 1.配置連接池 --><!-- 1.1創(chuàng)建連接池對象 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!-- 1.2設(shè)置連接池的屬性 --><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.配置事務(wù)管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 6.配置事務(wù)增強(qiáng) --><tx:advice id="txadvice" transaction-manager="transactionManager"><!-- 6.1 做事務(wù)操作 --><tx:attributes><!-- 6.2 設(shè)置進(jìn)行事務(wù)操作的方法匹配規(guī)則 --><tx:method name="account" propagation="REQUIRED"/></tx:attributes></tx:advice><!-- 7.配置切面 --><aop:config><!-- 7.1 配置切入點(diǎn) --><aop:pointcut expression="execution(* com.zwp.service.UserService.*(..))" id="pointcut1"/><!-- 7.2 配置切面 --><aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/></aop:config> <!-- 聲明式事務(wù)管理-基于xml配置文件實(shí)現(xiàn)end --> </beans> public class UserService {private UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao = userDao;}public void account(){//小明增加1000userDao.increase();//出現(xiàn)異常:int i=10/0;//小李減少1000userDao.decrease();} } public class Test2 {@Testpublic void test6(){ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");UserService userService = (UserService) context.getBean("userService");System.out.println("調(diào)用service...");userService.account();;} }運(yùn)行結(jié)果:在執(zhí)行account()方法是,出現(xiàn)異常,但是發(fā)生異常前的數(shù)據(jù)庫操作沒有被保存到數(shù)據(jù)庫,說明配置成功了。
2、基于注解方式的實(shí)現(xiàn):
步驟:第一步:配置連接池;
第二步:配置事務(wù)管理器;
第三步:開啟事務(wù)注解;
第四步:在要使用事務(wù)的方法所在類上面添加注解@Transactional。
spring配置文件配置如下:
總結(jié)
以上是生活随笔為你收集整理的spring配置c3p0连接池、spring的声明式事务管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spirng使用Aspectj实现AOP
- 下一篇: mybatis框架--学习笔记(上)