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