学习Spring事务操作-已编写事务代码
This commit is contained in:
@@ -4,12 +4,16 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com")
|
||||
@EnableTransactionManagement // 启用事务管理
|
||||
public class Application {
|
||||
@Bean
|
||||
public JdbcTemplate jdbcTemplate(DataSource dataSource){
|
||||
@@ -25,4 +29,9 @@ public class Application {
|
||||
dataSource.setPassword("root");
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager txManager(DataSource dataSource){
|
||||
return new DataSourceTransactionManager(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,15 @@ package com.ycjy.dao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Repository
|
||||
public class DeptDao1 {
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
public void insert(){
|
||||
String sql = "insert into a1 values('a3')";
|
||||
public void insert(String d1){
|
||||
String sql = "insert into a1 values('"+d1+"')";
|
||||
jdbcTemplate.update(sql);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,15 @@ package com.ycjy.dao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Repository
|
||||
public class DeptDao2 {
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
public void insert(){
|
||||
String sql = "insert into userinfo(username) values('b3')";
|
||||
public void insert(String d2){
|
||||
String sql = "insert into userinfo(username) values('"+d2+"')";
|
||||
jdbcTemplate.update(sql);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import com.ycjy.dao.DeptDao1;
|
||||
import com.ycjy.dao.DeptDao2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class DeptService {
|
||||
@@ -12,8 +13,9 @@ public class DeptService {
|
||||
@Autowired
|
||||
DeptDao2 deptDao2;
|
||||
|
||||
@Transactional
|
||||
public void update() {
|
||||
deptDao1.insert();
|
||||
deptDao2.insert();
|
||||
deptDao1.insert("ddd1");
|
||||
deptDao2.insert("ddd2");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user