Integration and use of h2database

1. Basic introduction

I remember that when I was studying the knowledge of the Spring Session JDBC part, I saw the com.h2database coordinates referenced from the example on the official website. The main purpose at that time was the Spring Session JDBC part. I just noticed that there was an online operation database component. At that time It's just an online login and visit. This time, I will briefly integrate the component and integrate it into the sample program of the Spring Boot project environment (when I checked the source code of the component, I found that it was written for JSP programs, and it simply did not use the JPS template engine. To render the page, otherwise you can only use external Tomcat to run, because Spring Boot does not support rendering JPS by default, the packaging method needs to be war, and you cannot directly use jar type applications to access JSP), in Java Servlet and Spring MVC type It is also supported in the project, see " Explore Spring Session: Introduction to JDBC Based on XML Configuration Version ".

Many Spring Boot integration h2database tutorials on the Internet have added a lot of parameter configurations in the application.properties file, including the configuration of the data source. Those tutorials should use h2database as a database. This example is just using this Tools to implement online operations of other databases. At the same time, based on the practice of the last example, I did not configure the parameter configuration of h2database separately, so this practice seems to have little to do with Spring Boot in the end. It just adds a Servlet configuration in the Spring Boot environment, which can be detailed see below.

Having said so much, let's briefly understand h2database. It is a high-performance open source JDBC API database engine that can be used and distributed for free. It supports independent deployment server mode and embedded in various applications. This article mainly integrates the control of h2database The platform application program realizes the online (addition, deletion, modification, and query) operation of the database in the web page. Because it uses JDBC, it supports a wide range of databases. Since the database is written in Java, it is tested with Java 8 and 11. So can run on many different platforms. All major operating systems are supported (Windows, Mac OS X, Linux, ...), some features are as follows:

(1) Very fast, open source JDBC API;

(2) Embedded and server modes; based on disk or in-memory database;

(3) Transaction support, multi-version concurrency;

(4) Browser-based console applications;

(5) Encrypted database;

(6) Full-text search;

(7) Pure Java with small footprint: about 2.5 MB jar file size;

(8) ODBC driver;

2. Code integration

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.2.7.RELEASE</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.16</version>
</dependency>

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>2.1.214</version>
</dependency>

Configure servlets

package cn.chendd.h2console.config;
import ...;
import org.h2.server.web.WebServlet;


/**
 * H2Console 配置类
 *
 * @author chendd
 * @date 2023/3/25 19:09
 */
@Configuration
public class H2ConsoleConfiguration {

    @Bean
    public ServletRegistrationBean<WebServlet> h2ConsoleServlet() {
        ServletRegistrationBean<WebServlet> servletRegistration = new ServletRegistrationBean<>(new WebServlet());
        servletRegistration.addUrlMappings("/h2-console/*");
        servletRegistration.addInitParameter("-webAdminPassword" , "www.chendd.cn");
        return servletRegistration;
    }

}

3. Run the example

(operation interface)

(operation interface)

4. Precautions

(1) User information is stored within the scope of the http session, including the Connection executed on the current page, so it is best to click the exit button to release resources after execution is complete;

(2) Set the manual commit of the transaction, especially when executing some modification and deletion statements, to prevent the operation from being reversible;

(3) Set a reasonable maximum number of query rows, limit the number of query rows, and have large fields such as Clob and Blob when placing query table data;

(4) Start port 8082 inside the program, and prevent the port from being occupied by other applications;

(5) Use with caution, use with caution, use with caution; transaction rollback, transaction rollback, transaction rollback; user exit, user exit, user exit;

5. Supplementary instructions

(1) At present, it has only been practiced under the two database versions of Oracle and MySQL. The current text screenshot is the MySQL database. You can see that it displays all the table names of all users after logging in. Clicking on the table name can give the query statement of the table and Expand to show all fields of the table;

(2) In the Oracle environment, all users are also displayed. The current user schema is displayed by default, and the tables or views of other users are folded and displayed. In this example, MySQL displays the tables and views under all databases of this user;

(3) There is a problem with the Oracle query in the source code of the latest version 2.1.214. After integration, it is found that the prompt table or view does not exist. After analysis, it is found that a piece of SQL query logic is oriented to non-Oracle writing, and later changed to 1.4 There is no such problem in version .200;

(4) Display the index structure of the table when there are less than 100 tables, see org.h2.engine.SysProperties class, which defines many configuration parameters;

(5) Many parameters are initialized in the WebServlet class, and the parameters are added by means of filters, such as adding password parameters for management configuration, see above for details;

(6) Please refer to the project source code and original text: https://www.chendd.cn/blog/article/1639568610137288706.html ;

Supongo que te gusta

Origin blog.csdn.net/haiyangyiba/article/details/129890981
Recomendado
Clasificación