1、使用JdbcTemplate需要引入的依赖(spring的maven项目)
<!-- jdbcTemplate实现jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- 数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> <!-- 事务管理 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!--上下文--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.22</version> </dependency> <!--beans依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.23</version> </dependency> <!--core核心--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.22</version> </dependency>
2、环境搭配----bean.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--组件扫描--> <context:component-scan base-package="com.lxh.spring5"></context:component-scan> <!--数据库连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="jdbc:mysql://localhost/user_db"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="username" value="root"></property> <property name="password" value="laixinghai"></property> </bean> <!--JDBCTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!--注入datasource--> <property name="dataSource" ref="dataSource"></property> </bean> <!--创建事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--开启事务注解--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans>
三、dao层代码
UserDao接口
public interface UserDao { // 加钱 public void addMoney(); // 减钱 public void reduceMoney(); }
UserDaoImpl实现类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl implements UserDao{ // 注入JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; // 加钱 @Override public void addMoney() { String sql = "update t_account set money=money+? where username=?"; Object[] obj = new Object[] {100, "mary"}; int update = jdbcTemplate.update(sql, obj); System.out.println(update); } // 减钱 @Override public void reduceMoney() { String sql = "update t_account set money=money-? where username=?"; Object[] obj = {100, "lucy"}; int update = jdbcTemplate.update(sql, obj); System.out.println(update); } }
4、service层代码
import com.lxh.spring5.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Service(value = "userService") // 事务注解 // propagation:传播行为 // isolation:隔离级别 @Transactional(propagation = Propagation.MANDATORY, isolation = Isolation.READ_COMMITTED) public class UserService { // 注入dao @Autowired private UserDao userDao; // 转账的方法 public void accountMoney(){ // 原始方式 // try { // // 1.开启事务 // // // 2.进行业务操作 // // lucy减100 // userDao.reduceMoney(); // // 添加异常 // int i = 10/0; // // mary加100 // userDao.addMoney(); // // // 3.没有异常提交事务 // }catch (Exception e) { // // 4.出现异常事务回滚 // } // 加Transactional注解后 // lucy减100 userDao.reduceMoney(); // 添加异常 int i = 10/0; // mary加100 userDao.addMoney(); } }
5、测试代码
import com.lxh.spring5.service.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo { @Test public void testAccountMoney() { ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); UserService userService = context.getBean("userService", UserService.class); userService.accountMoney(); } }
标签:事务,JavaSpring,spring,update,springframework,jdbcTemplate,org,import,public From: https://www.cnblogs.com/lxh-daniel/p/16755014.html