mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-10-09 04:57:11 +08:00
优化:优化使用 MyBatis Plus ChainWrapper 的方式
This commit is contained in:
@@ -18,6 +18,12 @@ package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
|
||||
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
|
||||
import com.baomidou.mybatisplus.extension.toolkit.Db;
|
||||
|
||||
/**
|
||||
@@ -40,4 +46,60 @@ public interface BaseMapper<T> extends com.baomidou.mybatisplus.core.mapper.Base
|
||||
default boolean insertBatch(Collection<T> entityList) {
|
||||
return Db.saveBatch(entityList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式查询
|
||||
*
|
||||
* @return QueryWrapper 的包装类
|
||||
*/
|
||||
default QueryChainWrapper<T> query() {
|
||||
return ChainWrappers.queryChain(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式查询(lambda 式)
|
||||
*
|
||||
* @return LambdaQueryWrapper 的包装类
|
||||
*/
|
||||
default LambdaQueryChainWrapper<T> lambdaQuery() {
|
||||
return ChainWrappers.lambdaQueryChain(this, this.currentEntityClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式查询(lambda 式)
|
||||
*
|
||||
* @param entity
|
||||
* 实体对象
|
||||
* @return LambdaQueryWrapper 的包装类
|
||||
*/
|
||||
default LambdaQueryChainWrapper<T> lambdaQuery(T entity) {
|
||||
return ChainWrappers.lambdaQueryChain(this, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式更改
|
||||
*
|
||||
* @return UpdateWrapper 的包装类
|
||||
*/
|
||||
default UpdateChainWrapper<T> update() {
|
||||
return ChainWrappers.updateChain(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式更改(lambda 式)
|
||||
*
|
||||
* @return LambdaUpdateWrapper 的包装类
|
||||
*/
|
||||
default LambdaUpdateChainWrapper<T> lambdaUpdate() {
|
||||
return ChainWrappers.lambdaUpdateChain(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体类 Class 对象
|
||||
*
|
||||
* @return 实体类 Class 对象
|
||||
*/
|
||||
default Class<T> currentEntityClass() {
|
||||
return (Class<T>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseMapper.class, 0);
|
||||
}
|
||||
}
|
||||
|
@@ -34,11 +34,6 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
|
||||
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
@@ -267,53 +262,6 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式查询
|
||||
*
|
||||
* @return QueryWrapper 的包装类
|
||||
*/
|
||||
protected QueryChainWrapper<T> query() {
|
||||
return ChainWrappers.queryChain(baseMapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式查询(lambda 式)
|
||||
*
|
||||
* @return LambdaQueryWrapper 的包装类
|
||||
*/
|
||||
protected LambdaQueryChainWrapper<T> lambdaQuery() {
|
||||
return ChainWrappers.lambdaQueryChain(baseMapper, entityClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式查询(lambda 式)
|
||||
*
|
||||
* @param entity
|
||||
* 实体对象
|
||||
* @return LambdaQueryWrapper 的包装类
|
||||
*/
|
||||
protected LambdaQueryChainWrapper<T> lambdaQuery(T entity) {
|
||||
return ChainWrappers.lambdaQueryChain(baseMapper, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式更改
|
||||
*
|
||||
* @return UpdateWrapper 的包装类
|
||||
*/
|
||||
protected UpdateChainWrapper<T> update() {
|
||||
return ChainWrappers.updateChain(baseMapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式更改(lambda 式)
|
||||
*
|
||||
* @return LambdaUpdateWrapper 的包装类
|
||||
*/
|
||||
protected LambdaUpdateChainWrapper<T> lambdaUpdate() {
|
||||
return ChainWrappers.lambdaUpdateChain(baseMapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体类 ID 名称
|
||||
*
|
||||
|
@@ -78,9 +78,9 @@ public class MybatisPlusConfiguration {
|
||||
// 对于单一数据库类型来说,都建议配置该值,避免每次分页都去抓取数据库类型
|
||||
// PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
|
||||
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
|
||||
// 溢出总页数后是否进行处理
|
||||
// 溢出总页数后是否进行处理,默认不处理
|
||||
paginationInnerInterceptor.setOverflow(false);
|
||||
// 单页分页条数限制
|
||||
// 单页分页条数限制,默认无限制
|
||||
paginationInnerInterceptor.setMaxLimit(-1L);
|
||||
return paginationInnerInterceptor;
|
||||
}
|
||||
|
@@ -26,7 +26,6 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
@@ -56,7 +55,7 @@ public class QueryHelper {
|
||||
* @return MyBatis Plus 查询条件封装对象
|
||||
*/
|
||||
public static <Q, R> QueryWrapper<R> build(Q query) {
|
||||
QueryWrapper<R> queryWrapper = Wrappers.query();
|
||||
QueryWrapper<R> queryWrapper = new QueryWrapper<>();
|
||||
// 没有查询条件,直接返回
|
||||
if (query == null) {
|
||||
return queryWrapper;
|
||||
|
Reference in New Issue
Block a user