数据库使用MySQL 5.7.18版本。
装配DataSource的步骤:
1.加入数据库驱动
pom.xml:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.study.springboot</groupId> 6 <artifactId>spring-boot-jdbc</artifactId> 7 <version>1.0.0</version> 8 <packaging>jar</packaging> 9 10 <name>springboot</name> 11 <url>http://maven.apache.org</url> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <dependencyManagement> 18 <dependencies> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-dependencies</artifactId> 22 <version>1.5.3.RELEASE</version> 23 <scope>import</scope> 24 <type>pom</type> 25 </dependency> 26 </dependencies> 27 </dependencyManagement> 28 29 <dependencies> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-jdbc</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>mysql</groupId> 36 <artifactId>mysql-connector-java</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>junit</groupId> 40 <artifactId>junit</artifactId> 41 <scope>test</scope> 42 </dependency> 43 </dependencies> 44 </project>
2.配置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot
spring.datasource.username=root
spring.datasource.password=adminroot
application.properties:
1 spring.datasource.driverClassName=com.mysql.jdbc.Driver 2 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot 3 spring.datasource.username=root 4 spring.datasource.password=adminroot
以上操作,springboot会自动装配好DataSource和JdbcTemplate,可以直接使用
1 package com.study.spring_boot_jdbc.dao; 2 3 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.jdbc.core.JdbcTemplate; 6 import org.springframework.stereotype.Repository; 7 8 @Repository 9 public class ProductDao { 10 11 @Autowired 12 private JdbcTemplate jdbcTemplate; 13 14 public void addProduct(String name){ 15 String sql = "insert into product (pname) values ('" + name + "')"; 16 jdbcTemplate.execute(sql); 17 } 18 19 }
1 @SpringBootApplication 3 public class App { 4 5 public static void main(String[] args) throws Exception { 6 ConfigurableApplicationContext context = SpringApplication.run(App.class,args); 7 DataSource ds = context.getBean(DataSource.class); 8 Connection conn = ds.getConnection(); 9 System.out.println(conn.getCatalog()); 10 System.out.println(context.getBean(JdbcTemplate.class)); 11 conn.close(); 12 System.out.println(ds.getClass()); 13 context.close(); 14 } 15 16 }
如果不想使用Tomcat的DataSource,可以使用两种方式配置需要的数据源,步骤:
1.pom.xml 添加相关依赖
1 <dependency> 2 <groupId>com.zaxxer</groupId> 3 <artifactId>HikariCP</artifactId> 4 </dependency>
2.
a)spring.datasource.type配置项,可指定具体使用哪种数据源
默认支持Tomcat,Hikari,Dbcp,Dbcp2,Generic(1.5.3.RELEASE)
application.properties:
1 spring.datasource.type=com.zaxxer.hikari.HikariDataSource
b)排除Tomcat的数据源
pom.xml:
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-jdbc</artifactId> 4 <exclusions> 5 <exclusion> 6 <groupId>org.apache.tomcat</groupId> 7 <artifactId>tomcat-jdbc</artifactId> 8 </exclusion> 9 </exclusions> 10 </dependency>
配置自定义DataSource,只需要装配一个DataSource到spring容器中即可
1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>druid</artifactId> 4 <version>1.0.31</version> 5 </dependency>
1 @SpringBootConfiguration 2 public class DBConfiguration { 3 @Autowired 4 private Environment env; 5 @Bean 6 public DataSource createDataSource(){ 7 DruidDataSource ds = new DruidDataSource(); 8 ds.setUrl(env.getProperty("spring.datasource.url")); 9 ds.setUsername(env.getProperty("spring.datasource.username")); 10 ds.setPassword(env.getProperty("spring.datasource.password")); 11 ds.setDriverClassName(env.getProperty("spring.datasource.driverClassName")); 12 return ds; 13 } 14 15 }
事务:
首先要使用@EnableTransactionManagement启用对事务的支持
然后,在需要使用事务的方法上加上@Transactional
注意,默认只会对运行时异常进行事务回滚,非运行时异常不会回滚事务
APP.class:
1 @SpringBootApplication 2 @EnableTransactionManagement 3 public class App { 4 5 public static void main(String[] args) throws Exception { 6 ConfigurableApplicationContext context = SpringApplication.run(App.class,args); 7 8 context.getBean(ProductDao.class).addProductBatch("TV","MP3","MP4"); 9 10 System.out.println(ds.getClass()); 11 context.close(); 12 } 13 14 }
1 package com.study.spring_boot_jdbc.dao; 2 3 import java.io.FileNotFoundException; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.jdbc.core.JdbcTemplate; 7 import org.springframework.stereotype.Repository; 8 import org.springframework.transaction.annotation.Transactional; 9 10 @Repository 11 public class ProductDao { 12 13 @Autowired 14 private JdbcTemplate jdbcTemplate; 15 16 public void addProduct(String name){ 17 String sql = "insert into product (pname) values ('" + name + "')"; 18 jdbcTemplate.execute(sql); 19 } 20 21 /** 22 * rollbackFor 设置对哪些异常进行回滚,默认是运行时异常 23 * noRollbackFor 设置哪些异常不回滚 24 * @param names 25 * @throws Exception 26 */ 27 @Transactional(noRollbackFor=NullPointerException.class) 28 public void addProductBatch(String ... names) throws Exception{ 29 for (String name : names) { 30 String sql = "insert into product (pname) values ('" + name + "')"; 31 jdbcTemplate.execute(sql); 32 if ("".equals("")) { 33 throw new NullPointerException(); 34 } 35 } 36 37 } 38 39 40 }
注意,在需要使用事务的方法上加上@Transactional!!!
1 package com.study.spring_boot_jdbc.dao; 2 3 import java.io.FileNotFoundException; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.jdbc.core.JdbcTemplate; 7 import org.springframework.stereotype.Repository; 8 import org.springframework.transaction.annotation.Transactional; 9 10 @Repository 11 public class ProductDao { 12 13 @Autowired 14 private JdbcTemplate jdbcTemplate; 15 16 public void addProduct(String name){ 17 String sql = "insert into product (pname) values ('" + name + "')"; 18 jdbcTemplate.execute(sql); 19 } 20 21 @Transactional 22 public void addProductBatch(String ... names) throws Exception{ 23 add(names); 24 25 } 26 27 @Transactional 28 public void add(String ... names) throws Exception{ 29 for (String name : names) { 30 String sql = "insert into product (pname) values ('" + name + "')"; 31 jdbcTemplate.execute(sql); 32 if ("".equals("")) { 33 throw new NullPointerException(); 34 } 35 } 36 } 37 38 }
若addProductBatch方法没有@Transactional,执行addProductBatch,事务不起作用。若是执行add,事务起作用。