zoukankan      html  css  js  c++  java
  • Pagehelper分页插件的使用

    Pagehelper分页插件的使用

    帮助后端实现分页查询的插件,使用效果:

    现在后端在数据层mapper只需要查询全部数据即可,如select * from students,不需要再定义使用page类结合查询语句来实现分页效果。之前sql语句:select * from student where sname like "胡%" limit 0,5;

    所以,只需要两个重要参数,查询页page和分页条数limit(通过page与limit可以得到真正SQL语句limit条件的第一个参数,limit值为第二个参数)

    1.首先pom文件的依赖引入
    <!--pagehelper分页的依赖-->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.1.10</version>
    </dependency>
    
    2.在mybatis的主配置文件中配置生效
    <!--pagehelper插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--设置数据库方言-->
            <property name="helperDialect" value="mysql"/>
        </plugin>
    </plugins>
    

    例题,条件查询并分页展示

    3.数据层(mapper或dao)

    备注:Customer:实体对象,String username(姓名), Integer state(取值:0或1)属性

    接口方法:List selectCustomerByCondition(@Param("username") String username,@Param("state") Integer state);

    配置:

    <select id="selectCustomerByCondition" resultType="com.hk.openapi.web.master.pojo.Customer">
            select * from customer
            <where>
                <if test="username!=null and username!=''">
                    and username like concat('%',#{username},'%')
                </if>
                <if test="state!=null">
                    and state = #{state}
                </if>
            </where>
    </select>
    
    4.业务层(service)

    接口方法:PageInfo selectCustomerByCondition(Integer page, Integer limit, String username, Integer state);

    实现方法:

    public PageInfo<Customer> selectCustomerByCondition(
    Integer page, Integer limit, String username, Integer state) {
            //先配置这两个重要参数,当前页与每页条数
            PageHelper.startPage(page, limit);
            //再获取分页前的所有数据(List<T>集合)-->
            List<Customer> customerList = customerMapper.selectCustomerByCondition(username, state);
            //创建一个PageInfo对象,将集合数据丢进去
            return new PageInfo<Customer>(customerList);
    }
    
    5.控制层(controller(servlet))

    备注:前面两个分页参数和后面两个查询参数由前端提供过来

    @Autowired
    private ICustomerService customerService;//操作业务层的对象

     PageInfo<Customer> pageInfo = customerService.selectCustomerByCondition(page,limit,username,state);
    
    Don't just say it. Show me your code.
  • 相关阅读:
    WPF 自适应布局控件
    c# 将Datarow转成Datarowview
    C# 全局Hook在xp上不回调
    WPF datagrid AutoGenerateColumns隐藏部分列
    WPF wpf中按钮操作权限控制
    C# autofac配置文件中设置单例
    Castle ActiveRecord 二级缓存使用 异常记录
    VS2013 抛出 stackoverflow exception 的追踪
    CastleActiveRecord在多线程 事务提交时数据库资源竞争导致更新失败的测试结果记录
    WF4.0 工作流设计器 传入参数问题记录?
  • 原文地址:https://www.cnblogs.com/bigbeardhk/p/13216542.html
Copyright © 2011-2022 走看看