MybatisPlus分页插件
配置类
package com.atguigu.ssyx.common.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
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;
/**
* @author shen
* @date 2023/7/23
* @description : MybatisPlus分页插件配置
* @version: v1.0
**/
@Configuration
@MapperScan("com.atguigu.ssyx.*.mapper")
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// DbType.MYSQL 连接的数据库
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
实际使用
/**
* 分页请求
* @param page
* @param pageSize
* @param name
* @return
*/
@GetMapping("/page")
public R<Page> page(int page,int pageSize,String name){
// 构造分页构造器
Page pageInfo = new Page(page, pageSize);
// 构造条件构造器
LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();
if (name != null) {
// 添加过滤条件
lambdaQueryWrapper.like(Employee::getName,name);
// 添加排序条件
lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
}
// sql查询
Page data = employeeService.page(pageInfo, lambdaQueryWrapper);
return R.success(data);
}
Mybatis分页插件
注意:只有紧跟着PageHelper.startPage(pageNum,pageSize)的sql语句才被pagehelper起作用
maven依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
Mapper
@Mapper
public interface UserMapper {
public List<User> selectAll();
}
mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springboot.springboot_mybatis.mapper.UserMapper">
<select id="selectAll" resultType="User">
select * from user
</select>
</mapper>
或
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.shy.mapper.ArticleMapper">
<select id="list" resultType="com.shy.dao.Article">
select * from article
<where>
<if test="categoryId != null">
category_id = #{categoryId}
</if>
<if test="state != null">
state = #{state}
</if>
and create_user = ${userId}
</where>
</select>
</mapper>
serviceImpl
@Service
public class QueryUser2 implements Query2 {
@Autowired
UserMapper userMapper;
@Override
public List<User> selectAll(int pageNum,int pageSize) {
PageHelper.startPage(pageNum,pageSize);
// 注意:只有紧跟着PageHelper.startPage(pageNum,pageSize)的sql语句才被pagehelper起作用
List<User> users = userMapper.selectAll();
PageInfo<User> pageInfo = new PageInfo<>(users);
return pageInfo.getList();
}
}