List of usage examples for org.apache.ibatis.executor.parameter ParameterHandler setParameters
void setParameters(PreparedStatement ps) throws SQLException;
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 w ww . j av a 2 s. c om 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//w w w . j av a2 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.monee1988.core.mybatis.pageinterceptor.PageInterceptor.java
License:Open Source License
/** * * @param page/*from ww w . j a va2 s .c o m*/ * @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 2s. c om * * @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.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(); }
From source file:com.tj.mybatisplus.plugins.PaginationInterceptor.java
License:Apache License
/** * ?/*from w w w.ja va2s . 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) { String sqlUse = sql; int order_by = sql.toUpperCase().lastIndexOf("ORDER BY"); if (order_by > -1) { sqlUse = sql.substring(0, order_by); } StringBuffer countSql = new StringBuffer("SELECT COUNT(1) AS TOTAL FROM ("); countSql.append(sqlUse).append(") A"); PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = connection.prepareStatement(countSql.toString()); BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql.toString(), 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 (page.getCurrent() > page.getPages()) { page = new Pagination(1, page.getSize()); page.setTotal(total); } } catch (SQLException e) { e.printStackTrace(); } finally { try { rs.close(); pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } return page; }