springboot中使用mybatisplus分页插件
1.环境初始化
引入依赖
添加插件
在application中配置数据源
添加日志
添加mapper注解,表明mapper接口的位置
2.添加配置类
package top.roud.cms.common;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName: MybatisPlusConfig
* @Description:
* @Author roud
* @Date 2022/6/8
* @Version 1.0
*/
@Configuration
@MapperScan("top.roud.cms.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}
3.使用分页插件
package top.roud.cms.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.roud.cms.entity.User;
/**
* @ClassName: UserMapper
* @Description:
* @Author roud
* @Date 2022/6/8
* @Version 1.0
*/
public interface UserMapper extends BaseMapper<User> {
User selectUserByPhonenumberAndPassword(String phonenumber, String password);
User selectUserByPhonenumber(String phonenumber);
}
需要传入的参数为1.当前页码、2.每页显示的数据数、3.进行模糊查询的字符串
@Override
public Result findPage(Integer pageNum, Integer pageSize, String search) {
LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
if(StrUtil.isNotBlank(search)){
wrapper.like(User::getNickname, search);
}
Page<User> userPage = userMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
return Result.success(userPage);
}