Search in sources :

Example 6 with ColumnMetadata

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

the class CacheHandler method onFinished.

@SuppressWarnings("rawtypes")
@Override
public void onFinished(InvocationVals invocationVal, Object result) {
    try {
        if (BLOCK_ON_CONCURRENT_LOCK_RETURN.equals(result))
            return;
        MappedStatement mt = invocationVal.getMappedStatement();
        QueryCacheMethodMetadata metadata = null;
        if (mt.getSqlCommandType().equals(SqlCommandType.SELECT)) {
            if (result == null)
                return;
            if ((metadata = invocationVal.getQueryMethodMetadata()) == null)
                return;
            final String cacheKey = invocationVal.getCacheKey();
            if (result instanceof List) {
                List list = (List) result;
                if (list.isEmpty()) {
                    if (nullValueCache) {
                        CacheUtils.set(cacheKey, NULL_PLACEHOLDER, IN_1MINS);
                    }
                    return;
                }
                result = metadata.collectionResult ? result : list.get(0);
            }
            // 
            if (!metadata.isSecondQueryById()) {
                CacheUtils.set(cacheKey, result, metadata.getExpire());
                if (logger.isDebugEnabled())
                    logger.debug(">>auto_cache_process addCache -> mapperId:{},cacheKey:{}", mt.getId(), cacheKey);
                if (metadata.isPk) {
                    // 唯一索引(业务上)
                    cacheUniqueSelectRef(invocationVal, result, mt, cacheKey);
                } else if (metadata.groupRalated) {
                    // 结果为集合的情况,增加key到cacheGroup
                    CacheUtils.addListItems(metadata.cacheGroupKey, cacheKey);
                }
            } else {
                // 之前没有按主键的缓存,增加按主键缓存
                String idCacheKey = genarateQueryCacheKey(invocationVal, getQueryByPkMethodCache(mt.getId()).keyPattern, result);
                if (idCacheKey != null && cacheKey != null) {
                    if (!CacheUtils.exists(idCacheKey)) {
                        // 缓存idkey->实体
                        CacheUtils.set(idCacheKey, result, metadata.getExpire());
                    }
                    // 缓存fieldkey->idkey
                    cacheFieldRefKey(cacheKey, idCacheKey, metadata.getExpire());
                    if (logger.isDebugEnabled())
                        logger.debug(">>auto_cache_process addCache -> mapperId:{},idCacheKey:{},cacheKey:{}", mt.getId(), idCacheKey, cacheKey);
                }
            }
        } else {
            String mapperClassName = invocationVal.getMapperNameSpace();
            if (!cacheEnableMappers.contains(mapperClassName) && !customUpdateCacheMapppings.containsKey(mt.getId()))
                return;
            // 返回0,未更新成功
            if (result != null && ((int) result) == 0)
                return;
            // 更新方法移除缓存,避免事务回滚导致缓存不一致,所以更新方法直接移除缓存
            if (mt.getSqlCommandType().equals(SqlCommandType.INSERT)) {
            // TODO 写入缓存 ,考虑回滚
            } else {
                if (updatePkCacheMethods.containsKey(mt.getId())) {
                    UpdateByPkCacheMethodMetadata updateMethodCache = updatePkCacheMethods.get(mt.getId());
                    String idCacheKey = genarateQueryCacheKey(invocationVal, updateMethodCache.keyPattern, invocationVal.getParameter());
                    CacheUtils.remove(idCacheKey);
                } else {
                    // 针对按条件更新或者删除的方法,按查询条件查询相关内容,然后清理对应主键缓存内容
                    MapperMetadata mapperMeta = MybatisMapperParser.getMapperMetadata(mapperClassName);
                    final Object parameter = invocationVal.getArgs()[1];
                    BoundSql boundSql = mt.getBoundSql(parameter);
                    String orignSql = boundSql.getSql();
                    ColumnMetadata idColumn = mapperMeta.getEntityMetadata().getIdColumn();
                    SqlMetadata sqlMetadata = MybatisSqlUtils.rewriteAsSelectPkField(orignSql, idColumn.getColumn());
                    // 
                    String tenantId = CurrentRuntimeContext.getTenantId();
                    cleanCacheExecutor.execute(new Runnable() {

                        @Override
                        public void run() {
                            if (tenantId != null) {
                                CurrentRuntimeContext.setTenantId(tenantId);
                            }
                            removeCacheByDyncQuery(mapperMeta, boundSql, sqlMetadata);
                        }
                    });
                }
            }
            // 删除同一cachegroup关联缓存
            removeCacheByGroup(mt.getId(), mapperClassName);
            // 删除自定义关联缓存
            if (customUpdateCacheMapppings.containsKey(mt.getId())) {
                removeCustomRelateCache(mt.getId());
            }
        }
    } finally {
        // 清除并发控制锁
        if (invocationVal.getConcurrentLockKey() != null) {
            CacheUtils.remove(invocationVal.getConcurrentLockKey());
        }
    }
}
Also used : ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata) BoundSql(org.apache.ibatis.mapping.BoundSql) List(java.util.List) ArrayList(java.util.ArrayList) SqlMetadata(com.mendmix.mybatis.metadata.SqlMetadata) MappedStatement(org.apache.ibatis.mapping.MappedStatement) MapperMetadata(com.mendmix.mybatis.metadata.MapperMetadata)

