In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "SpringBoot how to integrate H2 memory database", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "SpringBoot how to integrate H2 memory database" this article.
Prepare for
JDK 1.8 or later
Maven 3 or later
Technology stack
Spring Data JPA
Spring Boot
Directory structure
Pom.xml
Jpa-example cn.merryyou 1.0-SNAPSHOT 4.0.0 h3-webconsole UTF-8 1.8 org.springframework.boot spring-boot-starter-data-jpa com.h3database h3 1.4.196 Org.projectlombok lombok org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.springframework.boot spring -boot-maven-plugin org.apache.maven.plugins maven-compiler-plugin 3.6.1 1.8 1.8 entity class User@Entity@Table (name = " T_user ") @ Datapublic class User {@ Id @ GeneratedValue (strategy = GenerationType.IDENTITY) private Long id Private String name; private String url; @ Override public String toString () {return "User {" + "id=" + id + ", name='" + name +'\'+ ", url='" + url +'\'+'}';}}
@ Table declares that this object maps to the database's data table, which allows you to specify the names of the table (talbe), directory (Catalog), and schema for the entity. This comment is not required, if not, the system uses the default value (the short class name of the entity).
@ Id declares this property as the primary key. This property value can be created by itself, but Hibernate recommends that it be generated through Hibernate
@ GeneratedValue specifies the generation policy for the primary key.
TABLE: using tables to save id values
IDENTITY:identitycolumn
SEQUENCR: sequence
AUTO: use the above three depending on the database
@ Column declares the mapping of this attribute to the database field.
AddressRepositorypublic interface UserRepository extends JpaRepository {}
Spring Data JPA includes some built-in Repository and implements some common methods: findone,findall,save, etc.
Application.ymlspring: datasource: url: jdbc:h3:mem:h3test;DB_CLOSE_DELAY=-1 DB_CLOSE_ON_EXIT=FALSE platform: h3 username: sa password: driverClassName: org.h3.Driver jpa: database-platform: org.hibernate.dialect.H2Dialect hibernate: ddl-auto: update properties: hibernate: show_sql: true use_sql_comments: true format_sql: true h3: console: enabled: true path: / console settings: trace: false Web-allow-others: falselogging: level: debug connection configuration
Configure the database connection in the application.yml file
Spring.datasource.url=jdbc:h3:mem:h3test, configure the connection address of the H3 database
Spring.datasource.driver-class-name=org.h3.Driver, configuring JDBC Driver
Spring.datasource.username=sa, user name of the configuration database
Spring.datasource.password=, configuration database password
When you have completed the dependency and connection configuration steps, you can use h3 in the program. Spring will automatically help you with the injection of DataSource.
Data initialization configuration
If you need to initialize the database when the program starts, configure the database in the application.properties file
Spring.datasource.schema=classpath:db/schema.sql, after this configuration, every time you start the program, the program will run the resources/db/schema.sql file and operate on the structure of the database.
Spring.datasource.data=classpath:db/data.sql, after this configuration, every time you start the program, the program will run the resources/db/data.sql file to manipulate the data in the database.
This configuration is very suitable for the development environment. I will put the structural construction sql of the database in resources/db/schema.sql and the data sql in resources/db/data.sql. In this way, I can get a new database every time I run the program. This eliminates the need for me to modify the contents of the data every time for testing.
H3 web consloe configuration
H3 web consloe is a database GUI management application, similar to phpMyAdmin. H3 web consloe is started automatically when the program is running. Of course, you can also make the following configuration.
Spring.h3.console.settings.web-allow-others=true, after this configuration, the h3 web consloe can be accessed remotely. Otherwise, it can only be accessed locally.
Spring.h3.console.path=/h3-console, with this configuration, you can access H3 web consloe through YOUR_URL/h3-console. YOUR_URL is the access URl for your program.
Spring.h3.console.enabled=true, with this configuration, the H3 web consloe will be started when the program is opened. Of course, this is the default, if you don't want to start H3 web consloe when you start the program, then set it to false.
UserRepositoryTest
@ SpringBootTest@RunWith (SpringRunner.class) @ Slf4jpublic class UserRepositoryTest {@ Autowired private UserRepository userRepository; @ Test public void saveTest () throws Exception {User user = new User (); user.setName ("Zheng Longfei"); user.setUrl ("http://merryyou.cn"); User result = userRepository.save (user); log.info (result.toString ()); Assert.assertNotNull (user.getId () } @ Test public void findOneTest () throws Exception {User user = userRepository.findOne (1l); log.info (user.toString ()); Assert.assertNotNull (user); Assert.assertTrue (1l==user.getId ());}}
H3 web consloe
These are all the contents of the article "how SpringBoot integrates H2 in-memory database". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.