(一)springboot提供了JdbcTemplate类来快捷的实现操作数据库,记录如下:
pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.edu.spring</groupId> <artifactId>springboot_web</artifactId> <version>1.0.0</version> <name>springboot_web</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> </project>
application.properties
spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3309/springboot spring.datasource.username=root spring.datasource.password=123456
ProductDao.java
package com.edu.spring.springboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class ProductDao { @Autowired private JdbcTemplate jdbcTemplate; public void add(String name){ String sql="insert into t_product(pname) values('"+name+"')"; jdbcTemplate.execute(sql); } }
App.java
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class App { public static void main(String[] args) throws Exception{ ConfigurableApplicationContext context=SpringApplication.run(App.class, args); context.getBean(ProductDao.class).add("测试1"); context.close(); } }
运行结果如下:
(二)springboot中使用Transactional注解处理事务(其实是spring中的注解)
(1)默认运行时异常才会回滚,否需要rollbackFor指定:如@Transactional(rollbackFor=Exception.class)表示所有异常回滚。
App.java
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class App { public static void main(String[] args) throws Exception{ ConfigurableApplicationContext context=SpringApplication.run(App.class, args); context.getBean(ProductDao.class).addBitch("测试1","测试2","测试3","测试4"); context.close(); } }
1、测试不加注解的情况:
package com.edu.spring.springboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class ProductDao { @Autowired private JdbcTemplate jdbcTemplate; public void addBitch(String... names) throws Exception{ for(String name:names){ String sql="insert into t_product(pname) values('"+name+"')"; jdbcTemplate.execute(sql); if("".equals("")){ throw new NullPointerException(); } } } }
运行结果,没有回滚,如下:
2、测试添加默认注解,抛出运行时异常的情况:
package com.edu.spring.springboot; 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 ProductDao { @Autowired private JdbcTemplate jdbcTemplate; @Transactional public void addBitch(String... names) throws Exception{ for(String name:names){ String sql="insert into t_product(pname) values('"+name+"')"; jdbcTemplate.execute(sql); if("".equals("")){ throw new NullPointerException(); } } } }
运行结果,回滚,如下:
3、测试添加默认注解,抛出检查异常的情况:
package com.edu.spring.springboot; import java.io.FileNotFoundException; 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 ProductDao { @Autowired private JdbcTemplate jdbcTemplate; @Transactional public void addBitch(String... names) throws Exception{ for(String name:names){ String sql="insert into t_product(pname) values('"+name+"')"; jdbcTemplate.execute(sql); if("".equals("")){ throw new FileNotFoundException(); } } } }
运行结果,没有回滚,如下:
4、测试添加rollbackFor的注解,抛出指定异常的情况:
package com.edu.spring.springboot; import java.io.FileNotFoundException; 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 ProductDao { @Autowired private JdbcTemplate jdbcTemplate; @Transactional(rollbackFor=Exception.class) public void addBitch(String... names) throws Exception{ for(String name:names){ String sql="insert into t_product(pname) values('"+name+"')"; jdbcTemplate.execute(sql); if("".equals("")){ throw new FileNotFoundException(); } } } }
运行结果,回滚,如下:
(2)Transactional注解加在直接调用的方法上面才可以,否则不会回滚
1、下面的代码会回滚
package com.edu.spring.springboot; import java.io.FileNotFoundException; 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 ProductDao { @Autowired private JdbcTemplate jdbcTemplate; @Transactional(rollbackFor=Exception.class) public void addBitch(String... names) throws Exception{ actiontest(names); } private void actiontest(String...names) throws Exception{ for(String name:names){ String sql="insert into t_product(pname) values('"+name+"')"; jdbcTemplate.execute(sql); if("".equals("")){ throw new FileNotFoundException(); } } } }
下面的代码不会回滚
package com.edu.spring.springboot; import java.io.FileNotFoundException; 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 ProductDao { @Autowired private JdbcTemplate jdbcTemplate; public void addBitch(String... names) throws Exception{ actiontest(names); } @Transactional(rollbackFor=Exception.class) private void actiontest(String...names) throws Exception{ for(String name:names){ String sql="insert into t_product(pname) values('"+name+"')"; jdbcTemplate.execute(sql); if("".equals("")){ throw new FileNotFoundException(); } } } }