016-Spring Boot JDBC
一、数据源装配
通过查看代码可知,默认已装配了数据源和JdbcTemplate
System.out.println(context.getBean(DataSource.class)); System.out.println(context.getBean(JdbcTemplate.class));
1.1、环境搭建
主要是pom引用:spring-boot-starter-jdbc、增加数据库驱动
<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.lhx.spring</groupId> <artifactId>springboot-web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-web</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies> </project>
查看spring-boot-autoconfigure-1.5.9.RELEASE.jar包下的org.springframework.boot.autoconfigure.jdbc下的DataSourceProperties其实所有的都是类似的配置
@ConfigurationProperties(prefix = "spring.datasource") public class DataSourceProperties implements BeanClassLoaderAware, EnvironmentAware, InitializingBean {
可以看到默认读取spring.datasource开头的配置。
1.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=root
1.3、使用
public static void main(String[] args) throws SQLException { ConfigurableApplicationContext context = SpringApplication.run(App.class, args); dataSource = context.getBean(DataSource.class); Connection connection = dataSource.getConnection(); System.out.println(connection.getCatalog()); }
二、JdbcTemplate
在配置好基本配置后,JdbcTemplate就可以使用了
@Repository public class ProductDao { @Autowired private JdbcTemplate jdbcTemplate; public void addProdcut(String value) { String sql = "insert into product(pname) values('" + value + "')"; jdbcTemplate.execute(sql); } }
调用
public class App { public static void main(String[] args) throws SQLException { ConfigurableApplicationContext context = SpringApplication.run(App.class, args); context.getBean(ProductDao.class).addProdcut("mp3"); connection.close(); } }
三、切换数据源
System.out.println(context.getBean(DataSource.class));
查看到使用的是默认提供的:org.apache.tomcat.jdbc.pool.DataSource数据源
方式一、排除添加切换
1》排除掉tomcat-jdbc
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <exclusions> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </exclusion> </exclusions> </dependency>
2》添加新的数据源
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency>
方式二、type配置数据源切换
3.2.1、原理查看
查看spring-boot-autoconfigure-1.5.9.RELEASE.jar包下的org.springframework.boot.autoconfigure.jdbc下的DataSourceProperties的配置
使用type指定【private Class<? extends DataSource> type;】在application.properties中
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
默认支持以下几种数据源类型,查看spring-boot-autoconfigure-1.5.9.RELEASE.jar包下的org.springframework.boot.autoconfigure.jdbc下的DataSourceAutoConfiguration
DataSourceConfiguration.Tomcat.class DataSourceConfiguration.Hikari.class DataSourceConfiguration.Dbcp.class DataSourceConfiguration.Dbcp2.class DataSourceConfiguration.Generic.class
3.2.2、具体步骤
1》添加对应数据源pom
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency>
2》使用配置
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
3.3、配置自定义数据源
1》添加POM依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency>
2》、增加DBConfiguration配置类
package com.lhx.spring.springboot_web; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.core.env.Environment; import com.alibaba.druid.pool.DruidDataSource; @SpringBootConfiguration public class DBConfiguration { @Autowired private Environment env; @Bean public DataSource createDataSource() { DruidDataSource source = new DruidDataSource(); source.setUrl(env.getProperty("spring.datasource.url")); source.setUsername(env.getProperty("spring.datasource.username")); source.setPassword(env.getProperty("spring.datasource.password")); source.setDriverClassName(env.getProperty("spring.datasource.driverClassName")); return source; } }
四、事务
4.1、配置开启
1、在入口app增加注解
@EnableTransactionManagement
2、在需要事务的公有方法上添加注解
@Transactional
示例
@Transactional public void addProdcutBatch(String... names) throws Exception { for (String name : names) { String sql = "insert into product(pname) values('" + name + "')"; jdbcTemplate.execute(sql); if ("".equals("")) { throw new NullPointerException(); } } }
注意:spring事务,默认支持运行时异常,即RunTimeException,非运行时异常不会回滚
4.2、设置针对哪些异常使用事务
默认是运行时
设置所有异常
@Transactional(rollbackFor=Exception.class)
设置哪些异常不回滚
@Transactional(noRollbackFor=NullPointerException.class)
注意点:
1、要想使用事务生效,被调用的方法必须有Transactional 注解,对于内部调用方法有无注解无影响
2、需要数据库支持事务。