zoukankan      html  css  js  c++  java
  • Mybatis PageHelper 简单使用

    流程

    1,maven 依赖

    2,在 mybatis 配置文件启用插件

    3,修改 service 层

    依赖

    <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.1.2</version>
    </dependency>

    启用插件

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <property name="helperDialect" value="mysql" />
        </plugin>
    </plugins>

    service 层代码示例

    @Service("accountService")
    public class AccountServiceImpl implements IAccountService {
        @Autowired
        private AccountMapper am;
    
        @Override
        public PageInfo<Account> selectBySearch(Account record, String startDate, String endDate, String companyName,
                Integer currentPage, Integer pageSize) {
            
            // 如果没有传递分页属性值,默认第一页,每页10条数据
            if(currentPage == null) {
                currentPage = 1;
            }
            if(pageSize == null) {
                pageSize = 10;
            }
            
            // 这是关键,设置分页属性
            PageHelper.startPage(currentPage, pageSize);
            
            // 和 mapper 接口文件中的方法一致
            List<Account> list = am.selectBySearch(record, startDate, endDate, companyName);
            
            // 分页成功,返回
            PageInfo<Account> pageInfo = new PageInfo<>(list);
            return pageInfo;
        }
    }
  • 相关阅读:
    idea初始化配置
    常用网址
    linux改错了profile文件
    获得ip地址[转载]
    java 基本数据类型转换
    log4j配置概要
    HTTP状态码
    HTTP 的请求方式
    10、类和方法
    9、一切都是对象
  • 原文地址:https://www.cnblogs.com/huanggy/p/9326594.html
Copyright © 2011-2022 走看看