pom 文件配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
application.yaml 配置
spring:
datasource:
# 内存模式 jdbc:h2:mem:test
url: jdbc:h2:mem:test
driver-class-name: org.h2.Driver
# 控制台访问 http://localhost:8080/h2-console
# jdbc 要与上面的url保持一致
h2:
console:
enabled: true
此时项目启动就可以访问 H2数据库了 http://localhost:8080/h2-console
其中dict_dto 为jpa 自动创建的表
entity
@Data
@Builder
@Entity
public class DictDto {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String dict_key;
private String dict_value;
}
dao
import org.springframework.data.jpa.repository.JpaRepository;
public interface DictDao extends JpaRepository<DictDto,String> {
}