MyBatis-Plus
哪吒 2020/1/1
点击勘误issues (opens new window),哪吒感谢大家的阅读
# MyBatis-Plus
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`
(
id BIGINT NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
Spring Boot2
Maven
Gradle
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.9</version>
</dependency>
Spring Boot3
Maven
Gradle
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.9</version>
</dependency>
配置
在 application.yml 配置文件中添加 H2 数据库的相关配置:
application.yml
# DataSource Config
spring:
datasource:
driver-class-name: org.h2.Driver
username: root
password: test
sql:
init:
schema-locations: classpath:db/schema-h2.sql
data-locations: classpath:db/data-h2.sql
上面的配置是任何一个 Spring Boot 工程都会配置的数据库链接信息,如果您使用的是其他数据库,如 MySQL,则需要修改相应的配置信息。
在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
Application.java
@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
编码
编写实体类 User.java:
User.java
@Data
@TableName("`user`")
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
编写 Mapper 接口类 UserMapper.java:
public interface UserMapper extends BaseMapper<User> {
}
@SpringBootTest
public class SampleTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
Assert.isTrue(5 == userList.size(), "");
userList.forEach(System.out::println);
}
}