List of usage examples for org.apache.ibatis.scripting.defaults DefaultParameterHandler DefaultParameterHandler
public DefaultParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql)
From source file:com.baomidou.mybatisplus.plugins.SqlExplainInterceptor.java
License:Apache License
/** * <p>//from w w w .j ava 2 s .c o m * ? 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(); /*//from www. j ava 2 s . c om * 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.esofthead.mycollab.core.persistence.VelocityDriverDeclare.java
License:Open Source License
@Override public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {//from w ww . j av a 2 s. c o m return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql); }
From source file:com.hj.blog.common.orm.PageInterceptor.java
License:Apache License
private void setTotalCount(DigitalPage page, Object parameterObject, MappedStatement mappedStatement, Connection connection) {//from ww w . j a v a 2s . c o m BoundSql boundSql = mappedStatement.getBoundSql(parameterObject); String sql = boundSql.getSql(); String countSql = getTotalCountSql(sql); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); BoundSql countBoundSql = new BoundSql(mappedStatement.getConfiguration(), countSql, parameterMappings, parameterObject); ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, countBoundSql); PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(countSql); parameterHandler.setParameters(preparedStatement); resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { int totalCount = resultSet.getInt(1); page.setTotalCount(totalCount); } } catch (SQLException e) { throw new RuntimeException(e); } finally { try { if (resultSet != null) resultSet.close(); if (preparedStatement != null) preparedStatement.close(); } catch (SQLException e) { throw new RuntimeException(e); } } }
From source file:com.huang.rp.common.persistence.PageInterceptor.java
License:Apache License
/** * ??page//from www . jav a 2 s .c om */ private void setTotalRecord(QueryFilter filter, MappedStatement mappedStatement, Connection connection) { BoundSql boundSql = mappedStatement.getBoundSql(filter); String sql = boundSql.getSql(); String countSql = this.getCountSql(sql); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); BoundSql countBoundSql = new BoundSql(mappedStatement.getConfiguration(), countSql, parameterMappings, boundSql.getParameterObject()); MetaObject metaParameters = (MetaObject) ReflectionUtils.getFieldValue(boundSql, "metaParameters"); ReflectionUtils.setFieldValue(countBoundSql, "metaParameters", metaParameters); ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, filter, countBoundSql); PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = connection.prepareStatement(countSql); parameterHandler.setParameters(pstmt); rs = pstmt.executeQuery(); if (rs.next()) { int totalRecord = rs.getInt(1); filter.setRecords(totalRecord); int rows = filter.getRows(); // int total = totalRecord / rows; total = totalRecord % rows == 0 ? total : total + 1; filter.setTotal(total); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } }
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 {/*from w ww .j a v a2 s . c om*/ 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.monee1988.core.mybatis.pageinterceptor.PageInterceptor.java
License:Open Source License
/** * * @param page// w ww . j av a 2s . c om * @param mappedStatement * @param connection * @param parameterObject */ private void setTotalRecord(Page<?> page, MappedStatement mappedStatement, Connection connection, Object parameterObject) { BoundSql boundSql = mappedStatement.getBoundSql(parameterObject); String sql = boundSql.getSql(); String countSql = removeBreakingWhitespace(this.getCountSql(sql)); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); BoundSql countBoundSql = new BoundSql(mappedStatement.getConfiguration(), countSql, parameterMappings, parameterObject); ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, countBoundSql); PreparedStatement pstmt = null; ResultSet rs = null; logger.debug("Total count SQL [{}] ", countSql.toString()); logger.debug("Total count Parameters: {} ", parameterObject); try { pstmt = connection.prepareStatement(countSql); parameterHandler.setParameters(pstmt); rs = pstmt.executeQuery(); if (rs.next()) { int totalRecord = rs.getInt(1); logger.debug("Total count: {}", totalRecord); page.setTotalCount(totalRecord); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:com.mybatisX.plugins.PaginationInterceptor.java
License:Apache License
/** * ?/* w w w. ja va 2 s.co 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); BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), sql, boundSql.getParameterMappings(), boundSql.getParameterObject()); ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, boundSql.getParameterObject(), countBS); 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:com.mybatisX.plugins.SqlExplainInterceptor.java
License:Apache License
/** * <p>/* ww w.ja va 2 s . c o m*/ * ? 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:com.navercorp.pinpoint.web.dao.ibatis.BindingLogPlugin32.java
License:Apache License
private List<String> getParameters(MappedStatement ms, Object parameterObject, BoundSql boundSql) throws SQLException { // DefaultParameterHandler is the only implementation of parameterHandler interface currently. it may be changed later. // need additional codes to find a appropriate implementation in that case. ParameterHandler parameterHandler = new DefaultParameterHandler(ms, parameterObject, boundSql); PreparedStatementParameterLogger parameterLogger = new PreparedStatementParameterLogger(); parameterHandler.setParameters(parameterLogger); return parameterLogger.getParameters(); }