Permalink
Browse files

集成H2数据库

  • Loading branch information...
testacount1 committed Aug 20, 2018
1 parent 16d678d commit dc29be78aaf388f6f2db5b3b1ca397e162e92565
View
11 pom.xml
@@ -5,7 +5,7 @@
<groupId>com.github.program_in_chinese</groupId>
<artifactId>programming-term-dictionary</artifactId>
<version>0.1.1</version>
<version>0.2.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
@@ -22,6 +22,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</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>
@@ -2,18 +2,37 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class 应用 {
public class 应用 implements CommandLineRunner {
private static final Logger 笔录 = LoggerFactory.getLogger(应用.class);
private final 词库接口 词库;
@Autowired
public 应用(词库接口 词库) {
this.词库 = 词库;
}
public static void main(String[] 参数) {
笔录.info("服务启动开始");
SpringApplication.run(应用.class, 参数);
笔录.info("服务已启动");
}
@Override
public void run(String ...参数) throws Exception {
词条 词条1 = new 词条(1L, "集合");
词条 词条2 = new 词条(2L, "字典");
笔录.info("添加H2数据");
词库.save(词条1);
词库.save(词条2);
笔录.info("数据条数: {}", 词库.count());
}
}
@@ -0,0 +1,9 @@
package com.github.program_in_chinese;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface 词库接口 extends JpaRepository<词条, Long> {
}
@@ -1,10 +1,18 @@
package com.github.program_in_chinese;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class 词条 {
// TODO: 仅为演示用
private final long id;
private final String 中文术语;
@Id
private long id;
private String 中文术语;
public 词条() {
}
public 词条(long id, String 中文术语) {
this.id = id;
this.中文术语 = 中文术语;
@@ -1,2 +1,10 @@
# 打开H2控制台
spring:
h2:
console:
enabled: true
datasource:
url: jdbc:h2:mem:testdb;MODE=Oracle;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
logging:
file: 日志.log

0 comments on commit dc29be7

Please sign in to comment.