52.SpringBoot学习笔记--SpringData-简介、JDBC与自动配置原理

简介

对于数据访问层,无论是 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":"开发部"}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章