Search in sources :

Example 6 with MapperMetadata

use of com.mendmix.mybatis.metadata.MapperMetadata in project jeesuite-libs by vakinge.

the class JeesuiteMybatisEnhancer method handle.

public static void handle(String group, Configuration configuration) throws Exception {
    if ("tkMapper".equals(MybatisConfigs.getCrudDriver())) {
        Class<?> helperClazz = Class.forName("tk.mybatis.mapper.mapperhelper.MapperHelper");
        Object helper = helperClazz.newInstance();
        Class<?> configClazz = Class.forName("tk.mybatis.mapper.entity.Config");
        Object config = configClazz.newInstance();
        Method method = configClazz.getDeclaredMethod("setNotEmpty", boolean.class);
        method.invoke(config, false);
        method = helperClazz.getDeclaredMethod("setConfig", configClazz);
        method.invoke(helper, config);
        method = helperClazz.getDeclaredMethod("registerMapper", Class.class);
        List<MapperMetadata> mappers = MybatisMapperParser.getMapperMetadatas(group);
        for (MapperMetadata mapper : mappers) {
            method.invoke(helper, mapper.getMapperClass());
        }
        method = helperClazz.getDeclaredMethod("processConfiguration", Configuration.class);
        method.invoke(helper, configuration);
    } else {
        new GeneralSqlGenerator(group, configuration).generate();
    }
    // pageHelper
    try {
        Class<?> pageHelperClazz = Class.forName("com.github.pagehelper.PageInterceptor");
        Interceptor pageInterceptor = (Interceptor) pageHelperClazz.newInstance();
        configuration.addInterceptor(pageInterceptor);
    } catch (Exception e) {
    }
    // 注册拦截器
    String[] hanlderNames = MybatisConfigs.getHandlerNames(group);
    JeesuiteMybatisInterceptor interceptor = new JeesuiteMybatisInterceptor(group, hanlderNames);
    configuration.addInterceptor(interceptor);
    interceptor.afterRegister();
    logger.info(">> JeesuiteMybatisEnhancer finshed -> group:{},hanlderNames:{}", group, hanlderNames);
}
Also used : Configuration(org.apache.ibatis.session.Configuration) Method(java.lang.reflect.Method) MapperMetadata(com.mendmix.mybatis.metadata.MapperMetadata) JeesuiteMybatisInterceptor(com.mendmix.mybatis.plugin.JeesuiteMybatisInterceptor) Interceptor(org.apache.ibatis.plugin.Interceptor) JeesuiteMybatisInterceptor(com.mendmix.mybatis.plugin.JeesuiteMybatisInterceptor) GeneralSqlGenerator(com.mendmix.mybatis.crud.GeneralSqlGenerator)

Example 7 with MapperMetadata

use of com.mendmix.mybatis.metadata.MapperMetadata in project jeesuite-libs by vakinge.

the class SqlRewriteHandler method onInterceptor.

@Override
public Object onInterceptor(InvocationVals invocation) throws Throwable {
    if (!invocation.isSelect())
        return null;
    if (invocation.getMappedStatement().getId().endsWith(CrudMethods.selectByPrimaryKey.name())) {
        return null;
    }
    Map<String, String[]> dataMappings = null;
    if (dynaDataPermEnabled) {
        dataMappings = MybatisRuntimeContext.getDataProfileMappings();
    }
    // 
    rewriteSql(invocation, dataMappings);
    if (invocation.getPageParam() != null)
        return null;
    // 不查数据库直接返回
    if (invocation.getSql() == null) {
        List<Object> list = new ArrayList<>(1);
        // 
        MapperMetadata entityInfo = MybatisMapperParser.getMapperMetadata(invocation.getMapperNameSpace());
        String methodName = invocation.getMappedStatement().getId().replace(invocation.getMapperNameSpace(), StringUtils.EMPTY).substring(1);
        Class<?> returnType = entityInfo.getMapperMethod(methodName).getMethod().getReturnType();
        if (returnType == int.class || returnType == Integer.class || returnType == long.class || returnType == Long.class) {
            list.add(0);
        }
        return list;
    } else {
        Executor executor = invocation.getExecutor();
        MappedStatement mappedStatement = invocation.getMappedStatement();
        ResultHandler<?> resultHandler = (ResultHandler<?>) invocation.getArgs()[3];
        List<ParameterMapping> parameterMappings = invocation.getBoundSql().getParameterMappings();
        BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), invocation.getSql(), parameterMappings, invocation.getParameter());
        // 
        copyForeachAdditionlParams(invocation.getBoundSql(), newBoundSql);
        CacheKey cacheKey = executor.createCacheKey(mappedStatement, invocation.getParameter(), RowBounds.DEFAULT, newBoundSql);
        List<?> resultList = executor.query(mappedStatement, invocation.getParameter(), RowBounds.DEFAULT, resultHandler, cacheKey, newBoundSql);
        return resultList;
    }
}
Also used : ArrayList(java.util.ArrayList) ResultHandler(org.apache.ibatis.session.ResultHandler) MapperMetadata(com.mendmix.mybatis.metadata.MapperMetadata) Executor(org.apache.ibatis.executor.Executor) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) BoundSql(org.apache.ibatis.mapping.BoundSql) MappedStatement(org.apache.ibatis.mapping.MappedStatement) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 8 with MapperMetadata

