一. Spring boot 集成 H2
H2是一个由Java编写的轻量级关系型数据库,它可以作为一个单独的数据库服务器运行,也可以被嵌入到Java应用程序中使用(主要用于开发和测试)。
- 添加maven依赖:
<!-- h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>runtime</scope>
</dependency>
<!--unit test-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- spring boot tester-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybatis-plus, for BaseMapper interface-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
- 创建SQL脚本
schema-h2.sql:
# db/schema-h2.sql
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL,
name VARCHAR(30) NULL DEFAULT NULL,
age INT(11) NULL DEFAULT NULL,
email VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (id)
);
data-h2.sql:
# db/data-h2.sql
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'zhangSan', 18, 'zhangSan@test.com'),
(2, 'liSi', 20, 'liSi@test.com'),
(3, 'wangWu', 28, 'wangWu@test.com');
- 创建实体类:@Data注解可以自动编写get/set等基本方法
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
- 创建mapper类:继承BaseMapper(来源于com.baomidou.mybatisplus.mapper 包)接口后,无需编写 mapper.xml 文件,即可获得CRUD(create, read, update, delete)功能
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
- 编写主配置文件:application-testing.yml
spring:
datasource:
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql
url: jdbc:h2:mem:testdb
username: sa
password: sa
continue-on-error: true
- 编写测试类:@ActiveProfiles注解是指定所使用的主配置文件profile,比如这是是"testing", 说明使用的是application-testing.yml文件。
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("testing")
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect(){
System.out.println("----selectAll method test----");
List<User> userList = userMapper.selectList(null);
Assert.assertEquals(3, userList.size());
userList.forEach(System.out::println);
}
}
- 运行测试类
二. 引入Liquibase
Liquibase就像数据库的Git,是一个用Java编写的开源数据库版本管理工具,用于跟踪、管理和应用数据库的变化。通用于多种数据库(MySQL,Oracle等)。
- 添加maven依赖:
<!-- liquibase -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
- 配置application.yaml文件: 如果没有给change-log则默认为: classpath:/db/changelog/db.changelog-master.yaml
spring:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@//xxxx.xx.xx
username: <your account>
password: <your password>
liquibase:
enabled: true
change-log: classpath:/db.changelog-master.yaml
- 在resource folder下创建db.changelog-master.yaml,内容如下:
databaseChangeLog:
- changeSet:
id: init-table
author: Lynn
changes:
- createTable:
tableName: person
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: userName
type: varchar(50)
- changeSet:
id: change-table
author: Lynn
changes:
- addColumn:
tableName: person
columns:
- column:
name: age
type: int
-
启动项目后可以看到changeSet顺次执行
-
相应的table也已经create成功: