您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页【苍穹外卖】Springboot中快速使用mybatis插件-PageHelper

【苍穹外卖】Springboot中快速使用mybatis插件-PageHelper

来源:化拓教育网

Springboot中快速使用mybatis插件-PageHelper

1. 导入Maven坐标

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>

2. 拦截查询方法

在Service层中调用查询数据方法之前,我们需要先调用插件中的分页查询的,传输页码单页记录数这两个参数,以帮助动态在sql语句中添加"limit <记录索引> <单页记录数>"关键字。

/**
 * 分页查询
 * @param employeePageQueryDTO
 * @return
 */
public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
    //开启分页查询拦截
    PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
    //调用查询方法
    Page<Employee> page = employeeMapper.queryPage(employeePageQueryDTO.getName());
    PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
    return pageResult;
}

3. 编写查询的方法和mapper接口

Mybatis中可以通过使用注解的方式编写简单的SQL语句,遇到复杂的sql文件则需要在mapper.xml文件中编写SQL语句(定义mapper接口之后需要添加@Mapper注解)

<mapper namespace="com.sky.mapper.EmployeeMapper">
    <select id="queryPage" resultType="com.sky.entity.Employee">
        select * from employee
        <where>
            <if test="name != null and name != '' ">
                and name like concat('%', #{name}, '%')
            </if>
        </where>
    </select>
</mapper>

4. 配置:扫描Mapper.xml的映射文件路径

5. 版本说明

  • Springboot:2.x
  • JDK:1.8

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务