走的慢的人,只要他不丧失目标,也比漫无目的徘徊的人走得快。——莱辛
首先新建一个项目,勾选MP和H2

编写配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| spring: datasource: driver-class-name: org.h2.Driver schema: classpath:db/schema-h2.sql data: classpath:db/data-h2.sql url: jdbc:h2:mem:test username: root password: test
logging: level: com.baomidou.mybatisplus.samples.quickstart: debug mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
然后是数据表结构以及数据文件:

schema-h2.sql
1 2 3 4 5 6 7 8 9 10 11
| DROP TABLE IF EXISTS user;
CREATE TABLE user ( id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID', name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名', age INT(11) NULL DEFAULT NULL COMMENT '年龄', email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱', version INT(8) NULL COMMENT '版本', PRIMARY KEY (id) );
|
data-h2.sql
1 2 3 4 5 6 7 8
| DELETE FROM user;
INSERT INTO user (id, name, age, email,version) VALUES (1, 'Jone', 18, 'test1@baomidou.com',1), (2, 'Jack', 20, 'test2@baomidou.com',1), (3, 'Tom', 28, 'test3@baomidou.com',1), (4, 'Sandy', 21, 'test4@baomidou.com',1), (5, 'Billie', 24, 'test5@baomidou.com',1);
|

然后是对应的用户类:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package com.ruben.simpleh2database.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.Version;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -7219188882388819210L; @TableId(value = "id", type = IdType.AUTO) private Long id; private String name; private Integer age; private String email; @Version private Integer version;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public Integer getVersion() { return version; }
public void setVersion(Integer version) { this.version = version; } }
|
以及Mapper
1 2 3 4 5 6 7 8 9 10 11 12
| package com.ruben.simpleh2database.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.ruben.simpleh2database.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
|
别忘了再加一点点小注解
1
| @MapperScan("com.ruben.*.mapper")
|

编写测试类,尝试查询列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.ruben.simpleh2database;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.ruben.simpleh2database.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest class SimpleH2databaseApplicationTests {
@Resource private UserMapper userMapper;
@Test void contextLoads() { userMapper.selectList(Wrappers.lambdaQuery()); }
}
|
运行,成功查询到数据
