Search in sources :

Example 1 with JeesuiteMybatisInterceptor

use of com.mendmix.mybatis.plugin.JeesuiteMybatisInterceptor 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)

Example 2 with JeesuiteMybatisInterceptor

use of com.mendmix.mybatis.plugin.JeesuiteMybatisInterceptor 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)

Aggregations

MapperMetadata (com.mendmix.mybatis.metadata.MapperMetadata)2 JeesuiteMybatisInterceptor (com.mendmix.mybatis.plugin.JeesuiteMybatisInterceptor)2 CurrentRuntimeContext (com.mendmix.common.CurrentRuntimeContext)1 JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)1 MatchPolicy (com.mendmix.common.constants.MatchPolicy)1 AuthUser (com.mendmix.common.model.AuthUser)1 OrderBy (com.mendmix.common.model.OrderBy)1 OrderType (com.mendmix.common.model.OrderBy.OrderType)1 PageParams (com.mendmix.common.model.PageParams)1 ResourceUtils (com.mendmix.common.util.ResourceUtils)1 MybatisConfigs (com.mendmix.mybatis.MybatisConfigs)1 MybatisRuntimeContext (com.mendmix.mybatis.MybatisRuntimeContext)1 InterceptorHandler (com.mendmix.mybatis.core.InterceptorHandler)1 CrudMethods (com.mendmix.mybatis.crud.CrudMethods)1 GeneralSqlGenerator (com.mendmix.mybatis.crud.GeneralSqlGenerator)1 MybatisSqlUtils (com.mendmix.mybatis.kit.MybatisSqlUtils)1 ColumnMetadata (com.mendmix.mybatis.metadata.ColumnMetadata)1 MybatisMapperParser (com.mendmix.mybatis.parser.MybatisMapperParser)1 InvocationVals (com.mendmix.mybatis.plugin.InvocationVals)1 Method (java.lang.reflect.Method)1