Search in sources :

Example 1 with ColumnMetadata

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

the class AbstractSelectMethodBuilder method getResultMap.

/**
 * 生成当前实体的resultMap对象
 *
 * @param configuration
 * @return
 */
public static ResultMap getResultMap(Configuration configuration, Class<?> entityClass) {
    List<ResultMapping> resultMappings = new ArrayList<ResultMapping>();
    Set<ColumnMetadata> entityClassColumns = MetadataHelper.getEntityMapper(entityClass).getColumns();
    for (ColumnMetadata entityColumn : entityClassColumns) {
        ResultMapping.Builder builder = new ResultMapping.Builder(configuration, entityColumn.getProperty(), entityColumn.getColumn(), entityColumn.getJavaType());
        if (entityColumn.getJdbcType() != null) {
            builder.jdbcType(entityColumn.getJdbcType());
        }
        List<ResultFlag> flags = new ArrayList<ResultFlag>();
        if (entityColumn.isId()) {
            flags.add(ResultFlag.ID);
        }
        builder.flags(flags);
        builder.lazy(false);
        resultMappings.add(builder.build());
    }
    ResultMap.Builder builder = new ResultMap.Builder(configuration, "BaseResultMap", entityClass, resultMappings, true);
    return builder.build();
}
Also used : ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata) ResultMap(org.apache.ibatis.mapping.ResultMap) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) ResultFlag(org.apache.ibatis.mapping.ResultFlag)

Example 2 with ColumnMetadata

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

the class UpdateWithVersionProvider method updateByPrimaryKeyWithVersion.

public String updateByPrimaryKeyWithVersion(Object example) throws Exception {
    EntityMetadata entityMapper = MetadataHelper.getEntityMapper(example.getClass());
    Set<ColumnMetadata> columns = entityMapper.getColumns();
    SQL sql = new SQL().UPDATE(entityMapper.getTable().getName());
    StringBuilder setBuilder = new StringBuilder();
    StringBuilder whereBuilder = new StringBuilder();
    // 主键
    ColumnMetadata idColumn = entityMapper.getIdColumn();
    appendWhere(whereBuilder, idColumn);
    Object value;
    for (ColumnMetadata column : columns) {
        if (column.isId() || !column.isUpdatable())
            continue;
        value = MetadataHelper.getEntityField(entityMapper.getTable().getName(), column.getProperty()).get(example);
        if (value == null)
            continue;
        if (column.isVersionField()) {
            appendWhere(whereBuilder, column);
            // 乐观锁新值
            value = getNewVersionVal(column.getJavaType(), value);
            appendUpdateSet(setBuilder, column, value);
        } else {
            appendUpdateSet(setBuilder, column);
        }
    }
    sql.SET(setBuilder.toString());
    sql.WHERE(whereBuilder.toString());
    return sql.toString();
}
Also used : ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata) EntityMetadata(com.mendmix.mybatis.metadata.EntityMetadata) SQL(org.apache.ibatis.jdbc.SQL)

Example 3 with ColumnMetadata

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

the class CountByExampleProvider method countByExample.

public String countByExample(Object example) throws Exception {
    EntityMetadata entityMapper = MetadataHelper.getEntityMapper(example.getClass());
    Set<ColumnMetadata> columns = entityMapper.getColumns();
    SQL sql = new SQL().SELECT("COUNT(1)").FROM(entityMapper.getTable().getName());
    Object value;
    StringBuilder whereBuilder = new StringBuilder();
    for (ColumnMetadata column : columns) {
        value = MetadataHelper.getEntityField(entityMapper.getTable().getName(), column.getProperty()).get(example);
        if (value == null)
            continue;
        appendWhere(whereBuilder, column);
    }
    if (whereBuilder.length() == 0)
        throw new IllegalArgumentException("至少包含一个查询条件");
    sql.WHERE(whereBuilder.toString());
    return sql.toString();
}
Also used : ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata) EntityMetadata(com.mendmix.mybatis.metadata.EntityMetadata) SQL(org.apache.ibatis.jdbc.SQL)

Example 4 with ColumnMetadata

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

use of com.mendmix.mybatis.metadata.ColumnMetadata 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)

Aggregations

ColumnMetadata (com.mendmix.mybatis.metadata.ColumnMetadata)13 TableMetadata (com.mendmix.mybatis.metadata.TableMetadata)6 SQL (org.apache.ibatis.jdbc.SQL)5 ArrayList (java.util.ArrayList)4 EntityMetadata (com.mendmix.mybatis.metadata.EntityMetadata)3 MapperMetadata (com.mendmix.mybatis.metadata.MapperMetadata)3 List (java.util.List)3 CurrentRuntimeContext (com.mendmix.common.CurrentRuntimeContext)2 JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)2 MatchPolicy (com.mendmix.common.constants.MatchPolicy)2 AuthUser (com.mendmix.common.model.AuthUser)2 OrderBy (com.mendmix.common.model.OrderBy)2 OrderType (com.mendmix.common.model.OrderBy.OrderType)2 PageParams (com.mendmix.common.model.PageParams)2 ResourceUtils (com.mendmix.common.util.ResourceUtils)2 MybatisConfigs (com.mendmix.mybatis.MybatisConfigs)2 MybatisRuntimeContext (com.mendmix.mybatis.MybatisRuntimeContext)2 InterceptorHandler (com.mendmix.mybatis.core.InterceptorHandler)2 CrudMethods (com.mendmix.mybatis.crud.CrudMethods)2 MybatisSqlUtils (com.mendmix.mybatis.kit.MybatisSqlUtils)2