List of usage examples for org.apache.ibatis.scripting.defaults DefaultParameterHandler setParameters
@Override
public void setParameters(PreparedStatement ps)
From source file:com.baomidou.mybatisplus.plugins.PaginationInterceptor.java
License:Apache License
/** * ?/*from ww w .j a va2s . co m*/ * * @param sql * @param mappedStatement * @param boundSql * @param page */ protected void queryTotal(String sql, MappedStatement mappedStatement, BoundSql boundSql, Pagination page, Connection connection) { try (PreparedStatement statement = connection.prepareStatement(sql)) { DefaultParameterHandler parameterHandler = new MybatisDefaultParameterHandler(mappedStatement, boundSql.getParameterObject(), boundSql); parameterHandler.setParameters(statement); int total = 0; try (ResultSet resultSet = statement.executeQuery()) { if (resultSet.next()) { total = resultSet.getInt(1); } } page.setTotal(total); /* * */ int pages = page.getPages(); if (overflowCurrent && (page.getCurrent() > pages)) { page = new Pagination(1, page.getSize()); page.setTotal(total); } } catch (Exception e) { logger.error("Error: Method queryTotal execution error !", e); } }
From source file:com.baomidou.mybatisplus.plugins.SqlExplainInterceptor.java
License:Apache License
/** * <p>/* w w w. ja va 2 s . c om*/ * ? SQL * </p> * * @param configuration * @param mappedStatement * @param boundSql * @param connection * @param parameter * @return * @throws Exception */ protected void sqlExplain(Configuration configuration, MappedStatement mappedStatement, BoundSql boundSql, Connection connection, Object parameter) { StringBuilder explain = new StringBuilder("EXPLAIN "); explain.append(boundSql.getSql()); String sqlExplain = explain.toString(); StaticSqlSource sqlsource = new StaticSqlSource(configuration, sqlExplain, boundSql.getParameterMappings()); MappedStatement.Builder builder = new MappedStatement.Builder(configuration, "explain_sql", sqlsource, SqlCommandType.SELECT); builder.resultMaps(mappedStatement.getResultMaps()).resultSetType(mappedStatement.getResultSetType()) .statementType(mappedStatement.getStatementType()); MappedStatement query_statement = builder.build(); DefaultParameterHandler handler = new DefaultParameterHandler(query_statement, parameter, boundSql); try (PreparedStatement stmt = connection.prepareStatement(sqlExplain)) { handler.setParameters(stmt); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { if (!"Using where".equals(rs.getString("Extra"))) { if (this.isStopProceed()) { throw new MybatisPlusException( "Error: Full table operation is prohibited. SQL: " + boundSql.getSql()); } break; } } } } catch (Exception e) { throw new MybatisPlusException(e); } }
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(); /*/* w w w . j a v a 2s . 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.jeebase.common.base.component.DataPermissionPaginationInterceptor.java
License:Apache License
/** * ?/*from ww w . j ava2 s . c o m*/ * * @param sql count sql * @param mappedStatement MappedStatement * @param boundSql BoundSql * @param page IPage * @param connection Connection */ protected void queryTotal(boolean overflowCurrent, String sql, MappedStatement mappedStatement, BoundSql boundSql, IPage page, Connection connection) { try (PreparedStatement statement = connection.prepareStatement(sql)) { DefaultParameterHandler parameterHandler = new MybatisDefaultParameterHandler(mappedStatement, boundSql.getParameterObject(), boundSql); parameterHandler.setParameters(statement); long total = 0; try (ResultSet resultSet = statement.executeQuery()) { if (resultSet.next()) { total = resultSet.getLong(1); } } page.setTotal(total); /* * */ long pages = page.getPages(); if (overflowCurrent && page.getCurrent() > pages) { // ? page.setCurrent(1); } } catch (Exception e) { throw ExceptionUtils.mpe("Error: Method queryTotal execution error of sql : \n %s \n", e, sql); } }
From source file:com.jhcz.trade.framework.plugin.mybatis.PagePlugin.java
License:Open Source License
public Object intercept(Invocation ivk) throws Throwable { if (ivk.getTarget() instanceof RoutingStatementHandler) { RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget(); BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper .getValueByFieldName(statementHandler, "delegate"); MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(delegate, "mappedStatement"); // 1???.*query.* 2??page? // if (mappedStatement.getId().matches(pageSqlId)) { // ?SQL BoundSql boundSql = delegate.getBoundSql(); // SQL<select>parameterType??Mapper??,?? Object parameterObject = boundSql.getParameterObject(); if (parameterObject == null) { // throw new // NullPointerException("boundSql.getParameterObject() is null!"); return ivk.proceed(); } else {/* ww w . j a v a2 s . c o m*/ PageView pageView = null; if (parameterObject instanceof PageView) { // ?Pages pageView = (PageView) parameterObject; } else if (parameterObject instanceof Map) { for (Entry entry : (Set<Entry>) ((Map) parameterObject).entrySet()) { if (entry.getValue() instanceof PageView) { pageView = (PageView) entry.getValue(); break; } } if (pageView == null) { return ivk.proceed(); } } else { // ??Pages pageView = ReflectHelper.getValueByFieldType(parameterObject, PageView.class); if (pageView == null) { return ivk.proceed(); } } String sql = boundSql.getSql(); PreparedStatement countStmt = null; ResultSet rs = null; try { // Connection connection = (Connection) ivk.getArgs()[0]; String countSql = "select count(1) from (" + sql + ") tmp_count"; countStmt = connection.prepareStatement(countSql); ReflectHelper.setValueByFieldName(boundSql, "sql", countSql); DefaultParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql); parameterHandler.setParameters(countStmt); rs = countStmt.executeQuery(); int count = 0; if (rs.next()) { count = ((Number) rs.getObject(1)).intValue(); } pageView.setRowCount(count); } finally { try { rs.close(); } catch (Exception e) { } try { countStmt.close(); } catch (Exception e) { } } String pageSql = generatePagesSql(sql, pageView); ReflectHelper.setValueByFieldName(boundSql, "sql", pageSql); // sql???BoundSql. } // } } return ivk.proceed(); }
From source file:com.mybatisX.plugins.SqlExplainInterceptor.java
License:Apache License
/** * <p>// www. j a v a 2s. com * ? SQL * </p> * * @param configuration * @param mappedStatement * @param boundSql * @param connection * @param parameter * @return * @throws Exception */ protected void sqlExplain(Configuration configuration, MappedStatement mappedStatement, BoundSql boundSql, Connection connection, Object parameter) { PreparedStatement stmt = null; ResultSet rs = null; try { StringBuilder explain = new StringBuilder("EXPLAIN "); explain.append(boundSql.getSql()); String sqlExplain = explain.toString(); StaticSqlSource sqlsource = new StaticSqlSource(configuration, sqlExplain, boundSql.getParameterMappings()); MappedStatement.Builder builder = new MappedStatement.Builder(configuration, "explain_sql", sqlsource, SqlCommandType.SELECT); builder.resultMaps(mappedStatement.getResultMaps()).resultSetType(mappedStatement.getResultSetType()) .statementType(mappedStatement.getStatementType()); MappedStatement query_statement = builder.build(); DefaultParameterHandler handler = new DefaultParameterHandler(query_statement, parameter, boundSql); stmt = connection.prepareStatement(sqlExplain); handler.setParameters(stmt); rs = stmt.executeQuery(); while (rs.next()) { if (!"Using where".equals(rs.getString("Extra"))) { String tip = " Full table operation is prohibited. SQL: " + boundSql.getSql(); if (this.isStopProceed()) { throw new MybatisXException(tip); } logger.error(tip); break; } } } catch (Exception e) { throw new MybatisXException(e); } finally { IOUtils.closeQuietly(rs, stmt); } }
From source file:org.apache.playframework.mybatisplus.plugins.PaginationInterceptor.java
License:Apache License
/** * ?/*from ww w .j ava2s. c o m*/ * * @param sql * @param connection * @param mappedStatement * @param boundSql * @param page */ public Pagination count(String sql, Connection connection, MappedStatement mappedStatement, BoundSql boundSql, Pagination page) { PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = connection.prepareStatement(sql); DefaultParameterHandler parameterHandler = new MybatisDefaultParameterHandler(mappedStatement, boundSql.getParameterObject(), boundSql); parameterHandler.setParameters(pstmt); rs = pstmt.executeQuery(); int total = 0; if (rs.next()) { total = rs.getInt(1); } page.setTotal(total); /* * */ if (overflowCurrent && (page.getCurrent() > page.getPages())) { page = new Pagination(1, page.getSize()); page.setTotal(total); } } catch (Exception e) { // ignored } finally { IOUtils.closeQuietly(pstmt, rs); } return page; }
From source file:org.solmix.datax.mybatis.page.PageInterceptor.java
License:Open Source License
/** * {@inheritDoc}/*from ww w. j a v a 2 s . c om*/ * * @see org.apache.ibatis.plugin.Interceptor#intercept(org.apache.ibatis.plugin.Invocation) */ @Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); MappedStatement statement = (MappedStatement) args[0]; Object parameter = args[1]; if (parameter instanceof PagedParameter) { PagedParameter p = (PagedParameter) parameter; BoundSql boundSql = statement.getBoundSql(p.getValues()); String originalSql = boundSql.getSql().trim(); SQLDialect sqlDriver = p.getSqlDialect(); Integer total = p.page.getTotalRow(); if (total == null || total <= 0) { String countSql = sqlDriver.getRowCountQueryString(originalSql); Connection connection = p.sqlSession.getConnection(); PreparedStatement countStmt = connection.prepareStatement(countSql); BoundSql countBS = copyFromBoundSql(statement, boundSql, countSql, p.getValues()); DefaultParameterHandler parameterHandler = new DefaultParameterHandler(statement, p.getValues(), countBS); parameterHandler.setParameters(countStmt); ResultSet rs = null; try { rs = countStmt.executeQuery(); } catch (Exception e) { LOG.error("Page sql:\n{} with values:\n{}", countSql, p.getValues()); throw e; } if (rs.next()) { total = rs.getInt(1); } rs.close(); countStmt.close(); } p.page.setTotalRow(total); int end = p.page.getEndRow(); int start = p.page.getStartRow(); int batch = p.page.getBatchSize(); if (end != -1 && end - start > batch) { batch = end - start; p.page.setBatchSize(batch); } String limitQuery = sqlDriver.limitQuery(originalSql, start, batch, null); BoundSql newBoundSql = copyFromBoundSql(statement, boundSql, limitQuery.toString(), p.values); MappedStatement newMs = copyFromMappedStatement(statement, new BoundSqlSqlSource(newBoundSql)); invocation.getArgs()[0] = newMs; invocation.getArgs()[1] = p.values; } return invocation.proceed(); }