分页技术----模拟百度算法的分页查询(可挪用)
编译环境:
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)算法代码:
<