分页技术----模拟百度算法的分页查询(可挪用)

本文介绍了如何在Java环境下,使用PageHelper分页插件实现分页查询,结合Bootstrap前端框架,模拟百度的分页算法。详细步骤包括配置PageHelper依赖,实现分页查询方法,以及在控制器和页面上的处理。算法逻辑覆盖了不同页码范围的显示策略,旨在提供一种实用的分页解决方案。
摘要由CSDN通过智能技术生成

分页技术----模拟百度算法的分页查询(可挪用)

编译环境:

Idea编译工具,jdk版本1.8,WebApp项目中

操作步骤:

1.在prom文件中引入PageHelper依赖
在这里插入图片描述

2.要引入gitHub上的分页插件PageHelper到spring容器(applicationContext-mybatis)中

<!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--配置mybatis 插件-->
        <property name="plugins">
            <set>
                <!--配置pageHelper 分页插件-->
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                            reasonable=true
                            supportMethodsArguments=true
                            params=count=countsql
                            autoRuntimeDialect=true
                        </value>
                    </property>
                </bean>
            </set>
        </property>
    </bean>

3.UserServiceImpl实现类的分页查询用户列表的方法中(dao层是应用了逆向工程的mybatis技术)

 /**
     * 分页查询用户列表(有条件)
     *
     * @param userName
     * @param userRole
     * @param curPage
     * @param pageSize
     * @return
     */
    @Override
    public List<User> getUserList2(String userName, Long userRole, Integer curPage, Integer pageSize) {
   
        UserExample userExample = new UserExample();
        if(!StringUtils.isNullOrEmpty(userName)){
   
            userExample.createCriteria().andUserNameLike("%"+userName+"%");
        }
        if(userRole>0){
   
            userExample.createCriteria().andUserRoleEqualTo((userRole));
        }

        /**
         * 根据当前页和当前页的页面大小进行分页,
         * 必须两条代码捆绑在一起
         */
        PageHelper.startPage(curPage,pageSize);
        List<User> userList = userMapper.selectByExample(userExample);

        return userList;
    }
 

4.UserController控制器类的分页查询方法中

/**
 * 根据条件进行分页查询
 * @return
 */
@RequestMapping("query2")
public String queryAllUserByCondition(Model model
, String userName
, Long userRole
, String pageNum){
   
    if(StringUtils.isNullOrEmpty(pageNum)){
   
        pageNum="1";
    }
    if(userRole==null||"".equals(userRole)){
   
        userRole=0L;
    }

    List<Role> roles = roleService.queryAllRole();

    List<User> userList2 = userService.getUserList2(userName, userRole, Integer.valueOf(pageNum), Constants.PAGE_SIZE);
    PageInfo<User> pageInfo=new PageInfo<User>(userList2);

    model.addAttribute("pageInfo",pageInfo);
    model.addAttribute("roleList",roles);
    model.addAttribute("userRole",userRole);
    model.addAttribute("userName",userName);
    return "user/userList2";
}

5.userList2页面中,引入了bootstrap前端框架技术,jstl的c标签支持,模拟百度分页算法

1)算法代码:

<
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值