Spring官网文档中介绍的数据库部分


Start

官网参考资料

Data

“How-to” Guides Embedded Servers, Data Access,

TODO

  • 数据事务回顾整理
  • JPA 的细节
  • Mybatis的配合
  • Docker 中数据的初始化配置

按需

Ongoing

knowledge

Overall

官方文档对那个h2数据库的介绍

  • 只需要添加H2的依赖和spring-jdbc,不需要配置url,并可以通过浏览器地址访问。

生产数据库的使用

  • 使用相关的数据库,spring根据 url 自动推断使用的驱动(支持大部分,前提是提供了驱动),依据[推断算法](#Supported Connection Pools) 设置连接池
  • 定制化驱动类,使用spring.datasource.driver-class-name
  • Sring-boot-starter-jdbc or spring-boot-starter-data-jpa 自动获得 HikariCP

相关包和概念

JPA and Spring Data JPA :JPA 是标准,Spring Data JPA是spring的包,解释看英文更容易理解

  • Java Persistence API is a standard technology that lets you “map” objects to relational databases。

  • spring-boot-starter-data-jpa provide:

    • Hibernate: One of the most popular JPA implementations.
    • Spring Data JPA: Helps you to implement JPA-based repositories.
    • Spring ORM: Core ORM support from the Spring Framework.

Process

嵌入式数据库

仅作为开发使用

Spring Boot can auto-configure embedded H2, HSQL, and Derby databases. You need not provide any connection URLs.

need only include a build dependency to the embedded database that you want to use.

You need a dependency on spring-jdbc for an embedded database to be auto-configured. In this example, it is pulled in transitively through spring-boot-starter-data-jpa.

只需要配置添加依赖就可以使用,springboot已经自动配置了。但依赖一个jdbc初始化,官网给出的依赖例子如下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <scope>runtime</scope>
</dependency>

按照官网的例子,配置好,启动SpringBoot,输出中包括以下内容,可以看到自动配置的h2 数据库路径为 /h2-console, 给出的url

NFO 12228 --- [  restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:60ab62bd-fb36-4ea2-9935-fd29b7b86dcd'

浏览器其访问http://localhost:8080/h2-console/,直接登录会得到错误的提示,得输入上面输出的url地址

生产数据库

配置以spring.datasource.* 开头,不同的连接池使用的后缀不一样。如果使用Hikari, 配置的前缀就是 spring.datasource.hikari.*,具体的详细配置spring文档上并不会有,得自己查看官方的文档(毕竟这只是springboot的文档)。

spring 自动从url推断 JDBC driver class (支持大部分), 具体可以 设置spring.datasource.driver-class-name property.

spring:
  datasource:
    url: "jdbc:mysql://localhost/test"
    username: "dbuser"
    password: "dbpass"

Supported Connection Pools

Sring-boot-starter-jdbc or spring-boot-starter-data-jpa 自动获得 HikariCP

Spring Boot uses the following algorithm for choosing a specific implementation:

  1. We prefer HikariCP for its performance and concurrency. If HikariCP is available, we always choose it.
  2. Otherwise, if the Tomcat pooling DataSource is available, we use it.
  3. Otherwise, if Commons DBCP2 is available, we use it.
  4. If none of HikariCP, Tomcat, and DBCP2 are available and if Oracle UCP is available, we use it.

你知道配置数据库连接池的几种方法吗?

不做介绍,可以查看 官网这一部分介绍,只是用不同的方式实现Bean。比如如何在两个数据库中选择

数据库数据初始化

读取classpath下的名为data.sql 和schema.sql 文件具体化可以在配置文件中设置,见代码,通过sql脚本的方式实现。

spring 有多种方式初始化数据库,details见官网how-to

参考配置

# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/wj?characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=Admin123!
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# 每次运行时初始化数据库,如不需要可以注释掉
spring.datasource.initialization-mode=always
# 将 hibernate 的自动注入属性设置为 none(防止冲突,上面已经使用了 spring 的注入方法)
spring.jpa.hibernate.ddl-auto=none

# 打印 sql 语句
spring.jpa.properties.hibernate.show_sql=true
#spring.jpa.properties.hibernate.format_sql=true

##  Hikari 连接池配置
## 最小空闲连接数量
spring.datasource.hikari.minimum-idle=5
## 空闲连接存活最大时间,默认600000(10分钟)
spring.datasource.hikari.idle-timeout=180000
## 连接池最大连接数,默认是10
spring.datasource.hikari.maximum-pool-size=10
## 此属性控制从池返回的连接的默认自动提交行为,默认值:true
spring.datasource.hikari.auto-commit=true
## 连接池名
spring.datasource.hikari.pool-name=WJHikariCP
## 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
spring.datasource.hikari.max-lifetime=1800000
## 数据库连接超时时间,默认30秒,即30000
spring.datasource.hikari.connection-timeout=30000

Author: datefate
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source datefate !
  TOC
-->