配置application.yml
server:
port: 80
spring:
datasource:
dirverClassName: org.h2.Driver
url: jdbc:h2:file:F:\\project\\h2\\config\\h2Database;AUTO_SERVER=TRUE
username: root
password: 1234
jpa:
database: h2
hibernate:
ddl-auto: update
show-sql: true
h2:
console:
path: /h2
enabled: true
H2可以使用内存模式,服务器模式和分布式模式,但就把它当做普通的本地数据库的话配置成本地服务器模式即可。上面的就是配置到本地F:\\project\\h2\\config
文件夹下面,它会看是否存在h2Database文件,存在就从里面读取数据,不存在就新建。在jpa的配置中使用了update,意思是只要结构一样,用原来的,不一样推到重建。访问http://localhost/h2你会有惊喜发现!
pom配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
网友评论