Java Code Examples for org.apache.ibatis.executor.Executor#createCacheKey()

The following examples show how to use org.apache.ibatis.executor.Executor#createCacheKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SundialInterceptor.java    From Milkomeda with MIT License 6 votes vote down vote up
private void updateSql(String sql, Invocation invocation, MappedStatement ms, Object[] args, BoundSql boundSql) {
    BoundSql boundSqlNew = new BoundSql(ms.getConfiguration(), sql, boundSql.getParameterMappings(), boundSql.getParameterObject());
    MappedStatement mappedStatement = copyFrom(ms, new BoundSqlSqlSource(boundSqlNew));
    // 替换映射的语句
    args[0] = mappedStatement;

    // 针对查询方式的参数替换
    if (ms.getSqlCommandType() == SqlCommandType.SELECT) {
        Executor executor = (Executor) invocation.getTarget();
        Object parameter = args[1];
        RowBounds rowBounds = (RowBounds) args[2];
        // 6个参数时(因为分页插件的原因导致问题,需要修改对应的类型值)
        if (args.length == 6) {
            args[4] = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
            args[5] = boundSqlNew;
        }
    }
}
 
Example 2
Source File: PaginationHandler.java    From azeroth with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
private Long executeQueryCount(Executor executor, MappedStatement countMs, Object parameter,
                               BoundSql boundSql, RowBounds rowBounds,
                               ResultHandler resultHandler) throws IllegalAccessException,
        SQLException {
    CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT,
            boundSql);

    String orignSql = boundSql.getSql().replaceAll(";$", "");
    // count sql
    String countSql = PageSqlUtils.getCountSql(orignSql);

    BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql,
            boundSql.getParameterMappings(), parameter);
    // 执行 count 查询
    Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT,
            resultHandler, countKey, countBoundSql);
    Long count = (Long) ((List) countResultList).get(0);
    return count;
}
 
Example 3
Source File: PaginationHandler.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
	private Long executeQueryCount(Executor executor, MappedStatement countMs,
            Object parameter, BoundSql boundSql,
			RowBounds rowBounds, ResultHandler resultHandler) throws IllegalAccessException, SQLException {
		CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);
		
		String orignSql = StringUtils.replace(boundSql.getSql(), ";$", StringUtils.EMPTY);
		// count sql
		String countSql = PageSqlUtils.getCountSql(orignSql);
		
		BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql, boundSql.getParameterMappings(),
				parameter);
//		for (ParameterMapping parameterMapping : boundSql.getParameterMappings()) {
//			String propertyName = parameterMapping.getProperty();
//			if(boundSql.hasAdditionalParameter(propertyName)){
//				countBoundSql.setAdditionalParameter(propertyName, boundSql.getAdditionalParameter(propertyName));
//			}
//		}
		// 执行 count 查询
		Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey,
				countBoundSql);
		Long count = (Long) ((List) countResultList).get(0);
		return count;
	}
 
Example 4
Source File: PaginationHandler.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
private List executeQuery(Executor executor, MappedStatement ms,
           Object parameter, BoundSql boundSql,
		RowBounds rowBounds, ResultHandler resultHandler,PageParams pageParams) throws IllegalAccessException, SQLException {
	CacheKey countKey = executor.createCacheKey(ms, parameter, RowBounds.DEFAULT, boundSql);
	
	String orignSql = StringUtils.replace(boundSql.getSql(), ";$", StringUtils.EMPTY);
	
	String pageSql = PageSqlUtils.getLimitSQL(dbType,orignSql,pageParams);
	
	BoundSql countBoundSql = new BoundSql(ms.getConfiguration(), pageSql, boundSql.getParameterMappings(),
			parameter);
	
	List<?> resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, countKey,
			countBoundSql);
	return resultList;
}
 
