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>
关注林哥,持续更新哦!!!★,°:.☆( ̄▽ ̄)/$:.°★ 。