Search in sources :

Example 1 with MapperMetadata

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

the class CacheHandler method removeCacheByGroup.

/**
 * 删除缓存组
 * @param msId
 * @param mapperClassName
 * @param removePkCache  是否同时删除按主键的缓存
 */
private void removeCacheByGroup(String msId, String mapperClassName) {
    MapperMetadata entityInfo = MybatisMapperParser.getMapperMetadata(mapperClassName);
    if (entityInfo == null)
        return;
    final String groupName = entityInfo.getEntityClass().getSimpleName();
    cleanCacheExecutor.execute(new Runnable() {

        @Override
        public void run() {
            clearCacheGroup(groupName);
            if (logger.isDebugEnabled())
                logger.debug(">>auto_cache_process removeGroupCache -> mapperId:{},groupName:{}", msId, groupName);
        }
    });
}
Also used : MapperMetadata(com.mendmix.mybatis.metadata.MapperMetadata)

Example 2 with MapperMetadata

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

the class CacheHandler method buildEvictOnMethods.

/**
 * 构建自定义缓存更新关系
 * @param mapperClassName
 * @param method
 * @param evictOnMethods
 */
private void buildEvictOnMethods(String mapperClassName, MapperMethod method, String[] evictOnMethods) {
    if (evictOnMethods == null || evictOnMethods.length == 0) {
        return;
    }
    String targetMethodFullNamePrefix = mapperClassName.substring(0, mapperClassName.lastIndexOf(".") + 1);
    String targetMapperClassName = null;
    for (String methodName : evictOnMethods) {
        if ("*".equals(methodName)) {
            methodName = mapperClassName + ".*";
        } else if (!methodName.contains(InvocationVals.DOT)) {
            methodName = mapperClassName + InvocationVals.DOT + methodName;
        }
        if (!methodName.startsWith(targetMethodFullNamePrefix)) {
            methodName = targetMethodFullNamePrefix + methodName;
        }
        targetMapperClassName = methodName.substring(0, methodName.lastIndexOf("."));
        if (!methodName.endsWith("*")) {
            addCacheCheckRelations(methodName, method.getFullName());
        } else {
            MapperMetadata methodEntityInfo = MybatisMapperParser.getMapperMetadata(targetMapperClassName);
            if (methodEntityInfo == null) {
                continue;
            }
            for (MapperMethod mm : methodEntityInfo.getMapperMethods().values()) {
                if (mm.getSqlType() == SqlCommandType.SELECT)
                    continue;
                if (mm.getFullName().contains(methodName.replace("*", ""))) {
                    addCacheCheckRelations(mm.getFullName(), method.getFullName());
                }
            }
        }
    }
}
Also used : MapperMethod(com.mendmix.mybatis.metadata.MapperMetadata.MapperMethod) MapperMetadata(com.mendmix.mybatis.metadata.MapperMetadata)

Example 3 with MapperMetadata

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

the class DataChangeLogHandler method set.

public static void set(Class<? extends BaseEntity>[] entityClass) {
    List<String> names = new ArrayList<String>(entityClass.length);
    for (Class<? extends BaseEntity> clazz : entityClass) {
        MapperMetadata entityInfo = MybatisMapperParser.getMapperMetadata(clazz.getName());
        names.add(entityInfo.getMapperClass().getName());
    }
    entityClassNameHolder.set(names);
}
Also used : ArrayList(java.util.ArrayList) MapperMetadata(com.mendmix.mybatis.metadata.MapperMetadata)

Example 4 with MapperMetadata

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

the class DataChangeLogHandler method onFinished.

