use of com.mendmix.common.model.PageParams in project jeesuite-libs by vakinge.
the class PageSqlUtils method main.
public static void main(String[] args) throws IOException {
String sql = "select a.*,\nSUM(a.c) from audited_policy a where 1=1\nand title like CONCAT('%',?,'%')\norder by updated_at desc";
// sql = FileUtils.readFileToString(new File("D:\\datas\\1.txt"),
// StandardCharsets.UTF_8);
System.out.println(">>>>" + getCountSql(sql));
System.out.println(">>>>" + getLimitSQL(DatabaseType.mysql, sql, new PageParams()));
}
use of com.mendmix.common.model.PageParams in project jeesuite-libs by vakinge.
the class PaginationHandler method onInterceptor.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Object onInterceptor(InvocationVals invocation) throws Throwable {
PageParams pageParams = invocation.getPageParam();
if (pageParams == null)
return null;
final MappedStatement orignMappedStatement = invocation.getMappedStatement();
if (!orignMappedStatement.getSqlCommandType().equals(SqlCommandType.SELECT))
return null;
if (invocation.getSql() == null) {
List<Object> list = new ArrayList<>(1);
list.add(new Page<Object>(invocation.getPageParam(), 0, null));
return list;
}
final ResultHandler resultHandler = (ResultHandler) invocation.getArgs()[3];
// 查询总数
Long total = executeQueryCount(invocation, resultHandler);
// 查询分页数据
List<?> datas = executeQuery(invocation, resultHandler);
Page<Object> page = new Page<Object>(pageParams, total, (List<Object>) datas);
List<Page<?>> list = new ArrayList<Page<?>>(1);
list.add(page);
return list;
}
use of com.mendmix.common.model.PageParams in project jeesuite-libs by vakinge.
the class SqlRewriteHandler method rewriteSql.
/**
* @param invocation
* @param dataMappings
* @return
*/
private void rewriteSql(InvocationVals invocation, Map<String, String[]> dataMapping) {
String orignSql = invocation.getSql();
PageParams pageParam = invocation.getPageParam();
boolean sharddingTenant = false;
if (isFieldSharddingTenant && !CurrentRuntimeContext.getIgnoreTenant()) {
sharddingTenant = true;
}
boolean softDelete = softDeleteMappedStatements.contains(invocation.getMapperNameSpace()) || softDeleteMappedStatements.contains(invocation.getMappedStatement().getId());
if (softDelete) {
if (dataMapping == null)
dataMapping = new HashMap<>(1);
dataMapping.put(softDeletePropName, new String[] { softDeleteFalseValue });
}
if (deptPropName != null && dataMapping != null && dataMapping.containsKey(orgBasePermKey)) {
if (deptMappedStatements.contains(invocation.getMapperNameSpace()) || deptMappedStatements.contains(invocation.getMappedStatement().getId())) {
String departmentId = CurrentRuntimeContext.getAndValidateCurrentUser().getDeptId();
if (StringUtils.isBlank(departmentId)) {
throw new JeesuiteBaseException("当前登录用户部门ID为空");
}
String[] values = dataMapping.get(orgBasePermKey);
if (values != null && values.length > 0) {
if (MatchPolicy.exact.name().equals(values[0])) {
dataMapping.put(deptPropName, new String[] { departmentId + QUERY_FUZZY_CHAR });
} else {
dataMapping.put(deptPropName, new String[] { departmentId });
}
} else {
dataMapping.put(deptPropName, new String[] { departmentId });
}
}
}
if (!softDelete && dataMapping == null && !sharddingTenant) {
if (pageParam == null || (pageParam.getOrderBys() == null || pageParam.getOrderBys().isEmpty())) {
return;
}
}
SelectBody selectBody = null;
try {
Statement stmt = CCJSqlParserUtil.parse(orignSql);
selectBody = ((Select) stmt).getSelectBody();
} catch (JSQLParserException e) {
logger.error("PARSER_ERROR[" + orignSql + "]", e);
throw new RuntimeException("sql解析错误");
}
handleSelectRewrite(selectBody, invocation, dataMapping, sharddingTenant);
//
invocation.setRewriteSql(selectBody.toString());
}
use of com.mendmix.common.model.PageParams in project jeesuite-libs by vakinge.
the class SqlRewriteHandler method handleTableOrderBy.
private void handleTableOrderBy(PlainSelect selectBody, Table table, InvocationVals invocation) {
PageParams pageParam = invocation.getPageParam();
if (pageParam == null || pageParam.getOrderBys() == null || pageParam.getOrderBys().isEmpty()) {
return;
}
List<OrderByElement> orderByElements = new ArrayList<>(pageParam.getOrderBys().size());
OrderByElement orderByElement;
for (OrderBy orderBy : pageParam.getOrderBys()) {
if (orderBy == null)
continue;
MapperMetadata mapperMeta = MybatisMapperParser.getMapperMetadata(invocation.getMapperNameSpace());
String columnName = mapperMeta.getEntityMetadata().getProp2ColumnMappings().get(orderBy.getField());
if (columnName == null)
columnName = orderBy.getField();
orderByElement = new OrderByElement();
orderByElement.setAsc(OrderType.ASC.name().equals(orderBy.getSortType()));
orderByElement.setExpression(new Column(table, columnName));
orderByElements.add(orderByElement);
}
selectBody.setOrderByElements(orderByElements);
}
use of com.mendmix.common.model.PageParams in project jeesuite-libs by vakinge.
the class BaseMybatisTest method testPage.
@Test
public void testPage() {
Page<UserEntity> pageInfo;
UserEntity example = new UserEntity();
example.setType((short) 1);
PageParams pageParams = new PageParams(1, 10, new OrderBy("name"));
pageInfo = PageExecutor.pagination(pageParams, new PageDataLoader<UserEntity>() {
@Override
public List<UserEntity> load() {
return userMapper.selectByExample(example);
}
});
System.out.println(pageInfo);
}
Aggregations