增加分页插件limit参数配置.
This commit is contained in:
parent
6d4f1a6b44
commit
7f3d7d41ac
@ -119,6 +119,20 @@ public final class ClassUtils {
|
||||
throw ExceptionUtils.mpe("实例化对象时出现错误,请尝试给 %s 添加无参的构造方法", e, clazz.getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 实例化对象.
|
||||
*
|
||||
* @param clazzName 类名
|
||||
* @param <T> 类型
|
||||
* @return 实例
|
||||
* @since 3.3.2
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T newInstance(String clazzName) {
|
||||
return (T) newInstance(toClassConfident(clazzName));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -274,37 +274,25 @@ public class PaginationInterceptor extends AbstractSqlParserHandler implements I
|
||||
@Override
|
||||
public void setProperties(Properties prop) {
|
||||
String countSqlParser = prop.getProperty("countSqlParser");
|
||||
if (StringUtils.isNotBlank(countSqlParser)) {
|
||||
setSqlParser(countSqlParser);
|
||||
}
|
||||
String overflow = prop.getProperty("overflow");
|
||||
setOverflow(Boolean.parseBoolean(overflow));
|
||||
String limit = prop.getProperty("limit");
|
||||
String dialectType = prop.getProperty("dialectType");
|
||||
String dialectClazz = prop.getProperty("dialectClazz");
|
||||
setOverflow(Boolean.parseBoolean(overflow));
|
||||
if (StringUtils.isNotBlank(countSqlParser)) {
|
||||
setCountSqlParser(ClassUtils.newInstance(countSqlParser));
|
||||
}
|
||||
if (StringUtils.isNotBlank(dialectType)) {
|
||||
setDialectType(dialectType);
|
||||
}
|
||||
if (StringUtils.isNotBlank(dialectClazz)) {
|
||||
setDialectClazz(dialectClazz);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据配置文件设置 countSqlParser
|
||||
* @param countSqlParser
|
||||
*/
|
||||
public void setSqlParser(String countSqlParser) {
|
||||
try {
|
||||
Class<?> clazz = Class.forName(countSqlParser);
|
||||
if (ISqlParser.class.isAssignableFrom(clazz)) {
|
||||
ISqlParser sqlParser = ClassUtils.newInstance((Class<? extends ISqlParser>) clazz);
|
||||
setCountSqlParser(sqlParser);
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw ExceptionUtils.mpe("Class : %s is not found", countSqlParser);
|
||||
if (StringUtils.isNotBlank(limit)) {
|
||||
setLimit(Long.parseLong(limit));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置方言类型
|
||||
*
|
||||
|
@ -18,7 +18,6 @@ package com.baomidou.mybatisplus.extension.plugins.pagination;
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.DialectRegistry;
|
||||
@ -72,7 +71,7 @@ public class DialectFactory {
|
||||
@Deprecated
|
||||
private static IDialect getDialect(DbType dbType, String dialectClazz) {
|
||||
//这里需要注意一下,就的版本是把dbType和dialectClazz同时传进来的,所以会存在dbType是一定会有值,dialectClazz可能为空的情况,兼容需要先判断dialectClazz
|
||||
return StringUtils.isBlank(dialectClazz) ? DIALECT_REGISTRY.getDialect(dbType) : DIALECT_CACHE.computeIfAbsent(dialectClazz, DialectFactory::classToDialect);
|
||||
return StringUtils.isBlank(dialectClazz) ? DIALECT_REGISTRY.getDialect(dbType) : DIALECT_CACHE.computeIfAbsent(dialectClazz, ClassUtils::newInstance);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -83,23 +82,12 @@ public class DialectFactory {
|
||||
* @since 3.3.1
|
||||
*/
|
||||
public static IDialect getDialect(String dialectClazz) {
|
||||
return DIALECT_CACHE.computeIfAbsent(dialectClazz, DialectFactory::classToDialect);
|
||||
return DIALECT_CACHE.computeIfAbsent(dialectClazz, ClassUtils::newInstance);
|
||||
}
|
||||
|
||||
public static IDialect getDialect(DbType dbType) {
|
||||
return Optional.ofNullable(DIALECT_REGISTRY.getDialect(dbType))
|
||||
.orElseThrow(() -> new MybatisPlusException(String.format("%s database not supported.", dbType.getDb())));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private static IDialect newInstance(Class<? extends IDialect> dialectClazz) {
|
||||
IDialect dialect = ClassUtils.newInstance(dialectClazz);
|
||||
Assert.notNull(dialect, "The value of the dialect property in mybatis configuration.xml is not defined.");
|
||||
return dialect;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static IDialect classToDialect(String dialectClazz) {
|
||||
return ClassUtils.newInstance((Class<? extends IDialect>) ClassUtils.toClassConfident(dialectClazz));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,94 @@
|
||||
package com.baomidou.mybatisplus.extension.plugins.pagination;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
|
||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.DB2Dialect;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
|
||||
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.apache.ibatis.reflection.SystemMetaObject;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
|
||||
/**
|
||||
* @author nieqiurong 2020/4/10.
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PaginationInterceptorTest {
|
||||
|
||||
@Test
|
||||
void testSetCountSqlParser() {
|
||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
||||
MetaObject metaObject = SystemMetaObject.forObject(paginationInterceptor);
|
||||
Assertions.assertNull(metaObject.getValue("countSqlParser"));
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("countSqlParser", BlockAttackSqlParser.class.getName());
|
||||
paginationInterceptor.setProperties(properties);
|
||||
Assertions.assertEquals(metaObject.getValue("countSqlParser").getClass().getName(), BlockAttackSqlParser.class.getName());
|
||||
paginationInterceptor.setCountSqlParser(new TenantSqlParser());
|
||||
Assertions.assertEquals(metaObject.getValue("countSqlParser").getClass().getName(), TenantSqlParser.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetOverflow() {
|
||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
||||
MetaObject metaObject = SystemMetaObject.forObject(paginationInterceptor);
|
||||
Assertions.assertFalse((Boolean) metaObject.getValue("overflow"));
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("overflow", "true");
|
||||
paginationInterceptor.setProperties(properties);
|
||||
Assertions.assertTrue((Boolean) metaObject.getValue("overflow"));
|
||||
paginationInterceptor.setOverflow(false);
|
||||
Assertions.assertFalse((Boolean) metaObject.getValue("overflow"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetDbType() {
|
||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
||||
MetaObject metaObject = SystemMetaObject.forObject(paginationInterceptor);
|
||||
Assertions.assertNull(metaObject.getValue("dialectType"));
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("dialectType", "mysql");
|
||||
paginationInterceptor.setProperties(properties);
|
||||
Assertions.assertEquals(DbType.MYSQL, metaObject.getValue("dbType"));
|
||||
paginationInterceptor.setDbType(DbType.DB2);
|
||||
Assertions.assertEquals(DbType.DB2, metaObject.getValue("dbType"));
|
||||
paginationInterceptor.setDialectType("mysql");
|
||||
Assertions.assertEquals(DbType.MYSQL, metaObject.getValue("dbType"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetDialect() {
|
||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
||||
MetaObject metaObject = SystemMetaObject.forObject(paginationInterceptor);
|
||||
Assertions.assertNull(metaObject.getValue("dialectClazz"));
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("dialectClazz", MySqlDialect.class.getName());
|
||||
paginationInterceptor.setProperties(properties);
|
||||
Assertions.assertEquals(MySqlDialect.class.getName(), metaObject.getValue("dialect").getClass().getName());
|
||||
paginationInterceptor.setDialect(new DB2Dialect());
|
||||
Assertions.assertEquals(DB2Dialect.class.getName(), metaObject.getValue("dialect").getClass().getName());
|
||||
paginationInterceptor.setDialectClazz(MySqlDialect.class.getName());
|
||||
Assertions.assertEquals(MySqlDialect.class.getName(), metaObject.getValue("dialect").getClass().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetLimit(){
|
||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
||||
MetaObject metaObject = SystemMetaObject.forObject(paginationInterceptor);
|
||||
Assertions.assertEquals(500L, metaObject.getValue("limit"));
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("limit", "10086");
|
||||
paginationInterceptor.setProperties(properties);
|
||||
Assertions.assertEquals(10086L, metaObject.getValue("limit"));
|
||||
paginationInterceptor.setLimit(10010L);
|
||||
Assertions.assertEquals(10010L, metaObject.getValue("limit"));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user