Example 7 with ColumnMetadata

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

the class DeleteByPrimaryKeyBuilder method buildSQL.

@Override
String buildSQL(EntityMetadata entityMapper, boolean selective) {
    // 从表注解里获取表名等信息
    TableMetadata tableMapper = entityMapper.getTable();
    ColumnMetadata idColumn = entityMapper.getIdColumn();
    SQL sql = new SQL().DELETE_FROM(tableMapper.getName()).WHERE(idColumn.getColumn() + "=#{" + idColumn.getProperty() + "}");
    return sql.toString();
}
Also used : TableMetadata(com.mendmix.mybatis.metadata.TableMetadata) ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata) SQL(org.apache.ibatis.jdbc.SQL)

Example 8 with ColumnMetadata

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

the class InsertBuilder method buildSQL.

@Override
String buildSQL(EntityMetadata entityMapper, boolean selective) {
    // 从表注解里获取表名等信息
    TableMetadata table = entityMapper.getTable();
    Set<ColumnMetadata> columns = entityMapper.getColumns();
    StringBuilder fieldBuilder = new StringBuilder(SqlTemplate.TRIM_PREFIX);
    StringBuilder prppertyBuilder = new StringBuilder(SqlTemplate.TRIM_PREFIX);
    if (!entityMapper.autoId()) {
        /* 用户输入自定义ID */
        fieldBuilder.append(entityMapper.getIdColumn().getColumn()).append(",");
        prppertyBuilder.append("#{").append(entityMapper.getIdColumn().getProperty()).append("},");
    }
    for (ColumnMetadata column : columns) {
        if (column.isId() || !column.isInsertable()) {
            continue;
        }
        String fieldExpr = SqlTemplate.wrapIfTag(column.getProperty(), column.getColumn(), !selective);
        String propertyExpr = SqlTemplate.wrapIfTag(column.getProperty(), "#{" + column.getProperty() + "}", !selective);
        fieldBuilder.append(fieldExpr);
        fieldBuilder.append(selective ? "\n" : ",");
        prppertyBuilder.append(propertyExpr);
        prppertyBuilder.append(selective ? "\n" : ",");
    }
    if (!selective) {
        fieldBuilder.deleteCharAt(fieldBuilder.length() - 1);
        prppertyBuilder.deleteCharAt(prppertyBuilder.length() - 1);
    }
    fieldBuilder.append(SqlTemplate.TRIM_SUFFIX);
    prppertyBuilder.append(SqlTemplate.TRIM_SUFFIX);
    String sql = String.format(SqlTemplate.INSERT, table.getName(), fieldBuilder.toString(), prppertyBuilder.toString());
    return sql;
}
Also used : TableMetadata(com.mendmix.mybatis.metadata.TableMetadata) ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata)

Example 9 with ColumnMetadata

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

the class InsertListBuilder method buildSQL.

@Override
String buildSQL(EntityMetadata entityMapper, boolean selective) {
    // 从表注解里获取表名等信息
    TableMetadata table = entityMapper.getTable();
    Set<ColumnMetadata> columns = entityMapper.getColumns();
    StringBuilder fieldBuilder = new StringBuilder("(");
    StringBuilder prppertyBuilder = new StringBuilder("(");
    if (!entityMapper.autoId()) {
        fieldBuilder.append(entityMapper.getIdColumn().getColumn()).append(",");
        prppertyBuilder.append("#{item.").append(entityMapper.getIdColumn().getProperty()).append("},");
    }
    for (ColumnMetadata column : columns) {
        if (column.isId() || !column.isInsertable()) {
            continue;
        }
        String fieldExpr = SqlTemplate.wrapIfTag(column.getProperty(), column.getColumn(), true);
        String propertyExpr = SqlTemplate.wrapIfTag(column.getProperty(), "#{item." + column.getProperty() + "}", true);
        fieldBuilder.append(fieldExpr);
        fieldBuilder.append(",");
        prppertyBuilder.append(propertyExpr);
        prppertyBuilder.append(",");
    }
    fieldBuilder.deleteCharAt(fieldBuilder.length() - 1);
    prppertyBuilder.deleteCharAt(prppertyBuilder.length() - 1);
    fieldBuilder.append(")");
    prppertyBuilder.append(")");
    String sql = String.format(SqlTemplate.BATCH_INSERT, table.getName(), fieldBuilder.toString(), prppertyBuilder.toString());
    return sql;
}
Also used : TableMetadata(com.mendmix.mybatis.metadata.TableMetadata) ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata)

Example 10 with ColumnMetadata

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

the class SelectByPrimaryKeyBuilder method buildSQL.

@Override
String buildSQL(EntityMetadata entityMapper, boolean selective) {
    // 从表注解里获取表名等信息
    TableMetadata tableMapper = entityMapper.getTable();
    ColumnMetadata idColumn = entityMapper.getIdColumn();
    return new SQL().SELECT("*").FROM(tableMapper.getName()).WHERE(idColumn.getColumn() + "=#{" + idColumn.getProperty() + "}").toString();
}
Also used : TableMetadata(com.mendmix.mybatis.metadata.TableMetadata) ColumnMetadata(com.mendmix.mybatis.metadata.ColumnMetadata) SQL(org.apache.ibatis.jdbc.SQL)

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