diff --git a/spring_tx_01/.gitignore b/spring_tx_01/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/spring_tx_01/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/spring_tx_01/pom.xml b/spring_tx_01/pom.xml new file mode 100644 index 0000000..022d44a --- /dev/null +++ b/spring_tx_01/pom.xml @@ -0,0 +1,102 @@ + + + 4.0.0 + + org.example + spring_tx_01 + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + + org.springframework + spring-context + 6.0.11 + + + + org.springframework + spring-aspects + 6.0.11 + + + + org.springframework + spring-test + 6.0.11 + test + + + + aopalliance + aopalliance + 1.0 + + + + com.alibaba + druid + 1.1.10 + + + + mysql + mysql-connector-java + 8.0.28 + + + + org.springframework + spring-jdbc + 6.0.11 + + + + org.springframework + spring-tx + 6.0.11 + + + + org.springframework + spring-orm + 6.0.11 + + + + commons-logging + commons-logging + 1.2 + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.14.0 + test + + + + junit + junit + 4.13.2 + test + + + + org.projectlombok + lombok + 1.18.12 + provided + + + + \ No newline at end of file diff --git a/spring_tx_01/src/main/java/com/msb/config/SpringConfig.java b/spring_tx_01/src/main/java/com/msb/config/SpringConfig.java new file mode 100644 index 0000000..7c5fbfd --- /dev/null +++ b/spring_tx_01/src/main/java/com/msb/config/SpringConfig.java @@ -0,0 +1,85 @@ +package com.msb.config; + +import com.alibaba.druid.pool.DruidDataSource; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.sql.DataSource; + +//@Configuration // 配置类注解 +//@ComponentScan(basePackages = "com.msb") // Spring 包扫描 +//@PropertySource("classpath:jdbc.properties") // 读取配置文件 +//@EnableTransactionManagement // 开启事务 +public class SpringConfig { + @Value("${jdbc_username}") + private String username; + @Value("${jdbc_password}") + private String pwd; + @Value("${jdbc_url}") + private String url; + @Value("${jdbc_driver}") + private String driver; + + + /** + * 加载特鲁伊数据库连接池 + * + * + * + * + * + * + * @return + */ + @Bean + public DruidDataSource getDruidDataSource(){ + DruidDataSource dataSource = new DruidDataSource(); + dataSource.setUsername(username); + dataSource.setPassword(pwd); + dataSource.setDriverClassName(driver); + dataSource.setUrl(url); + + return dataSource; + } + + /** + * 获取jdbcTemplate 对象 + * + * + * + * + * + * @param dataSource + * @return + */ + @Bean + public JdbcTemplate getJdbcTemplate(DataSource dataSource){ + JdbcTemplate jdbcTemplate = new JdbcTemplate(); + jdbcTemplate.setDataSource(dataSource); + + return jdbcTemplate; + } + + /** + * + * + * + * + * + */ + @Bean + public PlatformTransactionManager getDataSourceTransactionManager(DataSource dataSource){ + DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); + dataSourceTransactionManager.setDataSource(dataSource); + + return dataSourceTransactionManager; + } + +} diff --git a/spring_tx_01/src/main/java/com/msb/dao/AccountDao.java b/spring_tx_01/src/main/java/com/msb/dao/AccountDao.java new file mode 100644 index 0000000..e6fac2d --- /dev/null +++ b/spring_tx_01/src/main/java/com/msb/dao/AccountDao.java @@ -0,0 +1,5 @@ +package com.msb.dao; + +public interface AccountDao { + int changeMoneyById(int id, int money); +} diff --git a/spring_tx_01/src/main/java/com/msb/dao/impl/AccountDaoImpl.java b/spring_tx_01/src/main/java/com/msb/dao/impl/AccountDaoImpl.java new file mode 100644 index 0000000..0d7f356 --- /dev/null +++ b/spring_tx_01/src/main/java/com/msb/dao/impl/AccountDaoImpl.java @@ -0,0 +1,21 @@ +package com.msb.dao.impl; + +import com.msb.dao.AccountDao; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; + +@Repository +public class AccountDaoImpl implements AccountDao { + private final JdbcTemplate jdbcTemplate; + + public AccountDaoImpl(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + @Override + public int changeMoneyById(int id, int money) { + String sql = "update account set money=money+? where id = ?"; + + return jdbcTemplate.update(sql, money, id); + } +} diff --git a/spring_tx_01/src/main/java/com/msb/pojo/Account.java b/spring_tx_01/src/main/java/com/msb/pojo/Account.java new file mode 100644 index 0000000..d73b5a1 --- /dev/null +++ b/spring_tx_01/src/main/java/com/msb/pojo/Account.java @@ -0,0 +1,14 @@ +package com.msb.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor +@Data +public class Account { + private Integer id; + private String name; + private Integer money; +} diff --git a/spring_tx_01/src/main/java/com/msb/service/AccountService.java b/spring_tx_01/src/main/java/com/msb/service/AccountService.java new file mode 100644 index 0000000..ee814fd --- /dev/null +++ b/spring_tx_01/src/main/java/com/msb/service/AccountService.java @@ -0,0 +1,5 @@ +package com.msb.service; + +public interface AccountService { + int transMoney(int form, int to, int money); +} diff --git a/spring_tx_01/src/main/java/com/msb/service/impl/AccountServiceImpl.java b/spring_tx_01/src/main/java/com/msb/service/impl/AccountServiceImpl.java new file mode 100644 index 0000000..f87a904 --- /dev/null +++ b/spring_tx_01/src/main/java/com/msb/service/impl/AccountServiceImpl.java @@ -0,0 +1,30 @@ +package com.msb.service.impl; + +import com.msb.dao.AccountDao; +import com.msb.dao.impl.AccountDaoImpl; +import com.msb.service.AccountService; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +//@Transactional // 代表当前类下面的所有类都开启事务 +public class AccountServiceImpl implements AccountService { + private final AccountDao accountDao; + + public AccountServiceImpl(AccountDao accountDao){ + this.accountDao = accountDao; + } + + @Override + @Transactional // 开启事务 + public int transMoney(int form, int to, int money) { + int rows = 0; + rows += accountDao.changeMoneyById(form, 0-money); + + // int i = 1/0; // 手动生产异常,测试事务 + + rows += accountDao.changeMoneyById(to, money); + + return rows; + } +} diff --git a/spring_tx_01/src/main/resources/applicationContext.xml b/spring_tx_01/src/main/resources/applicationContext.xml new file mode 100644 index 0000000..1702403 --- /dev/null +++ b/spring_tx_01/src/main/resources/applicationContext.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring_tx_01/src/main/resources/jdbc.properties b/spring_tx_01/src/main/resources/jdbc.properties new file mode 100644 index 0000000..4dcfa8d --- /dev/null +++ b/spring_tx_01/src/main/resources/jdbc.properties @@ -0,0 +1,4 @@ +jdbc_username=root +jdbc_password=12345678 +jdbc_driver=com.mysql.cj.jdbc.Driver +jdbc_url=jdbc:mysql://localhost:3306/learnjdbc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true \ No newline at end of file diff --git a/spring_tx_01/src/main/resources/log4j2.xml b/spring_tx_01/src/main/resources/log4j2.xml new file mode 100644 index 0000000..e9d972c --- /dev/null +++ b/spring_tx_01/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring_tx_01/src/test/java/com/msb/test/SpringTest.java b/spring_tx_01/src/test/java/com/msb/test/SpringTest.java new file mode 100644 index 0000000..e96c4df --- /dev/null +++ b/spring_tx_01/src/test/java/com/msb/test/SpringTest.java @@ -0,0 +1,22 @@ +package com.msb.test; + + +import com.msb.service.AccountService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:applicationContext.xml") +public class SpringTest { + @Autowired + AccountService accountService; + + @Test + public void testTranMoney(){ + int rows = accountService.transMoney(1,2,100); + System.out.println(rows); + } +} diff --git a/spring_tx_01/src/test/java/com/msb/test/TestTx.java b/spring_tx_01/src/test/java/com/msb/test/TestTx.java new file mode 100644 index 0000000..a6cfdea --- /dev/null +++ b/spring_tx_01/src/test/java/com/msb/test/TestTx.java @@ -0,0 +1,27 @@ +package com.msb.test; + +import com.msb.config.SpringConfig; +import com.msb.service.AccountService; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class TestTx { + + @Test + public void testTranMoney(){ + ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); + AccountService accountService = applicationContext.getBean(AccountService.class); + int rows = accountService.transMoney(1, 2, 100); + System.out.println(rows); + } + + @Test + public void testSpringConfig(){ + ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); + AccountService accountService = context.getBean(AccountService.class); + int rows = accountService.transMoney(1, 2, 100); + System.out.println(rows); + } +}