use of com.mendmix.mybatis.metadata.MapperMetadata in project jeesuite-libs by vakinge.

the class SqlRewriteHandler method initColumnConfig.

private void initColumnConfig(List<MapperMetadata> mappers, String column, List<String> mapperNames) {
    if (column == null)
        return;
    List<String> tmpTables = new ArrayList<>();
    ColumnMetadata columnMetadata;
    for (MapperMetadata mapper : mappers) {
        columnMetadata = mapper.getEntityMetadata().getColumns().stream().filter(o -> o.getColumn().equals(column)).findFirst().orElse(null);
        if (columnMetadata == null) {
            continue;
        }
        if (column.equals(softDeleteColumnName)) {
            softDeletePropName = columnMetadata.getProperty();
        }
        tmpTables.add(mapper.getTableName());
        if (!dataPermMappings.containsKey(mapper.getTableName())) {
            dataPermMappings.put(mapper.getTableName(), new LinkedHashMap<>());
        }
        dataPermMappings.get(mapper.getTableName()).put(columnMetadata.getProperty(), column);
    }
    // 
    for (MapperMetadata mapper : mappers) {
        if (tmpTables.contains(mapper.getTableName())) {
            mapperNames.add(mapper.getMapperClass().getName());
        } else {
            Set<String> querys = mapper.getQueryTableMappings().keySet();
            List<String> tables;
            for (String query : querys) {
                tables = mapper.getQueryTableMappings().get(query);
                for (String table : tables) {
                    if (tmpTables.contains(table)) {
                        mapperNames.add(query);
                        break;
                    }
                }
            }
        }
    }
}
Also used : MybatisSqlUtils(com.mendmix.mybatis.kit.MybatisSqlUtils) MybatisConfigs(com.mendmix.mybatis.MybatisConfigs) LoggerFactory(org.slf4j.LoggerFactory) MapperMetadata(com.mendmix.mybatis.metadata.MapperMetadata) StringUtils(org.apache.commons.lang3.StringUtils) SelectBody(net.sf.jsqlparser.statement.select.SelectBody) CrudMethods(com.mendmix.mybatis.crud.CrudMethods) Map(java.util.Map) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) SetOperation(net.sf.jsqlparser.statement.select.SetOperation) MybatisRuntimeContext(com.mendmix.mybatis.MybatisRuntimeContext) CCJSqlParserUtil(net.sf.jsqlparser.parser.CCJSqlParserUtil) Table(net.sf.jsqlparser.schema.Table) AuthUser(com.mendmix.common.model.AuthUser) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) Set(java.util.Set) FromItem(net.sf.jsqlparser.statement.select.FromItem) ResultHandler(org.apache.ibatis.session.ResultHandler) ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) MybatisMapperParser(com.mendmix.mybatis.parser.MybatisMapperParser) Expression(net.sf.jsqlparser.expression.Expression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) List(java.util.List) Select(net.sf.jsqlparser.statement.select.Select) PageParams(com.mendmix.common.model.PageParams) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) MatchPolicy(com.mendmix.common.constants.MatchPolicy) InvocationVals(com.mendmix.mybatis.plugin.InvocationVals) HashMap(java.util.HashMap) RowBounds(org.apache.ibatis.session.RowBounds) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) JSQLParserException(net.sf.jsqlparser.JSQLParserException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) InterceptorHandler(com.mendmix.mybatis.core.InterceptorHandler) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BoundSql(org.apache.ibatis.mapping.BoundSql) UnionOp(net.sf.jsqlparser.statement.select.UnionOp) Column(net.sf.jsqlparser.schema.Column) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) CurrentRuntimeContext(com.mendmix.common.CurrentRuntimeContext) Properties(java.util.Properties) Executor(org.apache.ibatis.executor.Executor) Logger(org.slf4j.Logger) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) Join(net.sf.jsqlparser.statement.select.Join) OrderBy(com.mendmix.common.model.OrderBy) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) OrderType(com.mendmix.common.model.OrderBy.OrderType) SetOperationList(net.sf.jsqlparser.statement.select.SetOperationList) JeesuiteMybatisInterceptor(com.mendmix.mybatis.plugin.JeesuiteMybatisInterceptor) CacheKey(org.apache.ibatis.cache.CacheKey) MappedStatement(org.apache.ibatis.mapping.MappedStatement) Statement(net.sf.jsqlparser.statement.Statement) ResourceUtils(com.mendmix.common.util.ResourceUtils) StringValue(net.sf.jsqlparser.expression.StringValue) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata) ArrayList(java.util.ArrayList) MapperMetadata(com.mendmix.mybatis.metadata.MapperMetadata)

