Introduction to H2

H2 is an embedded database developed in Java. It is simply a class library that can be embedded directly into an application project.

The H2 database has the following advantages:

  • Can be packaged and distributed with applications to easily store small amounts of structured data;
  • For unit testing, data can be stored in memory and restored to the initial state after each use case is executed;
  • Compatible with common mainstream relational databases, such as DB2, Oracle, MS SQL Server, Mysql, etc.
  • Browser-based console applications;

H2 integration in Springboot

Springboot’s use of the H2 database is very simple. H2 dependencies in Maven will do the job

< the dependency > < groupId > com. H2database < / groupId > < artifactId > h2 < / artifactId > < version > 1.3.172 < / version > <scope>runtime</scope> </dependency>Copy the code

3. Configuration of H2

1. Configuration of data source type and data-driven class:

Take Hikari for example:

spring:
    datasource:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: org.h2.Driver
Copy the code
2. Configure the data store location

Url: JDBC :h2:mem:myDb; MODE=MySQL; DATABASE_TO_LOWER=TRUE

#h2 :~/test:myDb; #h2 :~/test:myDb; #h2 :~/test:myDb; MODE=MySQL; DATABASE_TO_LOWER=TRUE; USER=sa

3. Table structure creation and data initialization

H2 can be configured with two SQL files for the creation and initialization of data tables at program startup;

Spring: datasource: schema: classpath: SQL /init. SQL # data: classpath: SQL /data. SQL # Data: classpath: SQL /data. SQLCopy the code

Note: As described above, you usually create an SQL directory under the Resource directory and add init. SQL and data. SQL files.

4. Configure the console

Can access by http://localhost:8080/h2Console H2 console;

Spring: h2: console: enabled: true Settings: trace: true The default is false path: /h2Console #h2 to access the path contextCopy the code

Note: The JDBC URL in the console must be the same as the URL found in your configuration file; Url: JDBC :h2:~/test:myDb;

After login, you can see the admin interface of the H2 database:

Attachment: Complete configuration

server: port: 8082 spring: application: name: zhangliao_mybatis_plus_crud datasource: type: Com. Zaxxer. Hikari. HikariDataSource driver - class - name: org. H2. The driver # h2 in-memory database persistence mode connection configuration library name: myDb # url: jdbc:h2:~/test:myDb; MODE=MySQL; DATABASE_TO_LOWER=TRUE; Url: JDBC :h2:mem:myDb; MODE=MySQL; DATABASE_TO_LOWER=TRUE; USER=sa schema: classpath:sql/init.sql data: classpath:sql/data.sql hikari: connection-timeout: Default :30 seconds minimum-idle: 10 maximum-pool-size: 100 auto-commit: 30 seconds minimum-idle: 10 maximum-pool-size: 100 auto-commit: 30 seconds True idle-timeout: 600000 # Specifies the maximum length of time a connection will be idle before it is retired. Default :10 minutes Default :30 minutes 1800000ms. Recommended set this to 60 seconds less than the database timeout. MySQL > show variables like '%timeout%'; --> connection-test-query: SELECT 1 validation-timeout: 3000 h2: console: enabled: true settings: False web-allow-others: true path: false web-allow-others: true path: /h2Console mybatis-plus: configuration: cache-enabled: false local-cache-scope: statement global-config: banner: falseCopy the code