提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
记录一下使用mybatisplus 的方法,还有它内置的分页如何使用
我这里使用的数据库是mysql
一、引入依赖配置数据源 application.yml中加入com.baomidou mybatis-plus-boot-starter3.5.1 mysql mysql-connector-java8.0.29 runtime
# DataSource Config
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=utf8
username: root
password: root
二、实体类
package org.example.pojo;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class User{
private Integer id;
private String username;
private String password;
private String email;
private String comlocal;
private Double occupy;
private LocalDateTime registerTime;
private Integer powerid;
private String name;
private String face;
}
实体类不重要
三、mapper
这个主要是加个 @mapper 注释,继承BaseMapper<>,它里面包含了很多继承的增删改查
四、查询
这里使用基础的查询语句,查询全部
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
List users = userMapper.selectList(null);
users.forEach(System.out::println);
}
五、分页
在上面的基础上要配置一个拦截器
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 添加分页插件
PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor();
// 设置请求的页面大于最大页后操作,true调回到首页,false继续请求。默认false
pageInterceptor.setOverflow(false);
// 单页分页条数限制,默认无限制
pageInterceptor.setMaxLimit(500L);
// 设置数据库类型
pageInterceptor.setDbType(DbType.MYSQL);
interceptor.addInnerInterceptor(pageInterceptor);
return interceptor;
}
}
然后再对上面的查询语句稍做修改即可



