Java Code Examples for org.apache.ibatis.executor.Executor#query()
The following examples show how to use
org.apache.ibatis.executor.Executor#query() .
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: PaginationHandler.java From azeroth with Apache License 2.0 | 6 votes |
@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 2
Source File: PaginationHandler.java From jeesuite-libs with Apache License 2.0 | 6 votes |
@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 3
Source File: PaginationHandler.java From jeesuite-libs with Apache License 2.0 | 6 votes |
@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 4
Source File: SortListInterceptor.java From QuickProject with Apache License 2.0 | 6 votes |
@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 5
Source File: ExecutorUtil.java From Mybatis-PageHelper with MIT License | 6 votes |
/** * 执行自动生成的 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 6
Source File: ExecutorUtil.java From Mybatis-PageHelper with MIT License | 6 votes |
/** * 分页查询 * * @param dialect * @param executor * @param ms * @param parameter * @param rowBounds * @param resultHandler * @param boundSql * @param cacheKey * @param <E> * @return * @throws SQLException */ public static <E> List<E> pageQuery(Dialect dialect, Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql, CacheKey cacheKey) throws SQLException { //判断是否需要进行分页查询 if (dialect.beforePage(ms, parameter, rowBounds)) { //生成分页的缓存 key CacheKey pageKey = cacheKey; //处理参数对象 parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey); //调用方言获取分页 sql String pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey); BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql, boundSql.getParameterMappings(), parameter); Map<String, Object> additionalParameters = getAdditionalParameter(boundSql); //设置动态参数 for (String key : additionalParameters.keySet()) { pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key)); } //执行分页查询 return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql); } else { //不执行分页的情况下,也不执行内存分页 return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql); } }
Example 7
Source File: PaginationHandler.java From sqlhelper with GNU Lesser General Public License v3.0 | 5 votes |
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: ResultLoader.java From mybaties with Apache License 2.0 | 5 votes |
private <E> List<E> selectList() throws SQLException { Executor localExecutor = executor; //如果executor已经被关闭了,则创建一个新的 if (Thread.currentThread().getId() != this.creatorThreadId || localExecutor.isClosed()) { localExecutor = newExecutor(); } try { //又调回Executor.query去了,比较巧妙 return localExecutor.<E> query(mappedStatement, parameterObject, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER, cacheKey, boundSql); } finally { if (localExecutor != executor) { localExecutor.close(false); } } }
Example 9
Source File: ResultLoader.java From mybatis with Apache License 2.0 | 5 votes |
private <E> List<E> selectList() throws SQLException { Executor localExecutor = executor; //如果executor已经被关闭了,则创建一个新的 if (Thread.currentThread().getId() != this.creatorThreadId || localExecutor.isClosed()) { localExecutor = newExecutor(); } try { //又调回Executor.query去了,比较巧妙 return localExecutor.<E> query(mappedStatement, parameterObject, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER, cacheKey, boundSql); } finally { if (localExecutor != executor) { localExecutor.close(false); } } }
Example 10
Source File: PaginationHandler.java From sqlhelper with GNU Lesser General Public License v3.0 | 4 votes |
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 11
Source File: PageStatementInterceptor.java From joyqueue with Apache License 2.0 | 4 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { StatementHandler handler = (StatementHandler) invocation.getTarget(); // 获取MappedStatement,Configuration对象 MetaObject metaObject = MetaObject.forObject(handler, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(), new DefaultReflectorFactory()); MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement"); String statement = mappedStatement.getId(); if (!isPageSql(statement,metaObject.getValue("boundSql.parameterObject"))) { return invocation.proceed(); } Configuration configuration = (Configuration) metaObject.getValue("delegate.configuration"); Executor executor = (Executor) metaObject.getValue("delegate.executor"); // 获取分页参数 BoundSql boundSql = handler.getBoundSql(); QPageQuery pageQuery = (QPageQuery) boundSql.getParameterObject(); String countStatement = buildCountStatement(statement); List<Integer> counts = executor.query(configuration. getMappedStatement(countStatement), pageQuery, RowBounds.DEFAULT, null); int count = 0; if (counts != null && !counts.isEmpty()) { count = counts.get(0) == null ? 0 : counts.get(0); } if (pageQuery.getPagination() == null) { pageQuery.setPagination(new Pagination()); } pageQuery.getPagination().setTotalRecord(count); String sql = boundSql.getSql(); if (logger.isDebugEnabled()) { logger.debug("raw SQL : " + sql); } if (sql == null || sql.isEmpty() || sql.contains(" limit ")) { return invocation.proceed(); } String originalSql = (String) metaObject.getValue("delegate.boundSql.sql"); metaObject.setValue("delegate.boundSql.sql", getLimitString(originalSql, pageQuery.getPagination().getStart(), pageQuery.getPagination().getSize())); metaObject.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET); metaObject.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT); if (logger.isDebugEnabled()) { logger.debug("pagination SQL : " + sql); } return invocation.proceed(); }
Example 12
Source File: CacheHandler.java From azeroth with Apache License 2.0 | 4 votes |
/** * 按更新的查询条件更新缓存 * @param executor * @param mt * @param mapperNameSpace * @param args */ private void removeCacheByUpdateConditon(Executor executor, MappedStatement mt, String mapperNameSpace, Object[] args) { try { Object parameterObject = args[1]; EntityInfo entityInfo = MybatisMapperParser.getEntityInfoByMapper(mapperNameSpace); MappedStatement statement = getQueryIdsMappedStatementForUpdateCache(mt, entityInfo); if (statement == null) { return; } String querySql = statement.getSqlSource().getBoundSql(parameterObject).getSql(); List<?> idsResult = null; if (PARSE_SQL_ERROR_DEFAULT.equals(querySql)) { BoundSql boundSql = mt.getBoundSql(parameterObject); querySql = "select " + entityInfo.getIdColumn() + " from " + entityInfo.getTableName() + " WHERE " + boundSql.getSql().split(WHERE_REGEX)[1]; BoundSql queryBoundSql = new BoundSql(statement.getConfiguration(), querySql, boundSql.getParameterMappings(), parameterObject); idsResult = executor.query(statement, parameterObject, RowBounds.DEFAULT, new DefaultResultHandler(), null, queryBoundSql); } else { idsResult = executor.query(statement, parameterObject, RowBounds.DEFAULT, null); } if (idsResult != null && !idsResult.isEmpty()) { for (Object id : idsResult) { String cacheKey = entityInfo.getEntityClass().getSimpleName() + ID_CACHEKEY_JOIN + id.toString(); getCacheProvider().remove(cacheKey); } if (logger.isDebugEnabled()) { logger.debug( "_autocache_ update Method[{}] executed,remove ralate cache {}.id:[{}]", mt.getId(), entityInfo.getEntityClass().getSimpleName(), idsResult); } } } catch (Exception e) { logger.error("_autocache_ update Method[{}] remove ralate cache error", e); } }
Example 13
Source File: ExecutorUtil.java From Mybatis-PageHelper with MIT License | 3 votes |
/** * 执行手动设置的 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; }