分页,关键字:count, limit, offset
JPA 通常 repository, myBatis 则 mapper 多一些
mongodb, redis, spring data 官方推荐 repository
Java 中一个方法可以返回多个值吗?包装后可以。
数据库:注意:如果数据过多,可以采用
select * from city \G
工程目录:
application.properties
#数据源
spring.datasource.url=jdbc:mysql://阿里云ip:3306/world
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.http.log-request-details=true
logging.level.web=debug
City.java
package com.newer.page.pojo;
//驼峰式命名法
//+-------------+----------+------+-----+---------+----------------+
//| Field | Type | Null | Key | Default | Extra |
//+-------------+----------+------+-----+---------+----------------+
//| ID | int | NO | PRI | NULL | auto_increment |
//| Name | char(35) | NO | | | |
//| CountryCode | char(3) | NO | MUL | | |
//| District | char(20) | NO | | | |
//| Population | int | NO | | 0 | |
public class City {
int id;
String name;
String countryCode;
String district;
int population;
public City() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
@Override
public String toString() {
return "City [id=" + id + ", name=" + name + ", countryCode=" + countryCode + ", district=" + district
+ ", population=" + population + "]";
}
}
CityRepository.java
package com.newer.page.repository;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.newer.page.pojo.City;
//JPA 通常 repository, myBatis 则 mapper 多一些
/**
* City的数据访问
* @author Admin
*
*/
@Mapper
public interface CityRepository {
/**
* 分页查询
*
* @param limit 记录行数
* @param offset 偏移量
* @return
*/
// 语句简单
@Select("select * from city where CountryCode='CHN' order by id limit #{limit} offset #{offset}")
// 用到动态SQL
// @SelectProvider
List find(@Param("limit") int limit, @Param("offset") int offset);
/**
* 记录总数
*
* @return int 记录总数
*/
@Select("select count(id) from city where CountryCode='CHN'")
int count();
}
AppService.java
package com.newer.page.service;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.newer.page.pojo.City;
import com.newer.page.repository.CityRepository;
import com.newer.page.util.Page;
/**
*
* 业务逻辑:粒度远大于POJO
* @author Admin
*
*/
@Service
public class AppService {
@Autowired
CityRepository cityRepository;
/**
*
* @param page 第几页,从0开始
* @param size 一页多少数据
* @return
*/
public Page cityByPage(int page,int size) {
// 10
// 总记录数
int count=cityRepository.count();
// 第page+1页的数据
List list=cityRepository.find(size, size*page );
// 向上取整计算出页数
int total=(int)Math.ceil(Double.valueOf(count)/size);
//
// HashMap data=new HashMap<>();
//// 列表数据
// data.put("list", list);
//// 总页数
// data.put("total", total);
//// 当前页码
// data.put("current", page);
Page cityPage=new Page<>();
cityPage.setData(list);
cityPage.setTotal(total);
cityPage.setCurrent(page);
return cityPage;
}
}
HomeController.java
package com.newer.page.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
// 返回视图
@GetMapping("/")
public String home() {
return "index.html";
}
}
CityController.java
package com.newer.page.controller;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.newer.page.pojo.City;
import com.newer.page.service.AppService;
import com.newer.page.util.Page;
@RestController
@RequestMapping("/api/city")
public class CityController {
@Autowired
AppService appService;
// Get/api/city ?p=5&s=20
/**
* 获得分页数据
* @param page 页码
* @param size 一页的记录数,可选,默认20条数据
* @return
*/
@GetMapping
public Page find(
@RequestParam(name="p",defaultValue = "0") int page,
@RequestParam(name="s",defaultValue = "20") int size
){
// 控制器调用业务逻辑
return appService.cityByPage(page, size);
}
}
Page.java
package com.newer.page.util;
import java.util.List;
/**
* 一页数据
*
* @author Admin
*
*/
public class Page {
/**
* 数据
*/
List data;
/**
* 总页数:基于0的开始到total-1
*/
int total;
/**
* 当前页数:基于0的开始
*/
int current;
public Page() {
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
}
index.html
数据分页
数据分页
Vue & Spring Boot &MyBatis& MySQL
编号
城市
国家
所在地区
人口
{{city.id}}
{{city.name}}
{{city.countryCode}}
{{city.district}}
{{city.population}}
程序运行,浏览器看效果: