首页 > 学院 > 开发设计 > 正文

spring对jdbc事务控制

2019-11-06 06:21:55
字体:
来源:转载
供稿:网友

pojo类

package com.pactera.pojo;public class User { public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } PRivate String name; private int age; @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; }}

IUserDao

package com.pactera.dao;import com.pactera.pojo.User;public interface IUserDao { void save(User user);}

UserDao

package com.pactera.daoImpl;import javax.annotation.Resource;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;import com.pactera.dao.IUserDao;import com.pactera.pojo.User;@Repositorypublic class UserDao implements IUserDao{ @Resource private JdbcTemplate jdbcTemplate; @Override public void save(User user) { String sql = "insert into user(name,age) values(?,?)"; jdbcTemplate.update(sql,user.getName(),user.getAge()); }}

IUserService

package com.pactera.service;import com.pactera.pojo.User;public interface IUserService { void save(User user);}

UserService

package com.pactera.serviceImpl;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.pactera.dao.IUserDao;import com.pactera.pojo.User;import com.pactera.service.IUserService;@Servicepublic class UserService implements IUserService{ @Resource private IUserDao userDao; @Override public void save(User user) { userDao.save(user); int i=1/0; userDao.save(user); }}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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 id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:MySQL://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8"> </property> <property name="user" value="root"></property> <property name="passWord" value="root"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="50"></property> <property name="acquireIncrement" value="5"></property> </bean> <!-- 创建jdbcTemplate对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 开启注解扫描 --> <context:component-scan base-package="com.pactera"></context:component-scan> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 声明式事务 --> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="query" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- aop事务切面 --> <aop:config> <aop:pointcut id="myPointcut" expression="execution(* com.pactera.service.*.*(..))" /> <!--将定义好的事务处理策略应用到上述的切入点 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" /> </aop:config></beans>

测试

private IUserService userService; @Before public void init(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); this.userService = ac.getBean("userService",IUserService.class); } @Test public void test(){ User user = new User(); user.setName("hello"); user.setAge(15); userService.save(user); }

配 有jdbc事务控制的时候service层中出现异常,再抛出异常之前对数据库的操作会回滚


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表