Example 5
Source File: SortListInterceptor.java    From QuickProject with Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
       List<Sort> sortList = getSortList();
       if (sortList == null || sortList.size() == 0) {
           return invocation.proceed();
       }

       Executor executor = (Executor) invocation.getTarget();
       Object[] args = invocation.getArgs();
       MappedStatement ms = (MappedStatement) args[0];
       Object parameter = args[1];
       RowBounds rowBounds = (RowBounds) args[2];
       ResultHandler resultHandler = (ResultHandler) args[3];

       // 计算修改BoundSql
       BoundSql boundSql = ms.getBoundSql(parameter);
       MetaObject boundSqlHandler = MetaObject.forObject(boundSql, new DefaultObjectFactory(), new DefaultObjectWrapperFactory());
       Dialect dialect = DialectParser.parse(ms.getConfiguration());
       String sql = (String) boundSqlHandler.getValue("sql");
       sql = dialect.addSortString(sql, sortList);
       boundSqlHandler.setValue("sql", sql);

       // 继续执行原来的代码
       CacheKey key = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
       return executor.query(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
 
Example 6
Source File: ExecutorUtil.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
/**
 * 执行自动生成的 count 查询
 *
 * @param dialect
 * @param executor
 * @param countMs
 * @param parameter
 * @param boundSql
 * @param rowBounds
 * @param resultHandler
 * @return
 * @throws SQLException
 */
public static Long executeAutoCount(Dialect dialect, Executor executor, MappedStatement countMs,
                                    Object parameter, BoundSql boundSql,
                                    RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
    Map<String, Object> additionalParameters = getAdditionalParameter(boundSql);
    //创建 count 查询的缓存 key
    CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);
    //调用方言获取 count sql
    String countSql = dialect.getCountSql(countMs, boundSql, parameter, rowBounds, countKey);
    //countKey.update(countSql);
    BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter);
    //当使用动态 SQL 时,可能会产生临时的参数,这些参数需要手动设置到新的 BoundSql 中
    for (String key : additionalParameters.keySet()) {
        countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
    }
    //执行 count 查询
    Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);
    Long count = (Long) ((List) countResultList).get(0);
    return count;
}
 
Example 7
Source File: PaginationHandler.java    From sqlhelper with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Object executeOrderBy(OrderBy orderBy, final MappedStatement ms, final Object parameter, final RowBounds rowBounds, final ResultHandler resultHandler, final Executor executor, final BoundSql boundSql) throws Throwable {
    SQLStatementInstrumentor instrumentor = SqlHelperMybatisPlugin.getInstrumentor();
    String orderBySqlId = getOrderById(ms, orderBy);
    MappedStatement orderByStatement = this.customOrderByStatement(ms, orderBySqlId);
    final CacheKey orderByCacheKey = executor.createCacheKey(orderByStatement, parameter, RowBounds.DEFAULT, boundSql);
    final String orderBySql = instrumentor.instrumentOrderBySql(boundSql.getSql(), orderBy);
    BoundSql orderByBoundSql = MybatisUtils.rebuildBoundSql(orderBySql, orderByStatement.getConfiguration(), boundSql);
    return executor.query(orderByStatement, parameter, RowBounds.DEFAULT, resultHandler, orderByCacheKey, orderByBoundSql);
}
 
Example 8
Source File: PaginationHandler.java    From sqlhelper with GNU Lesser General Public License v3.0 4 votes vote down vote up
private int executeCount(final MappedStatement ms, final Object parameter, final RowBounds rowBounds, final ResultHandler resultHandler, final Executor executor, final BoundSql boundSql) throws Throwable {
    final PagingRequestContext requestContext = PAGING_CONTEXT.get();
    final PagingRequest request = PAGING_CONTEXT.getPagingRequest();
    final String countStatementId = this.getCountStatementId(request, ms.getId());
    int count;
    BoundSql countBoundSql = null;
    try {
        MappedStatement countStatement = this.extractCountStatementFromConfiguration(ms.getConfiguration(), countStatementId);
        if (countStatement != null) {
            final CacheKey countKey = executor.createCacheKey(countStatement, parameter, RowBounds.DEFAULT, boundSql);
            countKey.update(request.getPageNo());
            countKey.update(request.getPageSize());
            countBoundSql = countStatement.getBoundSql(parameter);
            requestContext.set(MybatisSqlRequestContextKeys.COUNT_SQL, countBoundSql);
            final Object countResultList = executor.query(countStatement, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);
            count = ((Number) ((List) countResultList).get(0)).intValue();
        } else {
            String querySql = boundSql.getSql();
            SQLStatementInstrumentor instrumentor = SqlHelperMybatisPlugin.getInstrumentor();
            final String countSql = instrumentor.countSql(querySql, request.getCountColumn());
            countStatement = this.customCountStatement(ms, countStatementId, querySql, request);

            final CacheKey countKey2 = executor.createCacheKey(countStatement, parameter, RowBounds.DEFAULT, boundSql);
            countKey2.update(request.getPageNo());
            countKey2.update(request.getPageSize());

            countBoundSql = MybatisUtils.rebuildBoundSql(countSql, countStatement.getConfiguration(), boundSql);
            requestContext.set(MybatisSqlRequestContextKeys.COUNT_SQL, countBoundSql);
            final Object countResultList2 = executor.query(countStatement, parameter, RowBounds.DEFAULT, resultHandler, countKey2, countBoundSql);
            count = ((Number) ((List) countResultList2).get(0)).intValue();
        }
    } catch (Throwable ex) {
        if (countBoundSql != null) {
            logger.error("error occur when execute count sql [{}], error: {}", countBoundSql.getSql(), ex.getMessage(), ex);
        }
        throw ex;
    } finally {
        requestContext.set(MybatisSqlRequestContextKeys.COUNT_SQL, null);
    }
    return count;
}
 
