方式一
分页前后端展示数据分析
请求参数包括页码、每页显示记录数、查询条件。
请求参数的json格式为:{currentPage:1,pageSize:10,queryString:’’itcast’’}
后台响应数据包括总记录数、当前页需要展示的数据集合。
响应数据的json格式为:{total:1000,rows:[]}
后台的处理方式
对前端所有的请求参数,封装成一个类QueryPageBean
/*** 封装查询条件*/public class QueryPageBean implements Serializable{private Integer currentPage;//页码private Integer pageSize;//每页记录数private String queryString;//查询条件public Integer getCurrentPage() {return currentPage;}public void setCurrentPage(Integer currentPage) {this.currentPage = currentPage;}public Integer getPageSize() {return pageSize;}public void setPageSize(Integer pageSize) {this.pageSize = pageSize;}public String getQueryString() {return queryString;}public void setQueryString(String queryString) {this.queryString = queryString;}}
对后台响应回前端的数据,封装为另一个类PageResult
/*** 分页结果封装对象*/public class PageResult implements Serializable{private Long total;//总记录数private List rows;//当前页结果public PageResult(Long total, List rows) {super();this.total = total;this.rows = rows;}public Long getTotal() {return total;}public void setTotal(Long total) {this.total = total;}public List getRows() {return rows;}public void setRows(List rows) {this.rows = rows;}}
1.2.1 Controller
在CheckItemController中增加分页查询方法
//分页查询@RequestMapping("/findPage")public PageResult findPage(@RequestBody QueryPageBean queryPageBean){PageResult pageResult = checkItemService.pageQuery(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());return pageResult;}
1.2.2 服务接口
在CheckItemService服务接口中扩展分页查询方法
public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString);
1.2.3 服务实现类
在CheckItemServiceImpl服务实现类中实现分页查询方法,基于Mybatis分页助手插件实现分页
public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {PageHelper.startPage(currentPage,pageSize);Page<CheckItem> page = checkItemDao.selectByCondition(queryString);return new PageResult(page.getTotal(),page.getResult());}
1.2.4 Dao接口
在CheckItemDao接口中扩展分页查询方法
public Page<CheckItem> selectByCondition(String queryString);
1.2.5 Mapper映射文件
在CheckItemDao.xml文件中增加SQL定义
<select id="selectByCondition" parameterType="string"resultType="com.itheima.pojo.CheckItem">select * from t_checkitem<if test="value != null and value.length > 0">where code = #{value} or name = #{value}</if></select>
总体结构分析
