簡介
對於數據訪問層,無論是 SQL 還是 NOSQL,Spring Boot 默認採用整合 Spring Data 的方式進行統一處理,添加了大量自動配置,屏蔽了很多設置。引入各種 xxxTemplate,xxxRepository 來簡化開發者對數據訪問層的操作。對開發者來說只需要進行簡單的設置即可。
參考:
https://spring.io/projects/spring-data
JDBC
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
application.yml
spring:
datasource:
username: root
password: root
url: jdbc:mysql://192.168.25.157:3306/jdbc
driver-class-name: com.mysql.cj.jdbc.Driver
demo.yangxu.springboot.Springboot05DataJdbcApplicationTests#contextLoads
package demo.yangxu.springboot;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SpringBootTest
class Springboot05DataJdbcApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
效果:
默認使用 com.zaxxer.hikari.HikariDataSource 作爲數據源。
數據源的相關配置都在 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties#DataSourceProperties 裏面。
自動配置原理
與數據源有關的配置都在 org.springframework.boot.autoconfigure.jdbc 包下。
1、參考 org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration,根據配置創建數據源,默認使用 com.zaxxer.hikari.HikariDataSource 連接池,可以使用 spring.datasource.type 指定自定義的數據源類型;
2、Spring Boot 默認可以支持;
- org.apache.tomcat.jdbc.pool.DataSource
- HikariDataSource
- BasicDataSource
3、自定義數據源類型
/**
* Generic DataSource configuration.
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type")
static class Generic {
@Bean
DataSource dataSource(DataSourceProperties properties) {
//使用DataSourceBuilder創建數據源,利用反射創建響應type的數據源,並且綁定相關屬性
return properties.initializeDataSourceBuilder().build();
}
}
4、DataSourceInitializer:ApplicationListener;
作用:
(1) runSchemaScripts(); 運行建表語句;
(2) runDataScripts(); 運行插入數據的 SQL 語句。
默認只需要將文件命名爲:
#schema-*.sql、data-*.sql
#默認規則:schema.sql,schema-all.sql 放在resources目錄下
#可以使用
schema:
- classpath:department.sql
#來指定位置
示例:
spring:
datasource:
url: jdbc:mysql://192.168.25.157:3306/jdbc
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
initialization-mode: always
data-username: root
data-password: root
schema-username: root
schema-password: root
schema:
- classpath:department.sql
5、操作數據庫:自動配置了 JdbcTemplate 操作數據庫
demo.yangxu.springboot.controller.HelloController
package demo.yangxu.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
@Controller
public class HelloController {
@Autowired
JdbcTemplate jdbcTemplate;
@ResponseBody
@GetMapping("/query")
public Map<String,Object> map(){
List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * FROM department");
return list.get(0);
}
}
訪問
http://localhost:8080/query
返回結果
{"id":1,"departmentName":"開發部"}