Search in sources :

Example 1 with PageParams

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()));
}
Also used : PageParams(com.mendmix.common.model.PageParams)

Example 2 with 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;
}
Also used : ArrayList(java.util.ArrayList) PageParams(com.mendmix.common.model.PageParams) Page(com.mendmix.common.model.Page) MappedStatement(org.apache.ibatis.mapping.MappedStatement) ResultHandler(org.apache.ibatis.session.ResultHandler)

Example 3 with PageParams

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());
}
Also used : JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MappedStatement(org.apache.ibatis.mapping.MappedStatement) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) PageParams(com.mendmix.common.model.PageParams) SelectBody(net.sf.jsqlparser.statement.select.SelectBody)

Example 4 with PageParams

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);
}
Also used : OrderBy(com.mendmix.common.model.OrderBy) Column(net.sf.jsqlparser.schema.Column) ArrayList(java.util.ArrayList) PageParams(com.mendmix.common.model.PageParams) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) MapperMetadata(com.mendmix.mybatis.metadata.MapperMetadata)

Example 5 with PageParams

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);
}
Also used : OrderBy(com.mendmix.common.model.OrderBy) PageParams(com.mendmix.common.model.PageParams) PageDataLoader(com.mendmix.mybatis.plugin.pagination.PageExecutor.PageDataLoader) UserEntity(com.mendmix.mybatis.test.entity.UserEntity) Test(org.junit.Test)

Aggregations

PageParams (com.mendmix.common.model.PageParams)5 OrderBy (com.mendmix.common.model.OrderBy)2 ArrayList (java.util.ArrayList)2 MappedStatement (org.apache.ibatis.mapping.MappedStatement)2 JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)1 Page (com.mendmix.common.model.Page)1 MapperMetadata (com.mendmix.mybatis.metadata.MapperMetadata)1 PageDataLoader (com.mendmix.mybatis.plugin.pagination.PageExecutor.PageDataLoader)1 UserEntity (com.mendmix.mybatis.test.entity.UserEntity)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 JSQLParserException (net.sf.jsqlparser.JSQLParserException)1 Column (net.sf.jsqlparser.schema.Column)1 Statement (net.sf.jsqlparser.statement.Statement)1 OrderByElement (net.sf.jsqlparser.statement.select.OrderByElement)1 SelectBody (net.sf.jsqlparser.statement.select.SelectBody)1 ResultHandler (org.apache.ibatis.session.ResultHandler)1 Test (org.junit.Test)1