Example 9
Source File: OffsetLimitInterceptor.java    From AsuraFramework with Apache License 2.0 4 votes vote down vote up
public Object intercept(final Invocation invocation) throws Throwable {
       final Executor executor = (Executor) invocation.getTarget();
       final Object[] queryArgs = invocation.getArgs();
       final MappedStatement ms = (MappedStatement)queryArgs[MAPPED_STATEMENT_INDEX];
       final Object parameter = queryArgs[PARAMETER_INDEX];
       final RowBounds rowBounds = (RowBounds)queryArgs[ROWBOUNDS_INDEX];
       final PageBounds pageBounds = new PageBounds(rowBounds);

       if(pageBounds.getOffset() == RowBounds.NO_ROW_OFFSET
               && pageBounds.getLimit() == RowBounds.NO_ROW_LIMIT
               && pageBounds.getOrders().isEmpty()){
           return invocation.proceed();
       }

       final Dialect dialect;
       try {
           Class clazz = Class.forName(dialectClass);
           Constructor constructor = clazz.getConstructor(MappedStatement.class, Object.class, PageBounds.class);
           dialect = (Dialect)constructor.newInstance(new Object[]{ms, parameter, pageBounds});
       } catch (Exception e) {
           throw new ClassNotFoundException("Cannot create dialect instance: "+dialectClass,e);
       }

       final BoundSql boundSql = ms.getBoundSql(parameter);

       queryArgs[MAPPED_STATEMENT_INDEX] = copyFromNewSql(ms,boundSql,dialect.getPageSQL(), dialect.getParameterMappings(), dialect.getParameterObject());
       queryArgs[PARAMETER_INDEX] = dialect.getParameterObject();
       queryArgs[ROWBOUNDS_INDEX] = new RowBounds(RowBounds.NO_ROW_OFFSET,RowBounds.NO_ROW_LIMIT);

       Boolean async = pageBounds.getAsyncTotalCount() == null ? asyncTotalCount : pageBounds.getAsyncTotalCount();
       Future<List> listFuture = call(new Callable<List>() {
           public List call() throws Exception {
               return (List)invocation.proceed();
           }
       }, async);


       if(pageBounds.isContainsTotalCount()){
           Callable<Paginator> countTask = new Callable() {
               public Object call() throws Exception {
                   Integer count;
                   Cache cache = ms.getCache();
                   if(cache != null && ms.isUseCache() && ms.getConfiguration().isCacheEnabled()){
                       CacheKey cacheKey = executor.createCacheKey(ms,parameter,new PageBounds(),copyFromBoundSql(ms,boundSql,dialect.getCountSQL(), boundSql.getParameterMappings(), boundSql.getParameterObject()));
                       count = (Integer)cache.getObject(cacheKey);
                       if(count == null){
                           count = SQLHelp.getCount(ms,executor.getTransaction(),parameter,boundSql,dialect);
                           cache.putObject(cacheKey, count);
                       }
                   }else{
                       count = SQLHelp.getCount(ms,executor.getTransaction(),parameter,boundSql,dialect);
                   }
                   return new Paginator(pageBounds.getPage(), pageBounds.getLimit(), count);
               }
           };
           Future<Paginator> countFutrue = call(countTask, async);
           return new PageList(listFuture.get(),countFutrue.get());
       }

       return listFuture.get();
}
 
Example 10
Source File: ExecutorUtil.java    From Mybatis-PageHelper with MIT License 3 votes vote down vote up
/**
 * 执行手动设置的 count 查询,该查询支持的参数必须和被分页的方法相同
 *
 * @param executor
 * @param countMs
 * @param parameter
 * @param boundSql
 * @param resultHandler
 * @return
 * @throws SQLException
 */
public static Long executeManualCount(Executor executor, MappedStatement countMs,
                                      Object parameter, BoundSql boundSql,
                                      ResultHandler resultHandler) throws SQLException {
    CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);
    BoundSql countBoundSql = countMs.getBoundSql(parameter);
    Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);
    Long count = ((Number) ((List) countResultList).get(0)).longValue();
    return count;
}