一、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的声明式事务管理:
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();
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/114730.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...