SpringBoot - built-in database

a brief introdction

Regarding the three major components of the data layer, data sources, persistence technologies, and databases. The first two have introduced SpringBoot's built-in solutions, and the last database. In SpringBoot, there are three built-in databases. They are:

  • H2
  • HSQL
  • Derby

These three databases have several things in common:

  • They are all written in Java language and can be injected into the Spring container as Java classes.
  • Lightweight, light enough to run in memory

The first feature allows them to be built into Spring, and the second feature allows them to be used directly without installation when the program is running. This is the main reason why SpringBoot can build these three databases, and it is lightweight enough to facilitate our testing during the testing phase.

Environment introduction

We can continue to use the previous environment, but we need to make some modifications to the coordinates in the pom file. Annotate the previous MySQL coordinates, and then add the related dependencies of the H2 database and the related dependencies of the Web:

        <!--H2数据库必须的两个坐标-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

Then, we comment out everything in the previous configuration file, and then make some configurations for the H2 database and Web environment:

server.port=80

spring.h2.console.enabled=true
spring.h2.console.path=/h2

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=123456
spring.datasource.url=jdbc:h2:~/test

The top one is related to the configuration of the Web, and the middle one is related to the configuration of H2. The first line indicates the H2 console, the second line indicates the console path of the H2Web side, and the third part is related to the data source configuration of the H2 database. , this part of the configuration is only useful when connecting to H2 for the first time, and you can delete this part of the configuration after the first connection is successful.

Then start the SpringBoot boot class: 

It can be seen that the console outputs a lot of things that we have not seen before. The part in the red box is the H2 related log. Then we open the browser and enter the URL of the H2Web console:

 If you see this on your browser, it means that your H2 database has been started, then enter the default password 123456, and click [Connect]:

This interface is the web-side console for operating the H2 database. We can enter SQL statements in the input box on the right, and then see the results in the status bar below: 

Since H2 runs in memory, its operation is very fast, but the amount of stored data is not very large, and it is generally used in our tests. And as a SQL database, H2 basically supports all the syntax in MySQL, and some basic operations of adding, deleting, modifying and querying are the same. Next, we will use H2 with Druid and MyBatis to make a completed Dao layer.

First are all the dependencies we will use:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>2.3.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

 Configuration file:

spring:
  h2:
    console:
      enabled: true
      path: /h2
  datasource:
    url: "jdbc:h2:~/test"
    username: "sa"
    password: "123456"
    driver-class-name: "org.h2.Driver"

H2 database SQL statement:

create table tb_user (id int,name varchar)
insert into tn_user values(1,'张三'),(2,'李四'),(3,'王五'),(4,'赵六')
select * from tb_user

Then we have to write POJO and Mapper. We have seen these before, so I will directly show the test method in the test class here:

@Test
    void contextLoads(@Autowired userMapper userMapper) {
        book user = userMapper.selectUserById(1);
        System.out.println(user);
    }

 The method is still the same as the previous method, and the parameters are the same. SpringBoot focuses on something that you can integrate, so the basic code does not need to be changed. It is nothing more than importing the coordinates and changing the configuration file.

Our H2-related logs are also printed on the console, which basically completes the integration of H2. 

Summarize

So far, we have introduced the three major components related to the data layer, data source, persistence technology, database, and the default solution in SpringBoot. Here I just said that there is such a technology in SpringBoot, and it is automatically maintained. The specific data selection still needs to be determined according to the situation at the time, and the advantages and disadvantages of this concentrated technology are all in-depth. After learning, judge based on the situation.

In addition to introducing these technologies, the most important thing is that these technologies can be used in combination with each other. For example, we have been using the combination of MyBatis, Druid, and MySQL. During testing, we can replace MySQL with faster H2, or replace Druid with hikari with fewer configurations. Pay attention to importing the corresponding Dependence is OK.

Guess you like

Origin blog.csdn.net/hssjsh/article/details/131966158