在Spring Boot 项目中简单整合PageHelper

本文介绍了如何在Spring Boot项目中集成MyBatis、mapper-spring-boot-starter和PageHelper,展示了如何通过XML配置和Java接口定义数据操作,并演示了分页查询、CRUD操作及测试。
摘要由CSDN通过智能技术生成

目标:能用就行

依赖项:

<!--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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值