在 Spring Boot 项目中集成 H2 数据库可以按照以下步骤进行:
在项目的pom.xml文件中添加 H2 数据库和 Spring Boot 的依赖,如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hxstrive</groupId>
<artifactId>demo_springboot</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>demo_springboot</name>
<description>Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.15</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.3.232</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.15</version>
</plugin>
</plugins>
</build>
</project>在 Spring Boot 的配置文件(通常是application.yml)中配置 H2 数据库的连接信息:
spring:
# 数据源配置
datasource:
url: jdbc:h2:mem:test-db;DB_CLOSE_DELAY=-1
username: sa
password:
driver-class-name: org.h2.Driver注意:这里使用内存数据库模式,连接名为test-db,用户名默认是sa且无密码。
创建 person 表对应的实体类:
package com.hxstrive.springboot.h2;
import lombok.Data;
import lombok.ToString;
import javax.persistence.*;
/**
* @author hxstrive.com
* @since 1.0.0 2024/9/24 13:56
*/
@Data
@Entity
@ToString
public class PersonEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
}通过继承 Spring Data JPA 的JpaRepository 接口,定义自己的数据访问层接口,如下:
package com.hxstrive.springboot.h2;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author hxstrive.com
* @since 1.0.0 2024/9/24 14:00
*/
public interface PersonRepository extends JpaRepository<PersonEntity, Long> {
//
}在服务类中可以注入数据访问层接口来操作 H2 数据库。例如:
package com.hxstrive.springboot.h2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author hxstrive.com
* @since 1.0.0 2024/9/24 14:01
*/
@Service
public class PersonService {
@Autowired
private PersonRepository personRepository;
public void save(PersonEntity person) {
personRepository.save(person);
}
public void delete(PersonEntity person) {
personRepository.delete(person);
}
public PersonEntity findById(Long id) {
return personRepository.findById(id).orElse(null);
}
public List<PersonEntity> findAll() {
return personRepository.findAll();
}
}编写一个 Spring Boot 测试类,注入 PersonService 服务类,测试 H2 数据库,代码如下:
package com.hxstrive.springboot.h2;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
/**
* @author hxstrive.com
* @since 1.0.0 2024/9/24 14:05
*/
@SpringBootTest
public class SpringBootDemoTest {
@Autowired
private PersonService personService;
@Test
public void contextLoads() {
// 保存
PersonEntity personEntity = new PersonEntity();
personEntity.setName("张三");
personService.save(personEntity);
// 查询
List<PersonEntity> personEntityList = personService.findAll();
for(PersonEntity person : personEntityList) {
System.out.println(person);
}
if(personEntityList.size() > 0) {
// 更新
PersonEntity person = personEntityList.get(0);
person.setName("张三-update");
personService.save(person);
}
// 查询
personEntityList = personService.findAll();
for(PersonEntity person : personEntityList) {
System.out.println(person);
}
// 删除
for(PersonEntity person : personEntityList) {
personService.delete(person);
}
}
}调用测试示例,输出如下:
PersonEntity(id=1, name=张三)
PersonEntity(id=1, name=张三-update)