####实体类:User.java
package com.test.entity;
import java.util.Date;
import com.test.util.DateUtils;
/**
* 实体类 User
*/
public class User {
private int id;
private String username;
private String password;
private String phone;
public String subtime;
public String getSubtime() {
return subtime;
}
public void setSubtime(Date subtime) {
this.subtime = DateUtils.format(subtime,"yyyy-MM-dd");
}
public User() {
super();
}
public User(int id, String username, String password, String phone, String subtime) {
super();
this.id = id;
this.username = username;
this.password = password;
this.phone = phone;
this.subtime = subtime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
####工具类:page.java
package com.test.util;
import java.util.List;
/**
* 分页的工具类
*/
public class Page {
private int pageSize=6; //页大小
private int pageIndex=0; //当前页号
private int totalPageCount=0; //总页数
private int record=0; //记录总数
private Integer nextPage; //下一页
private Integer prePage; //上一页
private List datalist; //集合
public List getDatalist() {
return datalist;
}
public void setDatalist(List datalist) {
this.datalist = datalist;
}
//得到开始记录数
public int getSartRow(){
return (pageIndex-1)*pageSize;
}
//得到结束记录数
public int getEndRow(){
return pageSize;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageIndex() {
return pageIndex;
}
//得到当前页
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
//下一页
setNextPage();
//上一页
setPrePage();
}
public int getTotalPageCount() {
return totalPageCount;
}
//总页数
public void setTotalPageCount() {
int totalP = record % getPageSize() == 0 ? record / getPageSize() :
record/ getPageSize() + 1;
this.totalPageCount = totalP;
}
public int getRecord() {
return record;
}
//总记录数
public void setRecord(int record) {
this.record = record;
//设置总页数
setTotalPageCount();
}
public Integer getNextPage() {
return nextPage;
}
//设置下一页
public void setNextPage() {
this.nextPage = this.pageIndex+1;
}
public Integer getPrePage() {
return prePage;
}
//设置上一页
public void setPrePage() {
this.prePage =this.pageIndex-1;
if(this.prePage<1){
this.prePage=1;
}
}
}
id, username, password, phone,subtime
package com.test.mapper;
import java.util.HashMap;
import java.util.List;
import com.test.entity.User;
public interface UserMapper {
/**
* 获取总记录条数
*/
int getPageCount(User user);
/**
* 分页 带查询条件
*/
List findPagePrerequisite(HashMap map);
}
package com.test.service;
import com.test.entity.User;
import com.test.util.Page;
public interface UserService {
/**
* 获取总记录条数
*/
int getPageCountService(User user);
/**
* 分页 带查询条件
*/
Page findPagePrerequisiteService(User user,int pageIndex);
}
ServiceImpl 实现类:调用mapper(dao)接口
package com.test.service.impl;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.test.entity.User;
import com.test.mapper.UserMapper;
import com.test.service.UserService;
import com.test.util.Page;
@Service
public class UserServiceImpl implements UserService {
//调用dao接口
@Autowired
private UserMapper userMapper;
/**
* 获取分页总记录条数
*/
public int getPageCountService(User user) {
int pageCount = userMapper.getPageCount(user);
return pageCount;
}
/**
* 分页 带查询条件
*/
public Page findPagePrerequisiteService(User user,int pageIndex) {
HashMap map = new HashMap();
Page page = new Page();
//当前页
page.setPageIndex(pageIndex);
//封装总记录数
int getCount = userMapper.getPageCount(user);
page.setRecord(getCount);
//封装查询条件
map.put("username", user.getUsername());
//分页
map.put("stratRow", page.getSartRow());
map.put("endRow", page.getEndRow());
//封装每页显示的数据,调用分页方法,把map值传进去
List fileList = userMapper.findPagePrerequisite(map);
page.setDatalist(fileList);
return page;
}
}
package com.test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.test.entity.User;
import com.test.service.UserService;
import com.test.util.Page;
@Controller
@RequestMapping("/usercont")
public class UserController {
//调用Service接口
@Autowired
private UserService fileService;
/**
* 获取总记录条数
*/
@RequestMapping("/getPageCount")
@ResponseBody
public int getPageCount(User users) {
int count = fileService.getPageCountService(users);
return count;
}
/**
* 分页 带查询条件
*/
@RequestMapping("/page")
@ResponseBody
public Page findPagePrerequController(User users, int pageIndex){
Page page = new Page();
page = fileService.findPagePrerequisiteService(users,pageIndex);
return page;
}
}
####前台页面展示:index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
分页
Ajax分页
####显示效果:如图1(这个是还没加时间搜索条件的,下面加上)
图1
####这里用了一个时间控件daterangepicker,喜欢的可以自行搜搜看
1、首先在User.java实体类添加:beginTime和endTime两个字段
3、在UserServiceImpl.java实现类中,把beginTime和endTime加进map中
项目地址:ssm_ajax_page