com.gitee.cn9750wang.webtools.utils.MybatisPlusUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of web-tools Show documentation
Show all versions of web-tools Show documentation
web tools for spring-boot web project
The newest version!
/*
* Copyright 2021 wwy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitee.cn9750wang.webtools.utils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.cn9750wang.webtools.entity.SoftDeleteEntity;
import com.gitee.cn9750wang.webtools.error.defines.ParamErr;
import com.gitee.cn9750wang.webtools.query.AbstractQuery;
import com.gitee.cn9750wang.webtools.query.LimitPage;
import com.gitee.cn9750wang.webtools.vo.page.MybatisPlusPage;
import com.gitee.cn9750wang.webtools.vo.page.Page;
import com.gitee.cn9750wang.webtools.vo.page.PageImpl;
import java.util.Set;
/**
* MybatisPlus 相关工具类
* @author wwy
*/
public class MybatisPlusUtils {
private MybatisPlusUtils(){}
/**
* 转换为mybatis-plus分页插件的分页对象
* @param limitPage 分页查询对象
* @param 类型限制
* @return mybatis-plus分页对象
*/
public static MybatisPlusPage convert(LimitPage limitPage){
ParamErr.NOT_NULL.isNotNull(limitPage,"分页对象");
return new MybatisPlusPage<>(limitPage.getPage(), limitPage.getSize());
}
/**
* 转换为mybatis-plus分页插件的分页对象
* @param query 条件查询对象
* @param 类型限制
* @return mybatis-plus分页对象
*/
public static MybatisPlusPage convert(AbstractQuery query){
return convert(query,null);
}
/**
* 转换为mybatis-plus分页插件的分页对象
* @param query 条件查询对象
* @param enableSortField 可以进行排序的字段
* @param 类型限制
* @return mybatis-plus分页对象
*/
public static MybatisPlusPage convert(AbstractQuery query, Set enableSortField){
ParamErr.NOT_NULL.isNotNull(query,"查询条件");
MybatisPlusPage page = convert(query.getPage());
final var sort = query.getSort();
if(sort != null) {
// 检查是否可以排序
ParamErr.UN_SORT.isTrue(sort.isSortable(enableSortField),sort.getField());
page.setOrders(sort);
}
return page;
}
/**
* 将mybatis-plus的分页对象转换为自己的分页对象
* @param page mybatis-plus分页对象
* @param 数据类型
* @return 转换后分页对象
*/
public static Page page(IPage page){
if(page == null){
return Page.emptyPage();
}
var p = new PageImpl();
p.setItems(page.getRecords());
p.setTotal(page.getTotal());
p.setPage(page.getCurrent());
return p;
}
/**
* 获取UpdateWrapper,预设条件:仅修改未被软删除的数据
* @param 范型限制
* @return UpdateWrapper
*/
public static UpdateWrapper getNotSoftDeleteUpdateWrapper(){
return new UpdateWrapper().isNull("deleteTime");
}
/**
* 获取LambdaUpdateWrapper,预设条件:仅修改未被软删除的数据
* @param 范型限制
* @return LambdaUpdateWrapper
*/
public static LambdaUpdateWrapper getNotSoftDeleteLambdaUpdateWrapper(){
return new LambdaUpdateWrapper().isNull(SoftDeleteEntity::getDeleteTime);
}
}