springboot配置内存数据库h2

  1. pom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
</dependencies>
  1. application.properties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 应用名称
spring.application.name=dynamic-form
# 应用服务 WEB 访问端口
server.port=8080

# h2 内存数据库,内存模式连接配置 库名: dynamic-form
#spring.datasource.url=jdbc:h2:mem:dynamic-form
spring.datasource.url=jdbc:h2:file:./data/dynamic-form
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=admin
spring.datasource.password=123456
# 初始化数据表 DDL
#spring.datasource.schema=classpath:sql/init.sql
# 初始化数据 DML
#spring.datasource.data=classpath:sql/data.sql

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true


# 开启console访问,默认false, http://localhost:8080/h2-console
spring.h2.console.enabled=true
# h2 访问路径上下文
spring.h2.console.path=/h2-console
# 开启h2 console 跟踪,方便调试,默认 false
spring.h2.console.settings.trace=false
# 不允许console远程访问,默认false
spring.h2.console.settings.web-allow-others=false

logging.level.cn.idea360.dynamicform=debug
  1. entity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 完整表单也可以直接存储为json
* @author cuishiying
*/
@Data
@Table(name = "form")
@Entity
public class Form {

@Id
@GeneratedValue
private int id;

/**
* 表单名
*/
@Column
private String name;
}
  1. orm
1
2
3
@Repository
public interface FormRepository extends JpaRepository<Form, Integer> {
}
  1. junit5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@SpringBootTest
@DisplayName("主表单单元测试")
class FormRepositoryTest {

@Resource
private FormRepository formRepository;

@DisplayName("新建表单")
@Test
void addForm() throws Exception {
Form form = new Form();
form.setName("动态表单");
Form save = formRepository.save(form);
assertEquals(1, save.getId());
}

}