在默认状况下, 数据库链接可使用DataSource池进行自动配置。下面是选取一个特定实现的算法:java
若是你使用spring-boot-starter-jdbc或spring-boot-starter-data-jpa 'starter POMs', 你将会自动获取对tomcat-jdbc的依赖。mysql
注:其余的链接池能够手动配置。 若是你定义本身的DataSource bean,自动配置不会发生。git
DataSource配置经过外部配置文件的spring.datasource.*属性控制。示例中,你可能会在application.properties中声明下面的片断:github
spring.datasource.url=jdbc:mysql://localhost/test spring.datasource.username=dbuser spring.datasource.password=dbpass spring.datasource.driver-class-name=com.mysql.jdbc.Driver
其余可选的配置能够查看DataSourceProperties。同时注意你能够经过spring.datasource.*配置任何DataSource实现相关的特定属性:具体参考你使用的链接池实现的文档。web
注:既然Spring Boot可以从大多数数据库的url上推断出driver-class-name,那么你就不须要再指定它了。 对于一个将要建立的DataSource链接池,咱们须要可以验证Driver是否可用, 因此咱们会在作任何事情以前检查它。 好比, 若是你设置spring.datasource.driverClassName=com.mysql.jdbc.Driver,而后这个类就会被加载。算法
# 数据源配置 spring.datasource.url=jdbc:mysql://localhost:3306/ssb_test spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=root #链接池配置 spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource spring.datasource.dbcp2.max-wait-millis=10000 spring.datasource.dbcp2.min-idle=5 spring.datasource.dbcp2.initial-size=5 spring.datasource.dbcp2.validation-query=SELECT x spring.datasource.dbcp2.connection-properties=characterEncoding=utf8
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource:指定使用那个链接池,默认使用tomcate-jdbc链接池。spring
dbcp2配置详解http://blog.csdn.net/xiaolyuh123/article/details/73331093sql
package com.xiaolyuh; import net.minidev.json.JSONObject; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import javax.sql.DataSource; import java.util.HashMap; import java.util.Map; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringRunner.class) @SpringBootTest public class DataSourceTests { @Autowired ApplicationContext applicationContext; @Autowired DataSourceProperties dataSourceProperties; @Test public void testDataSource() throws Exception { // 获取配置的数据源 DataSource dataSource = applicationContext.getBean(DataSource.class); // 查看配置数据源信息 System.out.println(dataSource); System.out.println(dataSource.getClass().getName()); System.out.println(dataSourceProperties); } }
https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases数据库
spring-boot-student-data-jpa工程apache