赞
踩
建立一个简单的JDBC应用,设置数据库的事务而不用写JDBC代码。
pom文件
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.4.3</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example</groupId>
- <artifactId>managing-transactions</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>managing-transactions</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.13</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jdbc</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
写一个基于JDBC服务,将学生姓名登记到系统中。
新建src/main/java/com/example/managingtransactions/BookingService.java
- package com.example.managingtransactions;
-
- import java.util.List;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.stereotype.Component;
- import org.springframework.transaction.annotation.Transactional;
-
- @Component
- public class BookingService {
-
- private final static Logger logger = LoggerFactory.getLogger(BookingService.class);
-
- private final JdbcTemplate jdbcTemplate;
-
- public BookingService(JdbcTemplate jdbcTemplate) {
- this.jdbcTemplate = jdbcTemplate;
- }
-
- @Transactional
- public void book(String... persons) {
- for (String person : persons) {
- logger.info("Booking " + person + " in a seat...");
- jdbcTemplate.update("insert into BOOKINGS(FIRST_NAME) values (?)", person);
- }
- }
-
- public List<String> findAllBookings() {
- return jdbcTemplate.query("select FIRST_NAME from BOOKINGS",
- (rs, rowNum) -> rs.getString("FIRST_NAME"));
- }
-
- }
这里有一个book方法,使用JdbcTemple通过循环将每个学生插入到BOOKINGS表中。该方法使用@Transactional标记,意思是整个执行过程中,有任何失败都会回滚到之前的状态。也就是说,如果任何一个学生添加失败,就不会有学生添加到BOOKINGS表中。
新建src/main/java/com/example/managingtransactions/ManagingTransactionsApplication.java
- package com.example.managingtransactions;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class ManagingTransactionsApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(ManagingTransactionsApplication.class, args);
- }
-
- }
@SpringBootApplication,是简单的使用方法它包括:
@Configuration,将一个类作为bean定义的源代码添加到应用上下文中。
@EnableAutoConfiguration,告诉SpringBoot启动的时候将classpath路径下的、其他的bean和各种配置添加进来。例如,如果spring-webmvc在classpath中,那么该标记将应用作为一个web应用激活关键的行为,比如说建立DispatcherServlet。
@ComponentScan,告诉Spring在com/exampe下查找其他components、configurations和services,让他们能够找到controllers。
main方法使用SpringApplication.run()启动一个应用,你可能发现没有任何XML文件,没有web.xml文件。该web应用是100%纯的java代码。
该应用没有任何的配置。Spring Boot监测到spring-jdbc和h2在classpath中,然后会自动创建一个DataSource和一个JdbcTemplate。以上这些由DataSourceTransactionManager进行管理。
使用CommandLineRunner一样可以达到这样的效果。
新建src/main/java/com/example/managingtransactions/AppRunner.java
- package com.example.managingtransactions;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.boot.CommandLineRunner;
- import org.springframework.stereotype.Component;
- import org.springframework.util.Assert;
-
- @Component
- class AppRunner implements CommandLineRunner {
-
- private final static Logger logger = LoggerFactory.getLogger(AppRunner.class);
-
- private final BookingService bookingService;
-
- public AppRunner(BookingService bookingService) {
- this.bookingService = bookingService;
- }
-
- @Override
- public void run(String... args) throws Exception {
- bookingService.book("Alice", "Bob", "Carol");
- Assert.isTrue(bookingService.findAllBookings().size() == 3,
- "First booking should work with no problem");
- logger.info("Alice, Bob and Carol have been booked");
- try {
- bookingService.book("Chris", "Samuel");
- } catch (RuntimeException e) {
- logger.info("v--- The following exception is expect because 'Samuel' is too " +
- "big for the DB ---v");
- logger.error(e.getMessage());
- }
-
- for (String person : bookingService.findAllBookings()) {
- logger.info("So far, " + person + " is booked.");
- }
- logger.info("You shouldn't see Chris or Samuel. Samuel violated DB constraints, " +
- "and Chris was rolled back in the same TX");
- Assert.isTrue(bookingService.findAllBookings().size() == 3,
- "'Samuel' should have triggered a rollback");
-
- try {
- bookingService.book("Buddy", null);
- } catch (RuntimeException e) {
- logger.info("v--- The following exception is expect because null is not " +
- "valid for the DB ---v");
- logger.error(e.getMessage());
- }
-
- for (String person : bookingService.findAllBookings()) {
- logger.info("So far, " + person + " is booked.");
- }
- logger.info("You shouldn't see Buddy or null. null violated DB constraints, and " +
- "Buddy was rolled back in the same TX");
- Assert.isTrue(bookingService.findAllBookings().size() == 3,
- "'null' should have triggered a rollback");
- }
-
- }
通过命令行执行
mvn spring-boot:run
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。