List of usage examples for org.apache.ibatis.mapping MappedStatement getBoundSql
public BoundSql getBoundSql(Object parameterObject)
From source file:cn.myblog.uitl.MybatisPageHelper.PageHelper.java
License:Open Source License
@Override public Object intercept(Invocation invocation) throws Throwable { final Object[] args = invocation.getArgs(); RowBounds rowBounds = (RowBounds) args[2]; if (localPage.get() == null && rowBounds == RowBounds.DEFAULT) { return invocation.proceed(); } else {//from w ww.j ava2s . c o m //RowBounds-?Mybatis args[2] = RowBounds.DEFAULT; MappedStatement ms = (MappedStatement) args[0]; Object parameterObject = args[1]; BoundSql boundSql = ms.getBoundSql(parameterObject); //? Page page = localPage.get(); //?? localPage.remove(); if (page == null) { if (offsetAsPageNum) { page = new Page(rowBounds.getOffset(), rowBounds.getLimit(), rowBoundsWithCount ? Page.SQL_COUNT : Page.NO_SQL_COUNT); } else { page = new Page(rowBounds, rowBoundsWithCount ? Page.SQL_COUNT : Page.NO_SQL_COUNT); } } MappedStatement qs = newMappedStatement(ms, new BoundSqlSqlSource(boundSql)); //?MappedStatement?qs? args[0] = qs; MetaObject msObject = SystemMetaObject.forObject(qs); String sql = (String) msObject.getValue("sqlSource.boundSql.sql"); //?total??count if (page.getTotal() > Page.NO_SQL_COUNT) { //count - ?sql msObject.setValue("sqlSource.boundSql.sql", getCountSql(sql)); // Object result = invocation.proceed(); int totalCount = (Integer) ((List) result).get(0); page.setTotal(totalCount); int totalPage = totalCount / page.getPageSize() + ((totalCount % page.getPageSize() == 0) ? 0 : 1); page.setPages(totalPage); //sql - ?sql msObject.setValue("sqlSource.boundSql.sql", getPageSql(sql, page)); //?? msObject.setValue("resultMaps", ms.getResultMaps()); // result = invocation.proceed(); //? page.addAll((List) result); // return page; } else { //sql - ?sql msObject.setValue("sqlSource.boundSql.sql", getPageSql(sql, page)); //?? msObject.setValue("resultMaps", ms.getResultMaps()); // Object result = invocation.proceed(); //? page.addAll((List) result); // return page; } } }
From source file:cn.org.awcp.core.mybatis.page.PageHelper.java
License:Open Source License
/** * Mybatis/*from w ww. jav a2s . c o m*/ * * @param invocation ? * @return * @throws Throwable */ public Object intercept(Invocation invocation) throws Throwable { final Object[] args = invocation.getArgs(); RowBounds rowBounds = (RowBounds) args[2]; if (LOCAL_PAGE.get() == null && rowBounds == RowBounds.DEFAULT) { return invocation.proceed(); } else { //RowBounds-?Mybatis args[2] = RowBounds.DEFAULT; MappedStatement ms = (MappedStatement) args[0]; Object parameterObject = args[1]; //? Page page = getPage(rowBounds); //pageSizeZero if (pageSizeZero && page.getPageSize() == 0) { //? Object result = invocation.proceed(); //? page.addAll((List) result); // page.setPageNum(1); //?pageSize=total page.setPageSize(page.size()); //??total page.setTotal(page.size()); //?Page - ??? return page; } //?total??count if (page.isCount()) { BoundSql boundSql = null; //???sql??boundSql if (!SQLUTIL.isDynamic(ms)) { boundSql = ms.getBoundSql(parameterObject); } //?MappedStatement?qs args[0] = SQLUTIL.getCountMappedStatement(ms, boundSql); // Object result = invocation.proceed(); // page.setTotal((Integer) ((List) result).get(0)); if (page.getTotal() == 0) { return page; } } //pageSize>0pageSize<=0???count if (page.getPageSize() > 0 && ((rowBounds == RowBounds.DEFAULT && page.getPageNum() > 0) || rowBounds != RowBounds.DEFAULT)) { BoundSql boundSql = null; //???sql??boundSql if (!SQLUTIL.isDynamic(ms)) { boundSql = ms.getBoundSql(parameterObject); } //?MappedStatement?qs args[0] = SQLUTIL.getPageMappedStatement(ms, boundSql); //?sqlboundSqlms? if (boundSql == null) { boundSql = ((MappedStatement) args[0]).getBoundSql(parameterObject); } //parameterObject? args[1] = SQLUTIL.setPageParameter(ms, parameterObject, boundSql, page); // Object result = invocation.proceed(); //? page.addAll((List) result); } // return page; } }
From source file:com.appleframework.orm.mybatis.pagehelper.SqlUtil.java
License:Open Source License
/** * Mybatis//from w w w .j a v a 2 s. c o m * * @param invocation ? * @return * @throws Throwable */ private Page doProcessPage(Invocation invocation, Page page, Object[] args) throws Throwable { //?RowBounds? RowBounds rowBounds = (RowBounds) args[2]; //?ms MappedStatement ms = (MappedStatement) args[0]; //?PageSqlSource if (!isPageSqlSource(ms)) { processMappedStatement(ms); } //?parser????setThreadLocal??? ((PageSqlSource) ms.getSqlSource()).setParser(parser); try { //RowBounds-?Mybatis args[2] = RowBounds.DEFAULT; //?? pageSizeZero if (isQueryOnly(page)) { return doQueryOnly(page, invocation); } //?total??count if (page.isCount()) { page.setCountSignal(Boolean.TRUE); //?MS args[0] = msCountMap.get(ms.getId()); // Object result = invocation.proceed(); //ms args[0] = ms; // page.setTotal((Integer) ((List) result).get(0)); if (page.getTotal() == 0) { return page; } } else { page.setTotal(-1l); } //pageSize>0pageSize<=0???count if (page.getPageSize() > 0 && ((rowBounds == RowBounds.DEFAULT && page.getPageNum() > 0) || rowBounds != RowBounds.DEFAULT)) { //?MappedStatement?qs page.setCountSignal(null); BoundSql boundSql = ms.getBoundSql(args[1]); args[1] = parser.setPageParameter(ms, args[1], boundSql, page); page.setCountSignal(Boolean.FALSE); // Object result = invocation.proceed(); //? page.addAll((List) result); } } finally { ((PageSqlSource) ms.getSqlSource()).removeParser(); } // return page; }
From source file:com.autonavi.tsp.workbackend.util.page.SqlUtil.java
License:Open Source License
/** * ?//from ww w . j a v a 2 s.c o m * * @param parameterObject * @param page * @return */ public Map setPageParameter(MappedStatement ms, Object parameterObject, Page page) { BoundSql boundSql = ms.getBoundSql(parameterObject); return sqlParser.setPageParameter(ms, parameterObject, boundSql, page); }
From source file:com.badminton.interceptors.mySqlHelper.pagehelper.util.SqlUtil.java
License:Open Source License
/** * /*from w ww .j av a2 s . com*/ * * @param invocation * @return * @throws Throwable */ public Object doIntercept(Invocation invocation) throws Throwable { //?? Object[] args = invocation.getArgs(); MappedStatement ms = (MappedStatement) args[0]; Object parameterObject = args[1]; RowBounds rowBounds = (RowBounds) args[2]; List resultList; if (autoDialect) { lock.lock(); try { if (autoDialect) { autoDialect = false; this.dialect = getDialect(ms); } } finally { lock.unlock(); } } Dialect runtimeDialect = dialect; if (autoRuntimeDialect) { runtimeDialect = getDialect(ms); } //???? if (!runtimeDialect.skip(ms, parameterObject, rowBounds)) { ResultHandler resultHandler = (ResultHandler) args[3]; //? Executor executor = (Executor) invocation.getTarget(); BoundSql boundSql = ms.getBoundSql(parameterObject); //????? Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField .get(boundSql); //?? count if (runtimeDialect.beforeCount(ms, parameterObject, rowBounds)) { // count key CacheKey countKey = executor.createCacheKey(ms, parameterObject, RowBounds.DEFAULT, boundSql); countKey.update("_Count"); MappedStatement countMs = msCountMap.get(countKey); if (countMs == null) { //?? ms Long ms countMs = MSUtils.newCountMappedStatement(ms); msCountMap.put(countKey, countMs); } //? count sql String countSql = runtimeDialect.getCountSql(ms, boundSql, parameterObject, rowBounds, countKey); BoundSql countBoundSql = new BoundSql(ms.getConfiguration(), countSql, boundSql.getParameterMappings(), parameterObject); //? SQL ???? BoundSql for (String key : additionalParameters.keySet()) { countBoundSql.setAdditionalParameter(key, additionalParameters.get(key)); } // count Object countResultList = executor.query(countMs, parameterObject, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql); Long count = (Long) ((List) countResultList).get(0); //? runtimeDialect.afterCount(count, parameterObject, rowBounds); if (count == 0L) { // 0 return runtimeDialect.afterPage(new ArrayList(), parameterObject, rowBounds); } } //?? if (runtimeDialect.beforePage(ms, parameterObject, rowBounds)) { //? key CacheKey pageKey = executor.createCacheKey(ms, parameterObject, rowBounds, boundSql); //?? parameterObject = runtimeDialect.processParameterObject(ms, parameterObject, boundSql, pageKey); //? sql String pageSql = runtimeDialect.getPageSql(ms, boundSql, parameterObject, rowBounds, pageKey); BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql, boundSql.getParameterMappings(), parameterObject); //?? for (String key : additionalParameters.keySet()) { pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key)); } // resultList = executor.query(ms, parameterObject, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql); } else { resultList = new ArrayList(); } } else { args[2] = RowBounds.DEFAULT; resultList = (List) invocation.proceed(); } // return runtimeDialect.afterPage(resultList, parameterObject, rowBounds); }
From source file:com.baomidou.mybatisplus.plugins.CachePaginationInterceptor.java
License:Apache License
/** * Physical Pagination Interceptor for all the queries with parameter * {@link org.apache.ibatis.session.RowBounds} *///from w ww. j a v a 2s .c o m public Object intercept(Invocation invocation) throws Throwable { Object target = invocation.getTarget(); if (target instanceof StatementHandler) { StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget()); MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler); RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds"); if (rowBounds == null || rowBounds == RowBounds.DEFAULT) { return invocation.proceed(); } BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql"); String originalSql = boundSql.getSql(); if (rowBounds instanceof Pagination) { Pagination page = (Pagination) rowBounds; boolean orderBy = true; if (page.isSearchCount()) { CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType, page.isOptimizeCount()); orderBy = countOptimize.isOrderBy(); } String buildSql = SqlUtils.concatOrderBy(originalSql, page, orderBy); originalSql = DialectFactory.buildPaginationSql(page, buildSql, dialectType, dialectClazz); } else { // support physical Pagination for RowBounds originalSql = DialectFactory.buildPaginationSql(rowBounds, originalSql, dialectType, dialectClazz); } metaStatementHandler.setValue("delegate.boundSql.sql", originalSql); metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET); metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT); } else { RowBounds rowBounds = (RowBounds) invocation.getArgs()[2]; if (rowBounds == null || rowBounds == RowBounds.DEFAULT) { return invocation.proceed(); } MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; Executor executor = (Executor) invocation.getTarget(); Connection connection = executor.getTransaction().getConnection(); Object parameterObject = invocation.getArgs()[1]; BoundSql boundSql = mappedStatement.getBoundSql(parameterObject); String originalSql = boundSql.getSql(); if (rowBounds instanceof Pagination) { Pagination page = (Pagination) rowBounds; if (page.isSearchCount()) { CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType, page.isOptimizeCount()); super.queryTotal(countOptimize.getCountSQL(), mappedStatement, boundSql, page, connection); if (page.getTotal() <= 0) { return invocation.proceed(); } } } } return invocation.proceed(); }
From source file:com.baomidou.mybatisplus.plugins.SqlExplainInterceptor.java
License:Apache License
public Object intercept(Invocation invocation) throws Throwable { /**//from w w w . j a va 2 s . c o m * ? DELETE UPDATE ? */ MappedStatement ms = (MappedStatement) invocation.getArgs()[0]; if (ms.getSqlCommandType() == SqlCommandType.DELETE || ms.getSqlCommandType() == SqlCommandType.UPDATE) { Executor executor = (Executor) invocation.getTarget(); Configuration configuration = ms.getConfiguration(); Object parameter = invocation.getArgs()[1]; BoundSql boundSql = ms.getBoundSql(parameter); Connection connection = executor.getTransaction().getConnection(); String databaseVersion = connection.getMetaData().getDatabaseProductVersion(); if (GlobalConfiguration.getDbType(configuration).equals(DBType.MYSQL) && VersionUtils.compare(minMySQLVersion, databaseVersion)) { logger.warn("Warn: Your mysql version needs to be greater than '5.6.3' to execute of Sql Explain!"); return invocation.proceed(); } /** * SQL ? */ sqlExplain(configuration, ms, boundSql, connection, parameter); } return invocation.proceed(); }
From source file:com.bluesky.iplatform.commons.db.mybatis.SqlHelper.java
License:Open Source License
/** * ????sql//www. j a v a 2s. c om * * @param session * @param namespace * @param params * @return */ public static String getNamespaceSql(SqlSession session, String namespace, Object params) { params = wrapCollection(params); Configuration configuration = session.getConfiguration(); MappedStatement mappedStatement = configuration.getMappedStatement(namespace); TypeHandlerRegistry typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry(); BoundSql boundSql = mappedStatement.getBoundSql(params); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); String sql = boundSql.getSql(); if (parameterMappings != null) { for (int i = 0; i < parameterMappings.size(); i++) { ParameterMapping parameterMapping = parameterMappings.get(i); if (parameterMapping.getMode() != ParameterMode.OUT) { Object value; String propertyName = parameterMapping.getProperty(); if (boundSql.hasAdditionalParameter(propertyName)) { value = boundSql.getAdditionalParameter(propertyName); } else if (params == null) { value = null; } else if (typeHandlerRegistry.hasTypeHandler(params.getClass())) { value = params; } else { MetaObject metaObject = configuration.newMetaObject(params); value = metaObject.getValue(propertyName); } JdbcType jdbcType = parameterMapping.getJdbcType(); if (value == null && jdbcType == null) jdbcType = configuration.getJdbcTypeForNull(); sql = replaceParameter(sql, value, jdbcType, parameterMapping.getJavaType()); } } } return sql; }
From source file:com.bsb.cms.commons.page.interceptor.PaginationInterceptor.java
License:Open Source License
@Override public Object intercept(Invocation invocation) throws Throwable { // ? MappedStatementBoundSql?sql? MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; if (mappedStatement.getId().matches(pageSqlId)) { // ?SQL Object parameter = invocation.getArgs()[1]; BoundSql boundSql = mappedStatement.getBoundSql(parameter); String originalSql = boundSql.getSql().trim(); Object parameterObject = boundSql.getParameterObject(); /*//from ww w . jav a2s .com * if (parameterObject != null) { page = * (Page)ReflectHelper.isPage(parameterObject, "page"); } * * if ((page == null) && (context.isPagination())) { page = context; * } */ // Pagination page = null; PageContext page = PageContext.getContext(); // Page?? String countSql = getCountSql(originalSql); Connection connection = mappedStatement.getConfiguration().getEnvironment().getDataSource() .getConnection(); PreparedStatement countStmt = connection.prepareStatement(countSql); BoundSql countBS = copyFromBoundSql(mappedStatement, boundSql, countSql); log.debug("countSql=" + countSql); DefaultParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, countBS); parameterHandler.setParameters(countStmt); ResultSet rs = countStmt.executeQuery(); int totpage = 0; if (rs.next()) { totpage = rs.getInt(1); } rs.close(); countStmt.close(); connection.close();// TODO // page.init(totpage, page.getPageSize(), page.getCurrentPage()); if ((StringUtils.isNotEmpty(page.getOrderBy())) && (originalSql.indexOf("BUSINESS_CIRCLE") == -1)) { originalSql = originalSql + " ORDER BY " + page.getOrderBy(); } String pageSql = buildPageSqlForMysql(originalSql, page).toString(); BoundSql newBoundSql = copyFromBoundSql(mappedStatement, boundSql, pageSql); MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql)); log.debug("newMs=" + pageSql); invocation.getArgs()[0] = newMs; } return invocation.proceed(); }
From source file:com.cheyipai.platformservice.thirdparty.core.pager.PageHelper.java
License:Open Source License
/** * Mybatis// w w w . ja v a 2 s . c o m * * @param invocation ? * @return * @throws Throwable */ @Override public Object intercept(Invocation invocation) throws Throwable { final Object[] args = invocation.getArgs(); RowBounds rowBounds = (RowBounds) args[2]; if (LOCAL_PAGE.get() == null && rowBounds == RowBounds.DEFAULT) { return invocation.proceed(); } else { //RowBounds-?Mybatis args[2] = RowBounds.DEFAULT; MappedStatement ms = (MappedStatement) args[0]; Object parameterObject = args[1]; //? Page page = getPage(rowBounds); //pageSizeZero if (pageSizeZero && page.getPageSize() == 0) { //? Object result = invocation.proceed(); //? page.addAll((List) result); // page.setPageNum(1); //?pageSize=total page.setPageSize(page.size()); //??total page.setTotal(page.size()); //?Page - ??? return page; } //?total??count if (page.isCount()) { BoundSql boundSql = ms.getBoundSql(parameterObject); //?MappedStatement?qs args[0] = getMappedStatement(ms, boundSql, SUFFIX_COUNT); // Object result = invocation.proceed(); // page.setTotal((Integer) ((List) result).get(0)); if (page.getTotal() == 0) { return page; } } //pageSize>0pageSize<=0???count if (page.getPageSize() > 0 && ((rowBounds == RowBounds.DEFAULT && page.getPageNum() > 0) || rowBounds != RowBounds.DEFAULT)) { BoundSql boundSql = ms.getBoundSql(parameterObject); //?MappedStatement?qs args[0] = getMappedStatement(ms, boundSql, SUFFIX_PAGE); //parameterObject? args[1] = setPageParameter(parameterObject, boundSql, page); // Object result = invocation.proceed(); //? page.addAll((List) result); } // return page; } }