用MyBatis-Plus实现分页和分页查询

导入依赖

如果你的MyBatis-Plus版本过高的话,需要在pom.xml文件下新增一个依赖,因为从v3.5.9起分页插件也就分离出来,需要单独引入依赖

<!--mybatis-plus依赖-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.11</version>
</dependency>
<!--分页插件依赖-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-jsqlparser</artifactId>
    <version>3.5.11</version>
</dependency>

初始化核心插件

@Configuration
public class MyBatisConfiguration {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //初始化核心插件
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

创建一个QueryPageParam类用来接收分页需要的东西,比如显示的条数和当前页码

@Data//需要导入Loombok依赖和下载相关插件,springboot版本过高的话就只能手动创建javaBean了
public class QueryPageParam {
//显示条数
    private static int PAGE_SIZE=10;
//当前页码
    private static int PAGE_NUM=1;

    private int pageSize=PAGE_SIZE;
    private int pageNum=PAGE_NUM;
//用来接收前端传过来的数据进行条件查询
    private HashMap param = new HashMap();
}

控制层Controller

//日志对象
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(StudentController.class);
//分页查询
@PostMapping("/listPage")
public Result listPage(@RequestBody QueryPageParam query) {
    log.info("分页查询参数:{}", query);
//使用plus提供的page,注意别导错包了
    Page<Student> page = new Page<>(query.getPageNum(), query.getPageSize());
    IPage result = studentService.getPage(page, query.getParam());
    return Result.success(result.getRecords(), result.getTotal());
}

服务层

public interface IStudentService extends IService<Student> {
    IPage getPage(Page<Student> page, HashMap param);
}

实现层相关代码

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public IPage getPage(Page<Student> page, HashMap param) {
        return studentMapper.getPage(page, param);
    }

}

Mapper层相关代码

@Mapper
public interface StudentMapper extends BaseMapper<Student> {

    IPage getPage(Page<Student> page, HashMap param);
}

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.lzy.mapper.StudentMapper">
    
    <select id="getPage" resultType="com.lzy.entity.Student" parameterType="java.util.HashMap">
        select * from student
        <where>
            <if test="param.name != null and param.name != ''">
                and name like concat('%',#{param.name},'%')
            </if>
            <if test="param.sex != null and param.sex != ''">
                and sex = #{param.sex}
            </if>
            <if test="param.tel != null and param.tel != ''">
                and tel like concat('%',#{param.tel},'%')
            </if>
        </where>
    </select>

</mapper>

测试结果

打工告成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值