In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
The use of embedded database H2 and integration of Spring Boot operation examples, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
1. Summary
When I talk about some Mybatis features, I always write some examples to demonstrate to my fans. Using Mysql or other large databases is too heavy because it's just a demo. Of course, you can also use docker to install. But you still need to rely on something. Is there a database that is very small and easy to carry, and can meet very small scenarios? Of course there is. Today, we introduce an embedded relational database H2, which is written in pure java and supports jdbc.
two。 Database H2
Characteristics of H2 database:
Very fast, open source, support for JDBC API
Embedded and server mode; in-memory database
Browser-based console application
Small footprint, jar is only the size of 2MB
These are just the features listed on the official website. In fact, there are cross-platform advantages, supporting most of the common platforms. It is also compatible with common mainstream relational databases, such as DB2, Oracle, MS SQL Server, Mysql, PostgreSQL, HSQLDB, Ignite, Derby and so on.
3. Working with scen
Based on the above characteristics. H2 database is especially suitable for small applications that are built quickly. Especially in application development and unit testing, it is very convenient to use, and saves system resources. And the dependency pool of springboot also includes H2 database. Next, we will demonstrate some of the features of H2 database through springboot combined with Mybatis.
Using H2 in 4.springboot
It is very simple for springboot to use the H2 database. Integrating H2 dependencies under BOM is fine. Here we also introduce Mybatis for demonstration purposes.
Org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.0 com.h3database h3 runtime
Of course, it is not enough to introduce dependencies. We also need to configure other parameters. Of course, these configurations can be done through springboot's application.yml (application.properties). We know that H2 supports embedding in springboot applications like tomcat, as well as a separate server process mode. Let's demonstrate it through different configurations.
5. Embedded mode
Embedded mode is to introduce H2 into the application and start the H2 data service at the same time. The application not only includes the server of the H2 database, but also connects the H2 database as the client.
5.1 memory mode connection
The in-memory mode is that the database file exists in memory and is not persisted, and the database and data tables disappear when the application process is closed. For standalone environments, we use the profile feature of springboot to isolate different mode environment configurations. We customize a name called application-inner.yml to test the embedded pattern. The configuration is as follows:
Spring: datasource: # driver driver-class-name: org.h3.Driver # H3 memory database memory mode connection configuration library name: mybatis url: jdbc:h3:mem:mybatis # initialization data table DDL schema: classpath:sql/init.sql # initialization data DML data: classpath:sql/data.sql H3: # enable console access default false console: Enabled: true settings: # enable h3 console tracking to facilitate debugging default false trace: true # allow console remote access default false web-allow-others: true # H3 access path context path: / h3-console# log logging: level: cn: felord: debug# mybatis configuration mybatis: mapper-locations: classpath: Mapper/*.xml configuration: map-underscore-to-camel-case: true type-aliases-package: cn.felord.mybatis.entity type-handlers-package: cn.felord.mybatis.type
After the springboot project is started by specifying-- spring.profiles.active=inner, enter http://localhost:8080/h3-console to enter the console of H2 data:
Be sure to set the red box according to the spring.datasource.url in your configuration file, not the default value. If you do not set the password direct point connect, set the password for input. Enter the following interface:
Because we specified DDL and DML SQL scripts in initialization, we created the student table and inserted three pieces of data. So it will show up. It proves that the integration is successful. At the same time, our test package for executing the maven project will also successfully complete the Mybatis unit test. But when we close the application, the data will be lost because the data is in memory. Memory will be reclaimed. Please comment out spring.datasource.schema and spring.datasource.data restart. So how do you persist it? This uses embedded mode.
5.2 embedded mode connection
The embedded mode is that the database file exists in the application's current hard disk and is persisted, so that the database and data tables will not disappear when the application process is closed. We just need to change the spring.datasource.url in the yml configuration of 5.1 to jdbc:h3:file:E:/H2/mybatis. Then start logging back in to console. Note that url should be changed to jdbc:h3:file:E:/H2/mybatis. We found that the data was all there, and then we turned it off and started it and found that the error was reported:
Indicates that the data in the database conflicts. We commented out DDL, DML initialization and found that it can start again. To prove that the data is persistent. File in url: suffix the available path of your system, and H2 can persist the data to that path.
5.3 compatibility
At first we mentioned that H2 is compatible with many databases. How is it compatible? Use the parameter url suffix MODE to summarize your own use:
Oracle jdbc:h3:~/test;MODE=Oracle or SQL statement SET MODE Oracle
Mysql jdbc:h3:~/test;MODE=MySQL;DATABASE\ _ TO\ _ LOWER=TRUE
PostgreSQL jdbc:h3:~/test;MODE=PostgreSQL;DATABASE\ _ TO\ _ LOWER=TRUE
MSSQLServer jdbc:h3:~/test;MODE=MSSQLServer or SQL statement SET MODE MSSQLServer
Others are not listed one by one, but note that compatibility is not fully compatible, there will be some considerations and minor differences. There are usually no problems. For more information, you can consult official documents and other materials.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.