MybatisPlus快速使用
1.MybatisPlus简介
MyBatis Plus是一个基于MyBatis的增强工具,旨在简化和增强与数据库交互的开发过程。它提供了一系列的功能和特性,使得使用MyBatis更加便捷和高效。
MyBatis Plus的一些主要特点和功能:
2.快速搭建
2.1初始化工程
创建一个空的 Spring Boot 工程
这里我们直接跳过
2.2添加依赖
引入 Spring Boot Starter 父工程:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5+ 版本</version>
<relativePath/>
</parent>
引入 spring-boot-starter、spring-boot-starter-test、mybatis-plus-boot-starter、mysql lombok依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
2.3配置
在application.yml配置文件中添加mysql数据库的相关配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/testdb?serverTimezone=GMT%2B8&useSSL=false
username: root
password: 123456
在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
@SpringBootApplication
@MapperScan("com.cqgcxy.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2.4编码
编写实体类 User
@Data
@TableName("`user`")
public class User {
@TableId(value = "uid", type = IdType.AUTO)
private Integer uid;
@TableField("userName")
private String username;
@TableField("userPassword")
private String userpassword;
private Integer sex;
private String hobbit;
private String degree;
private String note;
private Integer rule;
private Integer state;
}
编写 Mapper 包下的 UserMapper接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
手写编码太麻烦可以选择代码生成器,MybatisPlus有详细介绍,或者去
3.开始使用
MybatisPlus提供了很多模板供我们使用,我们在这里对部分模板进行学习, 选择测试类,进行功能测试:
UserMapper 中的 selectList() 方法的参数为 MP 内置的条件封装器 Wrapper,所以不填写就是无任何条件
@Test
void selectUserAll() {
List<User> userList = userMapper.selectList(null);
userList.forEach(System.out::println);
}
控制台输出
User(uid=1, username=aa, userpassword=123456, sex=0, hobbit=打游戏, degree=本科, note=张三, rule=0, state=1)
User(uid=2, username=bb, userpassword=123456, sex=1, hobbit=唱, degree=博士, note=李四, rule=0, state=1)
User(uid=3, username=cc, userpassword=123456, sex=0, hobbit=跳, degree=硕士, note=王五, rule=1, state=1)
User(uid=4, username=哈哈, userpassword=123456, sex=1, hobbit=rap, degree=本科, note=老六, rule=0, state=1)
User(uid=13, username=小小, userpassword=123456, sex=null, hobbit=null, degree=null, note=null, rule=null, state=null)
User(uid=14, username=大大, userpassword=123456, sex=null, hobbit=null, degree=null, note=null, rule=null, state=null)
User(uid=15, username=大明, userpassword=123456, sex=null, hobbit=null, degree=null, note=null, rule=null, state=null)
User(uid=16, username=小a, userpassword=987654, sex=null, hobbit=null, degree=null, note=null, rule=null, state=null)