Ajax分页查询
Controller层
注意扫描业务逻辑层的注解是否正确否则SpringMVC无法识别
@Controller @RequestMapping("/xinmiao/v1/TStorageRecord") @ResponseBody public class TStorageRecordController { @Autowired private TStorageRecordService recordService; @RequestMapping("/getlist") public String getlist(TStorageRecord record, HttpServletRequest request){ Page page=new Page(); String currPageNoStr=request.getParameter("currPageNo"); if(currPageNoStr==null||"".equals(currPageNoStr)){ page.setCurrPageNo(1); }else{ page.setCurrPageNo(Integer.parseInt(currPageNoStr)); } String pageSizeStr=request.getParameter("pageSize"); if (pageSizeStr==null||"".equals(pageSizeStr)){ page.setPageSize(2); }else{ page.setPageSize(Integer.parseInt(pageSizeStr)); } page.setTotalCount(recordService.getStudentCount()); page.setStudents(recordService.getStudentList(page.getCurrPageNo(),page.getPageSize())); String result= JSON.toJSONString(page); return result; } }
Mapper层
注意扫描mapper接口是否正确否则Spring无法识别
public interface TStorageRecordMapper { int getStudentCount(); List<TStorageRecord> getStudentList(@Param("currPageNo") int currPageNo, @Param("pageSize")int pageSize); }
Service层
注意扫描业务逻辑层的注解是否正确否则SpringMVC无法识别
public interface TStorageRecordService { int getStudentCount(); List<TStorageRecord> getStudentList(@Param("currPageNo") int currPageNo, @Param("pageSize")int pageSize); }
Serviceimpl层
@Service public class TStorageRecordServiceimpl implements TStorageRecordService { @Autowired private TStorageRecordMapper RecordMapper; @Override public int getStudentCount() { return RecordMapper.getStudentCount(); } @Override public List<TStorageRecord> getStudentList(int currPageNo, int pageSize) { int num=(currPageNo-1)*pageSize; return RecordMapper.getStudentList(num,pageSize); } }
until层
public class Page { private int totalPageCount = 0; //总页数 计算 根据每页展示记录数和记录总数计算出来的 private int pageSize = 10; //每页展示记录数。用户指定,通常有默认值 private int totalCount; // 记录总数。 数据库查询 private int currPageNo = 1; //当前页码 用户指定,默认显示第一页 private List<TStorageRecord> records; //每页微博集合 数据库查询 public int getTotalPageCount() { if (totalCount%pageSize==0){ return totalCount/pageSize; }else { return totalCount/pageSize+1; } } public void setTotalPageCount(int totalPageCount) { this.totalPageCount = totalPageCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getCurrPageNo() { return currPageNo; } public void setCurrPageNo(int currPageNo) { this.currPageNo = currPageNo; } public List<TStorageRecord> getStudents() { return records; } public void setStudents(List<TStorageRecord> students) { this.records = students; } }
mappers层
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="xinmiao.mapper.TStorageRecordMapper"> <select id="getStudentCount" resultType="java.lang.Integer"> select count(1) from t_storage_record </select> <select id="getStudentList" resultType="xinmiao.bean.TStorageRecord"> SELECT * FROM `t_storage_record` limit #{currPageNo},#{pageSize} </select> </mapper>