Mybatis-plus自定义分页工具

news/2024/11/16 19:57:57/

Mybatis-plus自定义分页工具

这里主要是介绍通过MyBatis-Plus使用自定义分页工具进行条件分页查询示例等,方便以后查阅!!!

分页工具类-PageUtils

PageUtils

package com.wl.cloud.core.utils;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;import java.util.Objects;
import java.util.stream.Collectors;/*** @author: wanglin* @date: 2023-07-12 周三* @Version: 1.0* @Description: 分页工具类*/
public class PageUtils {private static ApplicationContext applicationContext;public static void setApplicationContext(ApplicationContext applicationContext) {PageUtils.applicationContext = applicationContext;}public static <T> Page<T> transferPage(Pageable pageable) {if (Objects.isNull(pageable)) {return new Page(1, 10);}int pageNumber = pageable.getPageNumber();int pageSize = pageable.getPageSize();Page page = new Page(pageNumber + 1, pageSize);Sort sort = pageable.getSort();if (sort.isUnsorted()) {return page;}SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);Configuration configuration = sqlSessionFactory.getConfiguration();boolean underCamel = configuration.isMapUnderscoreToCamelCase();page.setOrders(pageable.getSort().stream().map(r -> new OrderItem(underCamel ? StringUtils.camelToUnderline(r.getProperty()) : r.getProperty(), r.isAscending())).collect(Collectors.toList()));return page;}public static <T> void transferSort(QueryWrapper queryWrapper, Sort sort) {if (Objects.isNull(queryWrapper) || Objects.isNull(sort) || sort.isUnsorted()) {return;}SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);Configuration configuration = sqlSessionFactory.getConfiguration();boolean underCamel = configuration.isMapUnderscoreToCamelCase();for (Sort.Order order : sort) {queryWrapper.apply("1=1");if (order.isAscending()) {queryWrapper.orderByAsc(underCamel ? StringUtils.camelToUnderline(order.getProperty()) : order.getProperty());} else {queryWrapper.orderByDesc(underCamel ? StringUtils.camelToUnderline(order.getProperty()) : order.getProperty());}}}
}

配合mapper文件使用

serverImpl文件

@Transactional(readOnly = true)@Overridepublic DataStoreDTO<ProcessVO> page(Pageable pageable, ProcessQueryDTO queryDto) {QueryWrapper<Process> queryWrapper = this.buildQuery(queryDto);Page<ProcessVO> page = PageUtils.transferPage(pageable);IPage<ProcessVO> result = this.processMapper.getPage(page, queryWrapper);return new DataStoreDTO(result.getTotal(), result.getRecords());}@Transactional(readOnly = true)@Overridepublic List<ProcessVO> list(Sort sort, ProcessQueryDTO queryDto) {QueryWrapper<Process> queryWrapper = this.buildQuery(queryDto);PageUtils.transferSort(queryWrapper, sort);return this.processMapper.getList(queryWrapper);}@Transactional(readOnly = true)@Overridepublic ProcessVO get(String id) {Assert.hasText(id, "id不能为空");ProcessVO vo = processMapper.getById(id);Assert.notNull(vo, "找不到id为 " + id + " 的记录");return vo;}

mapper文件

package com.wl.cloud.business.common.dao;import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.wl.cloud.business.common.domain.Process;
import com.wl.cloud.business.common.support.dto.query.ProcessQueryDTO;
import com.wl.cloud.business.common.support.vo.ProcessVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;import java.util.List;/*** 进程信息 Mapper接口** @author wanglin* @since 2024-03-22*/
@Mapper
public interface ProcessMapper extends BaseMapper<Process> {/*** 分页** @param page* @param ew* @return*/IPage<ProcessVO> getPage(IPage<ProcessVO> page, @Param(Constants.WRAPPER) Wrapper<Process> ew);/*** 列表** @param* @return*/List<ProcessVO> getList(@Param(Constants.WRAPPER) Wrapper<Process> ew);/*** 查询详情** @param id* @return*/ProcessVO getById(@Param("id") String id);
}

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="com.wl.cloud.business.common.dao.ProcessMapper"><!-- 通用查询结果列 --><sql id="Base_Column_List">id, create_by, create_time, update_by, update_time, deleted, description, name, path, port, project_url, server_id</sql><select id="getList" resultType="com.wl.cloud.business.common.support.vo.ProcessVO">select<include refid="com.wl.cloud.business.common.dao.ProcessMapper.Base_Column_List"></include>from gl_process<where>deleted = 0<if test="ew.sqlSegment !=null and ew.sqlSegment !=''">and ${ew.sqlSegment}</if></where></select><select id="getPage" resultType="com.wl.cloud.business.common.support.vo.ProcessVO">select<include refid="com.wl.cloud.business.common.dao.ProcessMapper.Base_Column_List"></include>from gl_process<where>deleted = 0<if test="ew.sqlSegment !=null and ew.sqlSegment !=''">and ${ew.sqlSegment}</if></where></select><select id="getById" parameterType="string" resultType="com.wl.cloud.business.common.support.vo.ProcessVO">select<include refid="com.wl.cloud.business.common.dao.ProcessMapper.Base_Column_List"></include>from gl_process<where>deleted = 0 andid = #{id}</where></select>
</mapper>

