目标:能用就行
依赖项:
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<!--pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
application.properties
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/linlidb?characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#指定sql映射文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
entity
package cn.tedu.linli.entity;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.math.BigDecimal;
import java.util.Objects;
/**
* table name: hat_city
* author name: 何某人
* create time: 2022-06-07 07:35:54
*/
public class HatCity implements Serializable{
/**
* 表id;
*/
private Long id;
/**
* 市id;
*/
private String cityId;
/**
* 市;
*/
private String city;
/**
* 上一级;
*/
private String father;
/**
* 创建时间
*/
private LocalDateTime gmtCreate;
/**
* 修改时间
*/
private LocalDateTime gmtModified;
public void setId(Long id){
this.id=id;
}
public Long getId(){
return id;
}
public void setCityId(String cityId){
this.cityId=cityId;
}
public String getCityId(){
return cityId;
}
public void setCity(String city){
this.city=city;
}
public String getCity(){
return city;
}
public void setFather(String father){
this.father=father;
}
public String getFather(){
return father;
}
public void setGmtCreate(LocalDateTime gmtCreate){
this.gmtCreate=gmtCreate;
}
public LocalDateTime getGmtCreate(){
return gmtCreate;
}
public void setGmtModified(LocalDateTime gmtModified){
this.gmtModified=gmtModified;
}
public LocalDateTime getGmtModified(){
return gmtModified;
}
@Override
public String toString() {
return "hat_city[" +
"id=" + id +
", cityId=" + cityId +
", city=" + city +
", father=" + father +
", gmtCreate=" + gmtCreate +
", gmtModified=" + gmtModified +
"]";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HatCity HatCity = (HatCity) o;
return id.equals(HatCity.id) && cityId.equals(HatCity.cityId) && city.equals(HatCity.city) && father.equals(HatCity.father) && gmtCreate.equals(HatCity.gmtCreate) && gmtModified.equals(HatCity.gmtModified);
}
@Override
public int hashCode() {
return Objects.hash(id , cityId , city , father , gmtCreate , gmtModified);
}
}
dto:
@Data
public class PageAndSize implements Serializable {
private Integer page;
private Integer size;
}
mapper
@Repository
public interface HatCityMapper {
/**
* 查
*/
List<HatCity> getAllHatCity();//获取所有城市
}
mapper.xml
<?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 属性: 必须的,用于指定此XML文件对应的接口,取值为接口的全限定名-->
<mapper namespace="cn.tedu.linli.mapper.HatCityMapper">
<!--查-->
<!-- List<HatCity> getAllHatCity();//获取所有城市-->
<select id="getAllHatCity" resultType="cn.tedu.linli.entity.HatCity">
select * from hat_city
</select>
</mapper>
service
import com.github.pagehelper.PageInfo;
public interface HatCityService {
PageInfo<HatCity> getAllHatCity(PageAndSize pageAndSize);
}
impl
@Service
public class HatCityServiceImpl implements HatCityService {
private HatCityMapper hatCityMapper;
@Autowired
public void setHatCityMapper(HatCityMapper hatCityMapper) {
this.hatCityMapper = hatCityMapper;
}
@Override
public PageInfo<HatCity> getAllHatCity(PageAndSize pageAndSize) {
Integer page = pageAndSize.getPage();
Integer size = pageAndSize.getSize();
//分页查询
PageHelper.startPage(page, size);
//查询所有城市
List<HatCity> hatCities = hatCityMapper.getAllHatCity();
//封装分页信息
PageInfo<HatCity> pageInfo = new PageInfo<>(hatCities);
return pageInfo;
}
}
tests
@SpringBootTest
public class HatCityServiceImplTests {
@Autowired
private HatCityService hatCityService;
@Test
public void testGetAllHatCity() {
PageAndSize pageAndSize = new PageAndSize();
pageAndSize.setPage(3);
pageAndSize.setSize(10);
PageInfo<HatCity> pageInfo = hatCityService.getAllHatCity(pageAndSize);
//下一页
System.out.println("下一页"+pageInfo.getNextPage());
//总页数
System.out.println("总页数"+pageInfo.getPages());
//前一页
System.out.println("前一页"+pageInfo.getPrePage());
/**
* 由于startRow和endRow不常用,这里说个具体的用法
* 可以在页面中"显示startRow到endRow 共size条数据"
*/
//当前页面第一个元素在数据库中的行号
System.out.println("当前页面第一个元素在数据库中的行号"+pageInfo.getStartRow());
//当前页面最后一个元素在数据库中的行号
System.out.println("当前页面最后一个元素在数据库中的行号"+pageInfo.getEndRow());
//总记录数
System.out.println("总记录数"+pageInfo.getTotal());
//当前页
System.out.println("当前页"+pageInfo.getPageNum());
//每页的数量
System.out.println("每页的数量"+pageInfo.getPageSize());
//当前页的数量
System.out.println("当前页的数量"+pageInfo.getSize());
//导航条上的第一页
System.out.println("导航条上的第一页"+pageInfo.getNavigateFirstPage());
//导航条上的最后一页
System.out.println("导航条上的最后一页"+pageInfo.getNavigateLastPage());
//所有导航页号
System.out.println("所有导航页号"+pageInfo.getNavigatepageNums().toString());
//导航页码数
System.out.println("导航页码数"+pageInfo.getNavigatePages());
//获取存入的所有数据
List<HatCity> hatCities = pageInfo.getList();
for (HatCity hatCity : hatCities) {
System.out.println(hatCity);
}
}
}
相对查询到的数据进行排序
如下操作
dto
@Data
public class PageAndSize implements Serializable {
private Integer page;
private Integer size;
private String orderBy;//里面包含排序的字段,以及添加排序的方式,默认是从小到大排列 例如, id desc (ps参数中间空格分开)
}
impl
@Service
public class HatCityServiceImpl implements HatCityService {
private HatCityMapper hatCityMapper;
@Autowired
public void setHatCityMapper(HatCityMapper hatCityMapper) {
this.hatCityMapper = hatCityMapper;
}
@Override
public PageInfo<HatCity> getAllHatCity(PageAndSize pageAndSize) {
Integer page = pageAndSize.getPage();
Integer size = pageAndSize.getSize();
String orderBy = pageAndSize.getOrderBy();
//分页查询
PageHelper.startPage(page, size, orderBy);//这里添加orderBy参数即可
//查询所有城市
List<HatCity> hatCities = hatCityMapper.getAllHatCity();
//封装分页信息
PageInfo<HatCity> pageInfo = new PageInfo<>(hatCities);
return pageInfo;
}
}