pageNo
(第几页数据),pageSize
(每页的条数);这里返回的是json数据接口,实现方法在service层前端
@ResponseBody @GetMapping("/allPage") public String findAllPage( @RequestParam(required = true,defaultValue = "1") Integer pageNo, @RequestParam(required = false,defaultValue = "5") Integer pageSize) { return customerService.findAllPage(pageNo,pageSize); }
CustomerServiceImpl.javavue
@Override public String findAllPage(Integer pageNo, Integer pageSize) { List<Customer> customers = customerDao.findAll(); List<Map<String, String>> resultList = new ArrayList<>(); PageUtil<Customer> pageUtil = new PageUtil<>(); for (Customer customer : pageUtil.pageList(customers, pageNo, pageSize){ resultList.add(iterCustomer(customer)); } return JsonUtil.toJSON(resultList); }
PageUtil.javajava
public class PageUtil<T> { private int beginIndex;//起始索引 private int endIndex;//终止索引 public List<T> pageList(List<T> list, int pageNo, int pageSize) { int size = list.size(); beginIndex = (pageNo - 1) * pageSize; endIndex = pageNo * pageSize > size ? size : pageNo * pageSize; List<T> resultList = list.subList(beginIndex, endIndex); return resultList; } }
iterCustomer()方法git
public Map<String, String> iterCustomer(Customer customer) { Map<String, String> resultMap = new HashMap<>(); resultMap.put("id", customer.getId().toString()); resultMap.put("name", customer.getName()); resultMap.put("phone", customer.getPhone()); resultMap.put("email", customer.getEmail()); return resultMap; }
JsonUtil.javagithub
public class JsonUtil { //使用jackson 转换 json 数据的第一步 private static ObjectMapper MAPPER = new ObjectMapper(); static String jsonString=null; public static String toJSON(Object object){ try { //jackson转任意object对象 为json 字符串 jsonString = MAPPER.writeValueAsString(object); } catch (JsonProcessingException e) { e.printStackTrace(); } return jsonString; } }
第一种方法在数据量比较小时可使用,但当数据量很是大时,若是咱们仅须要某一页的几条数据,而去查找全部数据,显得没有必要,或者说代价太大,全部咱们如今采用第二种分页方式;sql
第二种方法与第一种相似,获取前端传来的页码和每页显示的条数,经过自定义SQL语句查询数据库来获得须要的数据;数据库
sql分页参考:json
Mysql复杂查询 或数组
【MySQL】条件查询之排序聚合分组分页查询mybatis
分析
int maxPage =(int) Math.ceil(count/pageSize.doubleValue());
CustomerController.java
@Controller public class CustomerController { @Autowired CustomerService customerService; @ResponseBody @GetMapping("/allSql") public String findAllPageSql( @RequestParam(required = true,defaultValue = "1") Integer pageNo, @RequestParam(required = false,defaultValue = "5") Integer pageSize) { /** * pageSize 是每页显示的条数 * pageNo 是页码,sql分页传递的第一个参数是开始的索引; * 计算公式:开始的索引 = (当前的页码 - 1) * 每页显示的条数 */ //数据总数 int count = customerService.count(); //计算最大页码 int maxPage =(int) Math.ceil(count/pageSize.doubleValue()); //当前页码超出最大页码返回最大页码值: pageNo = pageNo>maxPage?maxPage:pageNo; //分页开始的索引值 int index = (pageNo - 1) * pageSize > count ? count : (pageNo - 1) * pageSize; return customerService.findAllPageSql(index, pageSize); } }
CustomerServiceImpl.java
@Service public class CustomerServiceImpl implements CustomerService { @Autowired CustomerDao customerDao; @Override public String findAllPageSql(Integer index, Integer pageSize) { return JsonUtil.toJSON(customerDao.findAllPageSql(index, pageSize)); } }
CustomerDao.java
public interface CustomerDao { List<Customer> findAllPageSql(Integer index,Integer pageSize); }
CustomerMapper.xml
注意,这里参数为两个,获取参数时要使用#{param1}#{param2},或者经过封装Map的形式传参;
sql分页参考:
<?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="com.vue.dao.CustomerDao"> <select id="findAllPageSql" parameterType="java.lang.Integer" resultType="com.vue.entity.Customer"> SELECT * FROM t_customer LIMIT #{param1},#{param2} </select> </mapper>
Mapper.xml映射文件获取多个参数参考:
Mybatis中的Mapper.xml映射文件sql查询接收多个参数 或
Mybatis中的Mapper.xml映射文件sql查询接收多个参数