1、加入POM
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.7</version>
</dependency>
2、配置 application.properties
# spring h2 database configuration
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:mytest;MODE=MYSQL;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.schema-username=sa
spring.datasource.schema-password=sa
spring.datasource.data-username=sa
spring.datasource.data-password=sa
spring.datasource.schema=classpath:database/schema_*.sql
spring.datasource.data=classpath:database/data_*.sql
spring.datasource.initialization-mode=always
spring.jmx.enabled=false
# mybatis-plus
mybatis-plus.global-config.banner=false
mybatis-plus.global-config.db-config.id-type=auto
mybatis-plus.global-config.db-config.field-strategy=not_empty
mybatis-plus.global-config.db-config.table-underline=true
mybatis-plus.global-config.db-config.db-type=h2
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
3、设置schema和data文件
schema:
DROP TABLE IF EXISTS t_base_user;
CREATE TABLE t_base_user
(
id INT(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
user_name VARCHAR(255) NULL DEFAULT NULL COMMENT '用户姓名',
create_time DATETIME NULL DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (id)
);
data:
DELETE FROM t_base_user;
INSERT INTO t_base_user (user_name, create_time) VALUES
('Jone', now()),
('Jack', now()),
('Tom', now()),
('Sandy', now()),
('Billie', now());
4、实体、Mapper、Service
@ApiModel(value = "用户实体")
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("t_base_user")
public class User implements Serializable {
@ApiModelProperty(value = "主键")
@TableId(value = "id", type = IdType.NONE)
private Long id;
@ApiModelProperty(value = "姓名")
private String userName;
@ApiModelProperty(value = "创建时间")
private Date createTime;
}
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
public interface UserService extends IService<User> {
}
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}