Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of h2databse Integration in SpringBoot DB

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the example analysis of h2databse integration in SpringBoot DB. I hope you will get something after reading this article. Let's discuss it together.

i. Project creation

The corresponding example demo in this article is developed with SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA.

1. Pom configuration

This article will not cover how to create a springboot project. In the project we have created, the pom.xml file is as follows

Org.springframework.boot spring-boot-starter-parent 2.2.1.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa com.h3database h3 org.springframework.boot spring-boot-maven -plugin spring-snapshots Spring Snapshots https://repo.spring.io/libs-snapshot-local true spring-milestones Spring Milestones https://repo.spring.io/libs-milestone-local false spring-releases Spring Releases https://repo.spring.io/libs-release-local false

Focus on com.h3database in dependency, the other two are not necessary, but will be used in later test cases, it is recommended to add

From the introduction above, we can also know that we will use JPA to operate the database.

two。 Attribute configuration

Since you are connecting to the database, of course, you must configure the database. Under the resource path of the project, create a new configuration file application.properties.

# related configuration of database spring.datasource.url=jdbc:h3:~/h3-dbspring.datasource.username=testspring.datasource.password=spring.datasource.driverClassName=org.h3.Driver

There is nothing special about the above configuration and our mysql database configuration. Please pay attention to the url here.

Jdbc:h3:~/h3-db: embedded posture will generate a file called h3-db.mv.db in the user's root directory (schema and d column of the database are stored in it)

Jdbc:h3:mem:DBName;DB_CLOSE_DELAY=-1: in memory mode, the database is emptied after the application is restarted, so in the test case, you can consider using this

In addition to the embedded posture above, h3-dabase also supports specifying a remote directory through tcp.

Jdbc:h3:tcp://localhost/~/test

Above is the basic configuration of h3dabase. For a more friendly demonstration, we have opened the web console console of h3dabase.

# # after h3 web console sets spring.datasource.platform=h3# for this configuration, H3 web consloe can be accessed remotely. Otherwise, it can only be accessed locally. Spring.h3.console.settings.web-allow-others=true# for this configuration, you can access the h3 web consloespring.h3.console.path=/h3# through YOUR_URL/h3 for this configuration, and the h3 web consloespring.h3.console.enabled=true will be launched when the program is opened.

It is best to open the sql statement of jpa

Spring.jpa.show-sql=truespring.jpa.generate-ddl=trueII. Case test

After the above configuration is done, it can be said that the integration of h3dabase has been completed.

0. Entry @ SpringBootApplicationpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class);}}

After the startup entry of the SpringBoot application is executed above, we can access the console of h3dabase through http://localhost:8080/h3. Note the contents in the box below, which are consistent with the previous configuration file.

After logging in, it is a recommended database operations console

1. Entity definition

The following belongs to the knowledge point of JPA. For those who are interested in jpa, you can take a look at the previous "JPA series tutorials".

@ Entity@Table (name = "test") public class TestEntity {@ Id private Integer id; @ Column private String name; public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} 2. Repository interface

Database operation interface. You can use the default curd directly, and there is no additional method to add it.

@ Repositorypublic interface TestRepository extends CrudRepository {} 3. Test case

Next, give a few test case of CURD to demonstrate the effect of our integration.

@ RestControllerpublic class TestController {@ Autowired private TestRepository testRepository; @ GetMapping ("/ save") public TestEntity save (Integer id, String name) {TestEntity testEntity = new TestEntity (); testEntity.setId (id); testEntity.setName (name); return testRepository.save (testEntity);} @ GetMapping ("/ update") public TestEntity update (Integer id, String name) {Optional entity = testRepository.findById (id) TestEntity testEntity = entity.get (); testEntity.setName (name); return testRepository.save (testEntity);} @ GetMapping ("/ list") public Iterable list () {return testRepository.findAll ();} @ GetMapping ("/ get") public TestEntity get (Integer id) {return testRepository.findById (id). Get () } @ GetMapping ("/ del") public boolean del (Integer id) {testRepository.deleteById (id); return true;}}

The measured case is as follows

# add a record curl 'http://localhost:8080/save?id=1&name=-Grey' # query record curl 'http://localhost:8080/get?id=1'# modify record curl' http://localhost:8080/update?id=1&name=-Grey Blog'# query all curl 'http://localhost:8080/list'# delete records curl' http://localhost:8080/del?id=1'

4. Sql file import

Notice all the previous steps, there is no indication of the need to take the initiative to create a table called test, which is different from the familiar mysql

At some point we may want to initialize the database with the prepared sql file, which can be done as follows

Corresponding sql file

Table structure schema-h3.sql

DROP TABLE IF EXISTS book_to_book_store;DROP TABLE IF EXISTS book_store;DROP TABLE IF EXISTS book;DROP TABLE IF EXISTS author;DROP SEQUENCE IF EXISTS authoritative authoritative SEQUENCE s_author_id START WITH 1 create TABLE author (id INT NOT NULL, first_name VARCHAR (50), last_name VARCHAR (50) NOT NULL, date_of_birth DATE, year_of_birth INT, address VARCHAR (50), CONSTRAINT pk_t_author PRIMARY KEY (ID)) CREATE TABLE book (id INT NOT NULL, author_id INT NOT NULL, co_author_id INT, details_id INT, title VARCHAR (400) NOT NULL, published_in INT, language_id INT, content_text CLOB, content_pdf BLOB, rec_version INT, rec_timestamp TIMESTAMP, CONSTRAINT pk_t_book PRIMARY KEY (id), CONSTRAINT fk_t_book_author_id FOREIGN KEY (author_id) REFERENCES author (id) CONSTRAINT fk_t_book_co_author_id FOREIGN KEY (co_author_id) REFERENCES author (id)) CREATE TABLE book_store (name VARCHAR (400) NOT NULL, CONSTRAINT uk_t_book_store_name PRIMARY KEY (name)) CREATE TABLE book_to_book_store (book_store_name VARCHAR) NOT NULL, book_id INTEGER NOT NULL, stock INTEGER, CONSTRAINT pk_b2bs PRIMARY KEY (book_store_name, book_id), CONSTRAINT fk_b2bs_bs_name FOREIGN KEY (book_store_name) REFERENCES book_store (name) ON DELETE CASCADE CONSTRAINT fk_b2bs_b_id FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE)

Data file data-h3.sql

INSERT INTO author VALUES (next value for s_author_id, 'George',' Orwell', '1903-06-25, 1903, null); INSERT INTO author VALUES (next value for s_author_id,' Paulo', 'Coelho',' 1947-08-24, 1947, null) INSERT INTO book VALUES (1,1, null, null, '1984, 1948, 1,' To know and not to know, to be conscious of complete truthfulness while telling carefully constructed lies, to hold simultaneously two opinions which cancelled out, knowing them to be contradictory and believing in both of them, to use logic against logic, to repudiate morality while laying claim to it, to believe that democracy was impossible and that the Party was the guardian of democracy, to forget, whatever it was necessary to forget, then to draw it back into memory again at the moment when it was needed, and then promptly to forget it again And above all, to apply the same process to the process itself-- that was the ultimate subtlety Consciously to induce unconsciousness, and then, once again, to become unconscious of the act of hypnosis you had just performed. Even to understand the word 'doublethink'' involved the use of doublethink..', null, 1,' 2010-01-01 00 null, null, 'Animal Farm', 1945, 1, null,' 2010-01-01 00 null, null,'O Alquimista', 1988, 4, null, null, 1, null) INSERT INTO book VALUES (4, 2, null, null, 'Brida', 1990, 2, null, null); INSERT INTO book_store (name) VALUES (' Orell F ü ssli'), ('Ex Libris'), ('Buchhandlung im Volkshaus') INSERT INTO book_to_book_store VALUES ('Orell F ü ssli', 1,10), (' Orell F ü ssli', 2,10), ('Orell F ü ssli', 3,10), (' Ex Libris', 1,1), ('Ex Libris', 3,2), ('Buchhandlung im Volkshaus', 3,1)

The above two files are ready, how do we import them next?

With sql-maven-plugin, in the pom configuration file, add the following paragraph

Org.codehaus.mojo sql-maven-plugin create-database-h3 generate-sources execute org.h3.Driver jdbc:h3:~/h3-db test True src/main/resources/schema-h3.sql src/main/resources/data-h3.sql com.h3database h3 1.4.200

Then do the following

After the import is successful, go to the h3 console to view the corresponding data

After reading this article, I believe you have some understanding of "sample Analysis of h2databse Integration in SpringBoot DB". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report