@Override
public void onFinished(InvocationVals invocation, Object result) {
    if (entityClassNameHolder.get() == null || entityClassNameHolder.get().isEmpty())
        return;
    Object[] args = invocation.getArgs();
    MappedStatement mt = (MappedStatement) args[0];
    String mapperClassName = mt.getId().substring(0, mt.getId().lastIndexOf("."));
    if (!entityClassNameHolder.get().contains(mapperClassName))
        return;
    MapperMetadata entityInfo = MybatisMapperParser.getMapperMetadata(mapperClassName);
    if (entityInfo == null)
        return;
    try {
        System.out.println(mt.getId());
        if (mt.getId().endsWith(methodNames[0]) || mt.getId().endsWith(methodNames[1])) {
        // BehaviorLogContextHoler.onAddEntity(entityInfo.getEntityClass(), ((BaseEntity)args[1]).getId());
        } else if (mt.getId().endsWith(methodNames[2])) {
            if (result != null && !((List) result).isEmpty()) {
            // BehaviorLogContextHoler.onUpdateEntityBefore((BaseEntity)((List)result).get(0));
            }
        } else if (mt.getId().endsWith(methodNames[3])) {
        // BehaviorLogContextHoler.onDeleteEntity(entityInfo.getEntityClass(), args[1]);
        } else if (mt.getId().endsWith(methodNames[4]) || mt.getId().endsWith(methodNames[5])) {
        // BehaviorLogContextHoler.onUpdateEntityAfter((BaseEntity)args[1]);
        }
    } catch (Exception e) {
        logger.warn("datachange_interceptor_error for[" + mt.getId() + "]", e);
    }
}
Also used : MappedStatement(org.apache.ibatis.mapping.MappedStatement) MapperMetadata(com.mendmix.mybatis.metadata.MapperMetadata)

Example 5 with MapperMetadata

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

the class SqlRewriteHandler method start.

@Override
public void start(JeesuiteMybatisInterceptor context) {
    isFieldSharddingTenant = MybatisConfigs.isFieldSharddingTenant(context.getGroupName());
    softDeleteColumnName = MybatisConfigs.getSoftDeleteColumn(context.getGroupName());
    softDeleteFalseValue = MybatisConfigs.getSoftDeletedFalseValue(context.getGroupName());
    deptColumnName = MybatisConfigs.getDeptColumnName(context.getGroupName());
    ownerColumnName = MybatisConfigs.getOwnerColumnName(context.getGroupName());
    orgBasePermKey = MybatisConfigs.getCurrentOrgPermKey(context.getGroupName());
    Properties properties = ResourceUtils.getAllProperties("mendmix.mybatis.permission.table-column-mappings");
    properties.forEach((k, v) -> {
        String tableName = k.toString().substring(k.toString().indexOf("[") + 1).replace("]", "").trim();
        buildTableDataPermissionMapping(tableName, v.toString());
    });
    dynaDataPermEnabled = !dataPermMappings.isEmpty();
    final List<MapperMetadata> mappers = MybatisMapperParser.getMapperMetadatas(context.getGroupName());
    // 
    initColumnConfig(mappers, deptColumnName, deptMappedStatements);
    // 软删除
    initColumnConfig(mappers, softDeleteColumnName, softDeleteMappedStatements);
    // 字段隔离租户模式
    if (isFieldSharddingTenant) {
        String tenantField = MybatisConfigs.getTenantSharddingField(context.getGroupName());
        ColumnMetadata tenantColumn;
        for (MapperMetadata mapper : mappers) {
            tenantColumn = mapper.getEntityMetadata().getColumns().stream().filter(o -> {
                return o.getColumn().equals(tenantField) || o.getProperty().equals(tenantField);
            }).findFirst().orElse(null);
            if (tenantColumn == null)
                continue;
            if (tenantColumnName == null)
                tenantColumnName = tenantColumn.getColumn();
            if (tenantPropName == null)
                tenantPropName = tenantColumn.getProperty();
            if (!dataPermMappings.containsKey(mapper.getTableName())) {
                dataPermMappings.put(mapper.getTableName(), new LinkedHashMap<>());
            }
            dataPermMappings.get(mapper.getTableName()).put(tenantPropName, tenantColumnName);
        }
    }
    logger.info("dataProfileMappings >> {}", dataPermMappings);
}
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) Properties(java.util.Properties) 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