关注林哥,持续更新哦!!!★,°:.☆( ̄▽ ̄)/$:.°★ 。


http://www.ppmy.cn/news/1423067.html

相关文章

ceph large omap objects

问题 出现下面告警信息 ceph -scluster:id: xxxxxxxxxxxxxxxxxxxxxxxhealth: HEALTH_WARN7 large omap objects获取问题 pool # ceph health detail HEALTH_WARN 7 large omap objects; 4 clients failing to respond to cache pressure [WRN] LARGE_OMAP_OBJECTS: 7 larg…

安装Selenium和WebDriver

幻灯片4&#xff1a;安装Selenium和WebDriver 安装Python环境 步骤一&#xff1a;下载Python安装包 访问Python官方网站&#xff08;https://www.python.org/downloads/&#xff09;&#xff0c;根据您的操作系统选择对应的Python安装包进行下载。请确保下载最新稳定版本的P…

Spring Boot 框架集成Knife4j

本次示例使用 Spring Boot 作为脚手架来快速集成 Knife4j,Spring Boot 版本2.3.5.RELEASE,Knife4j 版本2.0.7&#xff0c;完整代码可以去参考 knife4j-spring-boot-fast-demo pom.xml 完整文件代码如下 <?xml version"1.0" encoding"UTF-8"?> &l…

Linux:Redis7.2.4的简单在线部署(1)

注意&#xff1a;我写的这个文章是以最快速的办法去搭建一个redis的基础环境&#xff0c;作用是为了做实验简单的练习&#xff0c;如果你想搭建一个相对稳定的redis去使用&#xff0c;可以看我下面这个文章 Linux&#xff1a;Redis7.2.4的源码包部署&#xff08;2&#xff09;-…

Java -- (part12)

一.权限修饰符 1.属性:用private ->封装思想 2.成员方法public ->便于调用 3.构造public ->便于new对象 二.final关键字 1.修饰类 a.格式 -- public final class 类名 b.特点:不能被继承 2.修饰方法 a.格式:修饰符 final 返回值类型 方法名(形参){} b.特点…

LeetCode-热题100:114. 二叉树展开为链表

题目描述 给你二叉树的根结点 root &#xff0c;请你将它展开为一个单链表&#xff1a; 展开后的单链表应该同样使用 TreeNode &#xff0c;其中 right 子指针指向链表中下一个结点&#xff0c;而左子指针始终为 null 。展开后的单链表应该与二叉树 先序遍历 顺序相同。 示例…

【分享】linux下安装sunshine串流配置进行远程办公

前排提示教程内容比较短&#xff0c;废话比较多&#xff0c;需要看教程的建议直接跳目录 目录 前言&#xff08;原因&#xff09; 选择远程连接软件 三种连接软件的优劣以及体验 sunshine支持显卡 教程 注意事项 显示器 如果为远程部署 前言&#xff08;原因&#xff0…

改进下记录学习的小网站

Strong改进 结束&#xff1a;2024-4-14 打算投入&#xff1a;10h 实际消耗&#xff1a;12h 3m 学习总是不在状态。 我的时间花得很零散&#xff0c;也有点茫然。所以想尝试一下集中式地、一块一块地花&#xff0c;比如投入30个小时&#xff0c;去干一件事&#xff0c;这样就可…