Example 9 with MapperMetadata

use of com.mendmix.mybatis.metadata.MapperMetadata 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 10 with MapperMetadata

use of com.mendmix.mybatis.metadata.MapperMetadata in project jeesuite-libs by vakinge.

the class TableRouteHandler method start.

@Override
public void start(JeesuiteMybatisInterceptor context) {
    List<MapperMetadata> mappers = MybatisMapperParser.getMapperMetadatas(context.getGroupName());
    List<String> tmpTables = new ArrayList<>();
    for (MapperMetadata mapper : mappers) {
        if (!mapper.getTableName().contains(GlobalConstants.PLACEHOLDER_PREFIX))
            continue;
        tmpTables.add(mapper.getTableName());
    }
    for (MapperMetadata mapper : mappers) {
        if (tmpTables.contains(mapper.getTableName())) {
            tableRouteMappedStatements.add(mapper.getMapperClass().getName());
        } else {
            Set<String> querys = mapper.getQueryTableMappings().keySet();
            List<String> tables;
            for (String query : querys) {
                tables = mapper.getQueryTableMappings().get(query);
                for (String table : tables) {
                    if (tmpTables.contains(table)) {
                        tableRouteMappedStatements.add(query);
                        break;
                    }
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MapperMetadata(com.mendmix.mybatis.metadata.MapperMetadata)

Aggregations

MapperMetadata (com.mendmix.mybatis.metadata.MapperMetadata)18 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)6 MappedStatement (org.apache.ibatis.mapping.MappedStatement)6 List (java.util.List)5 ColumnMetadata (com.mendmix.mybatis.metadata.ColumnMetadata)4 JeesuiteMybatisInterceptor (com.mendmix.mybatis.plugin.JeesuiteMybatisInterceptor)4 Map (java.util.Map)4 BoundSql (org.apache.ibatis.mapping.BoundSql)4 CurrentRuntimeContext (com.mendmix.common.CurrentRuntimeContext)3 AuthUser (com.mendmix.common.model.AuthUser)3 OrderBy (com.mendmix.common.model.OrderBy)3 PageParams (com.mendmix.common.model.PageParams)3 ResourceUtils (com.mendmix.common.util.ResourceUtils)3 MybatisConfigs (com.mendmix.mybatis.MybatisConfigs)3 MybatisRuntimeContext (com.mendmix.mybatis.MybatisRuntimeContext)3 InterceptorHandler (com.mendmix.mybatis.core.InterceptorHandler)3 CrudMethods (com.mendmix.mybatis.crud.CrudMethods)3 MybatisSqlUtils (com.mendmix.mybatis.kit.MybatisSqlUtils)3 MapperMethod (com.mendmix.mybatis.metadata.MapperMetadata.MapperMethod)3