mirror of https://github.com/elunez/eladmin
# 分页插件
parent
a90c851caf
commit
ea0e24dc36
|
@ -17,10 +17,14 @@ package me.zhengjie.base;
|
|||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.google.common.collect.Sets;
|
||||
import me.zhengjie.db.ElSpecification;
|
||||
import me.zhengjie.utils.QueryHelp;
|
||||
import me.zhengjie.utils.StringUtils;
|
||||
import me.zhengjie.utils.WhereFun;
|
||||
import me.zhengjie.utils.WrapperUtils;
|
||||
import me.zhengjie.utils.enums.DbType;
|
||||
|
@ -295,12 +299,21 @@ public class BaseRepository<I extends IService<T>, J extends JpaRepository<T, ID
|
|||
com.baomidou.mybatisplus.extension.plugins.pagination.Page<T> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
|
||||
page.setPages(pageable.getPageNumber());
|
||||
page.setSize(pageable.getPageSize());
|
||||
page.setCurrent(pageable.getOffset());
|
||||
page.setCurrent(pageable.getOffset() / page.getSize() + 1);
|
||||
final Sort sort = pageable.getSort();
|
||||
final ArrayList<OrderItem> orders = new ArrayList<>();
|
||||
|
||||
final Class<T> clazz = specifications.getClazz();
|
||||
final TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
|
||||
final String name = clazz.getName();
|
||||
|
||||
sort.get().forEach(order -> {
|
||||
final Sort.Direction direction = order.getDirection();
|
||||
final String property = order.getProperty();
|
||||
/**
|
||||
* 翻译位 数据库 Column
|
||||
*/
|
||||
final String tableColumnFromField = QueryHelp.getTableColumnFromField(tableInfo, name, order.getProperty());
|
||||
final String property = StringUtils.isNotBlank(tableColumnFromField) ? tableColumnFromField : order.getProperty();
|
||||
final OrderItem orderItem;
|
||||
switch (direction) {
|
||||
case DESC:
|
||||
|
@ -315,7 +328,9 @@ public class BaseRepository<I extends IService<T>, J extends JpaRepository<T, ID
|
|||
orders.add(orderItem);
|
||||
});
|
||||
page.setOrders(orders);
|
||||
return new PageImpl<T>(mpService.page(page, queryWrapper).getRecords(),
|
||||
final List<T> records1 = mpService.getBaseMapper().selectPage(page, queryWrapper).getRecords();
|
||||
final List<T> records = mpService.page(page, queryWrapper).getRecords();
|
||||
return new PageImpl<T>(records,
|
||||
pageable, page.getTotal());
|
||||
}
|
||||
|
||||
|
|
|
@ -43,6 +43,21 @@ public class ElSpecification<K> implements Specification {
|
|||
return QueryHelp.getQueryWrapper(criteria, clazz);
|
||||
}
|
||||
|
||||
public Object getCriteria() {
|
||||
return criteria;
|
||||
}
|
||||
|
||||
public Class<K> getClazz() {
|
||||
return clazz;
|
||||
}
|
||||
|
||||
public Specification<K> getSpecification() {
|
||||
return specification;
|
||||
}
|
||||
|
||||
public void setSpecification(Specification<K> specification) {
|
||||
this.specification = specification;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a WHERE clause for a query of the referenced entity in form of a {@link Predicate} for the given
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package me.zhengjie.db;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* MybatisPlus 分页插件
|
||||
*
|
||||
* @author liaojinlong
|
||||
* @since 2020/7/6 22:32
|
||||
*/
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@MapperScan({
|
||||
"me.zhengjie.repository.mp",
|
||||
"me.zhengjie.modules.system.repository.mp"
|
||||
})
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
/**
|
||||
* 分页插件
|
||||
*/
|
||||
@Bean
|
||||
public PaginationInterceptor paginationInterceptor() {
|
||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
||||
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
|
||||
paginationInterceptor.setOverflow(false);
|
||||
// 设置最大单页限制数量,默认 500 条,-1 不受限制
|
||||
paginationInterceptor.setLimit(500);
|
||||
// 开启 count 的 join 优化,只针对部分 left join
|
||||
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
|
||||
return paginationInterceptor;
|
||||
}
|
||||
}
|
||||
|
|
@ -42,7 +42,7 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||
public class QueryHelp {
|
||||
public static ThreadLocal<QueryWrapper> queryWrapperThreadLocal = new ThreadLocal<>();
|
||||
protected static Map<String, List<Field>> FIELD_CACHE = new ConcurrentHashMap<>(16);
|
||||
protected static Map<Field, ElField> COLUMN_CACHE = new ConcurrentHashMap<>(16);
|
||||
protected static Map<String, ElField> COLUMN_CACHE = new ConcurrentHashMap<>(16);
|
||||
|
||||
/**
|
||||
* JPA 查询参数构建
|
||||
|
@ -228,7 +228,7 @@ public class QueryHelp {
|
|||
if (Objects.isNull(value)) {
|
||||
return;
|
||||
}
|
||||
attributeName = getTableColumnFromField(tableInfo, field);
|
||||
attributeName = getTableColumnFromField(tableInfo, clazz.getName(), field.getName());
|
||||
} catch (IllegalAccessException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return;
|
||||
|
@ -316,6 +316,7 @@ public class QueryHelp {
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 依据Mybatis Plus 获取 Database 真实Column 字段
|
||||
*
|
||||
|
@ -323,16 +324,17 @@ public class QueryHelp {
|
|||
* @param field
|
||||
* @return /
|
||||
*/
|
||||
public static String getTableColumnFromField(TableInfo tableInfo, Field field) {
|
||||
public static String getTableColumnFromField(TableInfo tableInfo, String className, String field) {
|
||||
String columnName = null;
|
||||
if (FIELD_CACHE.containsKey(field)) {
|
||||
final String key = className + "_" + field;
|
||||
if (FIELD_CACHE.containsKey(key)) {
|
||||
final ElField elField = COLUMN_CACHE.get(field);
|
||||
if (elField.isStatus()) {
|
||||
columnName = elField.getColumn();
|
||||
}
|
||||
} else {
|
||||
AtomicReference<String> tableColumnName = null;
|
||||
final String name = field.getName();
|
||||
final String name = field;
|
||||
for (TableFieldInfo item : tableInfo.getFieldList()) {
|
||||
if (item.getField().getName().equals(name)) {
|
||||
tableColumnName = new AtomicReference<>();
|
||||
|
@ -342,9 +344,9 @@ public class QueryHelp {
|
|||
}
|
||||
if (Objects.nonNull(tableColumnName)) {
|
||||
columnName = tableColumnName.get();
|
||||
COLUMN_CACHE.put(field, new ElField(columnName, true));
|
||||
COLUMN_CACHE.put(key, new ElField(columnName, true));
|
||||
} else {
|
||||
COLUMN_CACHE.put(field, new ElField(columnName, false));
|
||||
COLUMN_CACHE.put(key, new ElField(columnName, false));
|
||||
}
|
||||
}
|
||||
return columnName;
|
||||
|
|
|
@ -40,10 +40,6 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
@Api(hidden = true)
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement
|
||||
@MapperScan({
|
||||
"me.zhengjie.repository.mp",
|
||||
"me.zhengjie.modules.system.repository.mp"
|
||||
})
|
||||
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
|
||||
public class AppRun {
|
||||
|
||||
|
|
Loading…
Reference in New Issue