Have you ever come across a scenario in your development where you need data storage but don’t need to deploy a dedicated data storage service? Instead, you can use a built-in H2 database. Let’s take a look at this H2 database.
The H2 website
www.h2database.com/
This is the official website of H2 data
H2 database features:
- Very fast, open source, JDBC API
- Embedded and server mode; In-memory database
- Browser-based console application
- Small footprint: about 1.5 MB JAR file size
Embedded mode
The embedded mode is to introduce H2 into the application. When the application is started, the H2 data service is also started. The application not only contains the H2 database server, but also connects to the H2 database as a client
For example
Use springBoot to make a chestnut for embedded H2
Start by creating a springBoot project
H2 dependency:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Copy the code
Enable the console display of H2 data in the configuration file
Spring. The datasource. The console platform = h2 # open h2 database access spring. The h2. The console. The enabled = true logging. Level. The root = INFOCopy the code
After the project is started, enterhttp://localhost:8080/h2-console, go to the H2 data console
The default password is SA. You can perform database operations on the console
Create an information info table using the table builder
CREATE TABLE info
(id bigint,
name varchar);
Copy the code
H2 Memory mode connection configuration
In memory mode, the database file exists in memory without persistence. When the application process is shut down, the database and data table will disappear. The configuration file is as follows:
Spring. The datasource. The console platform = h2 # open h2 database access spring. The h2. The console. The enabled = true logging. Level. The root = INFO # driver Spring. Datasource. URL = JDBC :h2:mem:hello # spring. The datasource. Url = JDBC: h2: file: F: / h2 / hello # database password is spring. The datasource. The username = sa spring. The datasource. Password = sa Spring.jpa.hibernate. DDL -auto=none # Whether to print SQL statements spring.jpa.show-sql=trueCopy the code
Do the above table building operations
When we restarted our application, the data was not persisted and disappeared
H2 embedded mode connection configuration
The embedded mode is that the database file exists in the current hard disk of the application and is persisted. When the application process is shut down, the database and data tables will not disappear. The configuration file is as follows:
Spring. The datasource. The console platform = h2 # open h2 database access spring. The h2. The console. The enabled = true logging. Level. The root = INFO # driver Spring.datasource. Driver-class-name =org.h2. driver # database URL #spring.datasource Spring. The datasource. Url = JDBC: h2: file: F: / h2 / hello # database password is spring. The datasource. The username = sa spring. The datasource. Password = sa Spring.jpa.hibernate. DDL -auto=none # Whether to print SQL statements spring.jpa.show-sql=trueCopy the code
After you do the same table creation and add data, the application is restarted and the data is still there
H2 :file:F:/H2/hello :/H2 :file:F:/H2/hello :/H2 :file:F:/H2/hello
Connect to the H2 database using Spring Data
Writing entity classes
package com.ftd.h2.entity; import javax.persistence.*; import java.io.Serializable; /** * @author ZhaoWeinan */ @Entity public class Info implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Info{" + "id=" + id + ", name='" + name + '\'' + '}'; }}Copy the code
Write the Repository class
package com.ftd.h2.mapper;
import com.ftd.h2.entity.Info;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
*
* @author ZhaoWeinan
* @date 2018/3/14
*/
@Repository
public interface InfoRepository extends JpaRepository<Info,Long> {
}
Copy the code
Write a controller class
package com.ftd.h2.controlloer; import com.ftd.h2.entity.Info; import com.ftd.h2.mapper.InfoRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * * @author ZhaoWeinan * @date 2018/3/14 */ @RestController public class InfoController { @Autowired private InfoRepository repository; @RequestMapping(value = "info/{id}",method = RequestMethod.GET) public Info getInfoById(@PathVariable Long id){ return repository.getOne(id); }}Copy the code
Take a look at the results:
Server mode
The server mode is to deploy the H2 database and the application separately, and the application connects to the H2 database as a client
For example
Download and install the H2 database
Run the batch file in the installation bin directory
The H2 database service is up with the default console port 8082
Modify the database connection configuration in the program
Modified as follows:
Spring. The datasource. The console platform = h2 # open h2 database access spring. The h2. The console. The enabled = true logging. Level. The root = INFO # driver Spring.datasource. Driver-class-name =org.h2. driver # JDBC :h2:mem:hello # # spring. The datasource. Url = JDBC: h2: file: F: spring/h2 / hello # database url service mode. The datasource. Url = JDBC: h2: TCP: / / localhost / / F: / h2 / hello # database password is spring. The datasource. The username = sa spring. The datasource. The password = sa spring. Jpa, hibernate. DDL - auto = none # whether print SQL statements spring.jpa.show-sql=true spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=falseCopy the code
Use the spring Boot program test
The H2 summary
The differences between the three modes of memory, embedded and server are mainly reflected in the configuration of database connection URL
Spring. Datasource. URL = JDBC :h2:file:F:/ h2 /hello # database URL service pattern spring. The datasource. URL = JDBC: h2: TCP: / / localhost / / F: / h2 / helloCopy the code
As to which model to choose depends on the specific situation in your development and try, there is no best technology selection, only a more appropriate technology selection
H2 database will give you a brief introduction here, welcome everyone to exchange, point out some mistakes in the article, let me deepen my understanding, thank you!