org.springframework.jdbc.core.ResultSetExtractor Java Examples
The following examples show how to use
org.springframework.jdbc.core.ResultSetExtractor.
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: PreJdbcUsersConnectionRepository.java From pre with GNU General Public License v3.0 | 7 votes |
@Override public Set<String> findUserIdsConnectedTo(String providerId, Set<String> providerUserIds) { MapSqlParameterSource parameters = new MapSqlParameterSource(); parameters.addValue("providerId", providerId); parameters.addValue("providerUserIds", providerUserIds); final Set<String> localUserIds = new HashSet<String>(); return new NamedParameterJdbcTemplate(jdbcTemplate).query("select userId from " + tablePrefix + "UserConnection where providerId = :providerId and providerUserId in (:providerUserIds)", parameters, new ResultSetExtractor<Set<String>>() { @Override public Set<String> extractData(ResultSet rs) throws SQLException, DataAccessException { while (rs.next()) { localUserIds.add(rs.getString("userId")); } return localUserIds; } }); }
Example #2
Source File: ShardJdbcTemplate.java From compass with Apache License 2.0 | 7 votes |
@SuppressWarnings("rawtypes") private ShardOperationProcessor<List> getNonAggregationProcessor(final RowMapper rowMapper) { final List<QueryCallable<List>> callableList = new ArrayList<ShardJdbcTemplate.QueryCallable<List>>(); ShardOperationProcessor<List> processor = new ShardOperationProcessor<List>() { @SuppressWarnings("unchecked") @Override public void addOperation(Shard shard, String sql, Object[] args) { ResultSetExtractor extractor = new RowMapperResultSetExtractor(rowMapper); QueryCallable<List> callable = new QueryCallable<List>(shard, sql, args, extractor); callableList.add(callable); } @Override public List processOperations() { List<List> rawResultList = ShardJdbcTemplate.this.executeQuery(callableList); return AggregationUtil.aggregateObjectList(rawResultList); } }; return processor; }
Example #3
Source File: DirectSqlGetPartition.java From metacat with Apache License 2.0 | 6 votes |
private Map<Long, Map<String, String>> getParameters(final List<Long> ids, final String sql, final String idName) { // Create the sql final StringBuilder queryBuilder = new StringBuilder(sql); if (!ids.isEmpty()) { queryBuilder.append(" and ").append(idName) .append(" in ('").append(Joiner.on("','").skipNulls().join(ids)).append("')"); } final ResultSetExtractor<Map<Long, Map<String, String>>> handler = rs -> { final Map<Long, Map<String, String>> result = Maps.newHashMap(); while (rs.next()) { final Long id = rs.getLong(idName); final String key = rs.getString("param_key"); final String value = rs.getString("param_value"); final Map<String, String> parameters = result.computeIfAbsent(id, k -> Maps.newHashMap()); parameters.put(key, value); } return result; }; return jdbcTemplate.query(queryBuilder.toString(), handler); }
Example #4
Source File: ComRecipientDaoImpl.java From openemm with GNU Affero General Public License v3.0 | 6 votes |
private <T> T selectRecipients(@VelocityCheck int companyID, ResultSetExtractor<T> extractor, String sqlStatement, Object... sqlParameters) throws SQLException { // TODO: IGNORE_BOUNCELOAD_COMPANY_ID is a bad hack for CONRAD-371 final boolean useUnsharpRecipientQuery = configService.useUnsharpRecipientQuery(companyID); try (Connection connection = getDataSource().getConnection()) { try { // TODO: IGNORE_BOUNCELOAD_COMPANY_ID is a bad hack for CONRAD-371!!! if (useUnsharpRecipientQuery) { setRuleOptimizerMode(connection, true); } final SingleConnectionDataSource scds = new SingleConnectionDataSource(connection, true); final JdbcTemplate template = new JdbcTemplate(scds); return template.query(sqlStatement, extractor, sqlParameters); } finally { // TODO: IGNORE_BOUNCELOAD_COMPANY_ID is a bad hack for CONRAD-371!!! if (useUnsharpRecipientQuery) { setRuleOptimizerMode(connection, false); } } } }
Example #5
Source File: TransactionLogStorage.java From dts with Apache License 2.0 | 6 votes |
public GlobalLog getGlobalLog(long transId) { return jdbcTemplate.query("select * from dts_global_record where trans_id = ?", new Object[] {transId}, new ResultSetExtractor<GlobalLog>() { @Override public GlobalLog extractData(ResultSet rs) throws SQLException, DataAccessException { if (!rs.next()) { return null; } GlobalLog log = new GlobalLog(); log.setTransId(rs.getLong("trans_id")); log.setState(rs.getInt("state")); log.setGmtCreated(rs.getTimestamp("gmt_created")); log.setGmtModified(rs.getTimestamp("gmt_modified")); log.setClientInfo(rs.getString("client_info")); log.setClientIp(rs.getString("client_ip")); return log; } }); }
Example #6
Source File: JdbcQueryService.java From poli with MIT License | 6 votes |
private QueryResult executeQuery(NamedParameterJdbcTemplate npjt, String sql, Map<String, Object> namedParameters, int resultLimit) { // Determine max query result final int maxQueryResult = JdbcQueryServiceHelper.calculateMaxQueryResultLimit(appProperties.getMaximumQueryRecords(), resultLimit); QueryResult result = npjt.query(sql, namedParameters, new ResultSetExtractor<QueryResult>() { @Nullable @Override public QueryResult extractData(ResultSet rs) { try { ResultSetMetaData metadata = rs.getMetaData(); String[] columnNames = getColumnNames(metadata); List<Column> columns = getColumnList(metadata); String data = resultSetToJsonString(rs, metadata, maxQueryResult); return QueryResult.ofData(data, columns); } catch (Exception e) { String error = CommonUtils.getSimpleError(e); return QueryResult.ofError(error); } } }); return result; }
Example #7
Source File: JdbcDaoImpl.java From herd with Apache License 2.0 | 6 votes |
/** * Delegates to {@link JdbcOperations#query(JdbcTemplate, String, ResultSetExtractor)} where the {@link ResultSetExtractor} converts the {@link ResultSet} * into {@link JdbcStatementResultSet}. */ @Override public JdbcStatementResultSet query(JdbcTemplate jdbcTemplate, String sql, final Integer maxResult) { return jdbcOperations.query(jdbcTemplate, sql, new ResultSetExtractor<JdbcStatementResultSet>() { @Override public JdbcStatementResultSet extractData(ResultSet resultSet) throws SQLException, DataAccessException { JdbcStatementResultSet jdbcStatementResultSet = new JdbcStatementResultSet(); List<String> columnNames = getColumnNames(resultSet.getMetaData()); jdbcStatementResultSet.setColumnNames(columnNames); List<JdbcStatementResultSetRow> rows = getRows(resultSet, maxResult); jdbcStatementResultSet.setRows(rows); return jdbcStatementResultSet; } }); }
Example #8
Source File: JdbcDataflowTaskExecutionDao.java From spring-cloud-dataflow with Apache License 2.0 | 6 votes |
@Override public Set<Long> getTaskExecutionIdsByTaskName(String taskName) { final MapSqlParameterSource queryParameters = new MapSqlParameterSource() .addValue("taskName", taskName, Types.VARCHAR); try { return this.jdbcTemplate.query(getQuery(FIND_TASK_EXECUTION_IDS_BY_TASK_NAME), queryParameters, new ResultSetExtractor<Set<Long>>() { @Override public Set<Long> extractData(ResultSet resultSet) throws SQLException, DataAccessException { Set<Long> taskExecutionIds = new TreeSet<>(); while (resultSet.next()) { taskExecutionIds .add(resultSet.getLong("TASK_EXECUTION_ID")); } return taskExecutionIds; } }); } catch (DataAccessException e) { return Collections.emptySet(); } }
Example #9
Source File: AbstractBlobResource.java From spring-content with Apache License 2.0 | 6 votes |
@Override public boolean exists() { String sql = "SELECT COUNT(id) FROM BLOBS WHERE id='" + this.id + "'"; return this.template.query(sql, new ResultSetExtractor<Boolean>() { @Override public Boolean extractData(ResultSet rs) throws SQLException, DataAccessException { if (rs.next()) { int count = rs.getInt(1); return count == 1; } else { return false; } } }); }
Example #10
Source File: YugongExampleJoinDataTranslator.java From yugong with GNU General Public License v2.0 | 6 votes |
public boolean translator(DataSource sourceDs, Record record) { ColumnValue idColum = record.getColumnByName("id"); if (idColum != null) { // 需要根据test.id字段,和另一张表做join,提取到关联id记录的name字段,合并输出到一个目标表 JdbcTemplate jdbcTemplate = new JdbcTemplate(sourceDs); String name_value = (String) jdbcTemplate.query("select NAME FROM JOIN_NAME WHERE JOIN_ID = " + idColum.getValue().toString(), new ResultSetExtractor() { public Object extractData(ResultSet rs) throws SQLException, DataAccessException { if (rs.next()) { return rs.getString("NAME"); } return null; } }); ColumnMeta nameMeta = new ColumnMeta("name", Types.VARCHAR); ColumnValue nameColumn = new ColumnValue(nameMeta, name_value); record.addColumn(nameColumn); } return super.translator(record); }
Example #11
Source File: DirectSqlGetPartition.java From metacat with Apache License 2.0 | 6 votes |
private List<FieldSchema> getPartitionKeys(final String databaseName, final String tableName, final boolean forceDisableAudit) { final List<FieldSchema> result = Lists.newArrayList(); final ResultSetExtractor<List<FieldSchema>> handler = rs -> { while (rs.next()) { final String name = rs.getString("pkey_name"); final String type = rs.getString("pkey_type"); result.add(new FieldSchema(name, type, null)); } return result; }; final Optional<QualifiedName> sourceTable = getSourceTableName(databaseName, tableName, forceDisableAudit); return sourceTable.map(qualifiedName -> jdbcTemplate .query(SQL.SQL_GET_AUDIT_TABLE_PARTITION_KEYS, new Object[]{databaseName, tableName, qualifiedName.getDatabaseName(), qualifiedName.getTableName()}, new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR}, handler)) .orElseGet(() -> jdbcTemplate .query(SQL.SQL_GET_PARTITION_KEYS, new Object[]{databaseName, tableName}, new int[]{Types.VARCHAR, Types.VARCHAR}, handler)); }
Example #12
Source File: MockJdbcOperations.java From herd with Apache License 2.0 | 5 votes |
/** * Executes query based on some predefined sql strings. * * CASE_1: * - Runs extractor on a result set which has 3 columns [COL1, COL2, COL3] and 2 rows [A, B, C] and [D, E, F] * CASE_2: * - Throws a DataIntegrityViolationException */ @SuppressWarnings("resource") @Override public <T> T query(JdbcTemplate jdbcTemplate, String sql, ResultSetExtractor<T> resultSetExtractor) { LOGGER.debug("sql = " + sql); MockResultSet mockResultSet = new MockResultSet(); List<List<String>> rows = new ArrayList<>(); MockResultSetMetaData mockResultSetMetaData = new MockResultSetMetaData(); if (CASE_1_SQL.equals(sql)) { mockResultSetMetaData.setColumnNames(Arrays.asList("COL1", "COL2", "COL3")); rows.add(Arrays.asList("A", "B", "C")); rows.add(Arrays.asList("D", "E", "F")); } else if (CASE_2_SQL.equals(sql)) { throw new DataIntegrityViolationException("test", new SQLException("test DataIntegrityViolationException cause")); } try { mockResultSet.setRowIterator(rows.iterator()); mockResultSet.setMockResultSetMetaData(mockResultSetMetaData); return resultSetExtractor.extractData(mockResultSet); } catch (SQLException e) { throw new RuntimeException(e); } }
Example #13
Source File: PermissionDaoImpl.java From qconfig with MIT License | 5 votes |
@Override public Integer selectPermission(String group, String rtxId) { return jdbcTemplate.query(SELECT_PERMISSION_SQL, new ResultSetExtractor<Integer>() { @Override public Integer extractData(ResultSet rs) throws SQLException, DataAccessException { if (rs.next()) { return rs.getInt("permission"); } else { return null; } } }, group, rtxId); }
Example #14
Source File: NamedParameterJdbcTemplate.java From java-technology-stack with MIT License | 5 votes |
@Override @Nullable public <T> T query(String sql, Map<String, ?> paramMap, ResultSetExtractor<T> rse) throws DataAccessException { return query(sql, new MapSqlParameterSource(paramMap), rse); }
Example #15
Source File: NamedParameterJdbcTemplate.java From java-technology-stack with MIT License | 5 votes |
@Override @Nullable public <T> T query(String sql, SqlParameterSource paramSource, ResultSetExtractor<T> rse) throws DataAccessException { return getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rse); }
Example #16
Source File: JdbcTaskExecutionDao.java From spring-cloud-task with Apache License 2.0 | 5 votes |
@Override public Set<Long> getJobExecutionIdsByTaskExecutionId(long taskExecutionId) { final MapSqlParameterSource queryParameters = new MapSqlParameterSource() .addValue("taskExecutionId", taskExecutionId, Types.BIGINT); try { return this.jdbcTemplate.query( getQuery(FIND_JOB_EXECUTION_BY_TASK_EXECUTION_ID), queryParameters, new ResultSetExtractor<Set<Long>>() { @Override public Set<Long> extractData(ResultSet resultSet) throws SQLException, DataAccessException { Set<Long> jobExecutionIds = new TreeSet<>(); while (resultSet.next()) { jobExecutionIds .add(resultSet.getLong("JOB_EXECUTION_ID")); } return jobExecutionIds; } }); } catch (DataAccessException e) { return Collections.emptySet(); } }
Example #17
Source File: CandidateSnapshotDaoImpl.java From qconfig with MIT License | 5 votes |
@Override public Long findEditVersion(ConfigMeta configMeta, long editVersion) { return jdbcTemplate.query(FIND_EDIT_VERSION_SQL, new Object[]{configMeta.getGroup(), configMeta.getProfile(), configMeta.getDataId(), editVersion}, new ResultSetExtractor<Long>() { @Override public Long extractData(ResultSet rs) throws SQLException, DataAccessException { if (rs.next()) { return rs.getLong(rs.getInt("edit_version")); } return -1L; } }); }
Example #18
Source File: DirectSqlGetPartition.java From metacat with Apache License 2.0 | 5 votes |
/** * query partitions using filters from name or uri column. */ private List<String> filterPartitionsColumn( final String databaseName, final String tableName, final List<String> partitionNames, final String columnName, final String filterExpression, final Sort sort, final Pageable pageable, final boolean forceDisableAudit) { final FilterPartition filter = new FilterPartition(); // batch exists final boolean isBatched = !Strings.isNullOrEmpty(filterExpression) && filterExpression.contains(FIELD_BATCHID); final boolean hasDateCreated = !Strings.isNullOrEmpty(filterExpression) && filterExpression.contains(FIELD_DATE_CREATED); ResultSetExtractor<List<String>> handler = rs -> { final List<String> columns = Lists.newArrayList(); while (rs.next()) { final String name = rs.getString(PARTITION_NAME); final String uri = rs.getString(PARTITION_URI); final long createdDate = rs.getLong(FIELD_DATE_CREATED); Map<String, String> values = null; if (hasDateCreated) { values = Maps.newHashMap(); values.put(FIELD_DATE_CREATED, createdDate + ""); } if (Strings.isNullOrEmpty(filterExpression) || filter.evaluatePartitionExpression(filterExpression, name, uri, isBatched, values)) { columns.add(rs.getString(columnName)); } } return columns; }; return getHandlerResults(databaseName, tableName, filterExpression, partitionNames, SQL.SQL_GET_PARTITIONS_WITH_KEY_URI, handler, sort, pageable, forceDisableAudit); }
Example #19
Source File: DataSourceMonitor.java From springboot-shiro-cas-mybatis with MIT License | 5 votes |
@Override protected StatusCode checkPool() throws Exception { return this.jdbcTemplate.query(this.validationQuery, new ResultSetExtractor<StatusCode>() { public StatusCode extractData(final ResultSet rs) throws SQLException { if (rs.next()) { return StatusCode.OK; } return StatusCode.WARN; } }); }
Example #20
Source File: NotificationServiceImpl.java From pacbot with Apache License 2.0 | 5 votes |
@Override public Map<String, Object> unsubscribeDigestMail(final String mailId) { String sql = "SELECT subscriptionValue AS subscriptionValue FROM PacmanSubscriptions WHERE emailId = ?"; String updateOrInsertQuery ; Object[] parameters; Date date = new Date(); Boolean isUnsubscriptedAlready = jdbcTemplate.query(sql, new Object[]{mailId}, new ResultSetExtractor<Boolean>() { @Override public Boolean extractData(ResultSet rs) { try { return rs.next() ? rs.getBoolean("subscriptionValue") : null; } catch (SQLException e) { Log.error(e.getMessage()); return null; } } }); if(isUnsubscriptedAlready != null) { updateOrInsertQuery="UPDATE PacmanSubscriptions SET emailId=?, subscriptionValue=?, modifiedDate=? WHERE emailId=?"; parameters = new Object[] {mailId, false, date, mailId}; } else { updateOrInsertQuery="INSERT INTO PacmanSubscriptions (emailId, subscriptionValue, createdDate, modifiedDate) VALUES (?,?,?,?)"; parameters = new Object[] {mailId, false, date, date}; } int status = jdbcTemplate.update(updateOrInsertQuery, parameters); Map<String, Object> response = Maps.newHashMap(); if(status==1) { response.put(STATUS, true); response.put(MESSAGE_KEY, "You have successfully unsubscribed"); } else { response.put(STATUS, false); response.put(MESSAGE_KEY, "Unsubscribing Failed!!!"); } return response; }
Example #21
Source File: DataSourceMonitor.java From cas4.0.x-server-wechat with Apache License 2.0 | 5 votes |
@Override protected StatusCode checkPool() throws Exception { return this.jdbcTemplate.query(this.validationQuery, new ResultSetExtractor<StatusCode>() { public StatusCode extractData(final ResultSet rs) throws SQLException { if (rs.next()) { return StatusCode.OK; } return StatusCode.WARN; } }); }
Example #22
Source File: DirectSqlGetPartition.java From metacat with Apache License 2.0 | 5 votes |
/** * Gets the partition uris based on a filter expression for the specified table. * * @param requestContext The Metacat request context * @param tableName table handle to get partition for * @param partitionsRequest The metadata for what kind of partitions to get from the table * @return filtered list of partition names */ @Transactional(readOnly = true) public List<String> getPartitionUris(final ConnectorRequestContext requestContext, final QualifiedName tableName, final PartitionListRequest partitionsRequest) { final long start = registry.clock().wallTime(); final List<String> result; final List<String> partitionNames = partitionsRequest.getPartitionNames(); final Sort sort = partitionsRequest.getSort(); final Pageable pageable = partitionsRequest.getPageable(); final String filterExpression = partitionsRequest.getFilter(); if (filterExpression != null) { return filterPartitionsColumn( tableName.getDatabaseName(), tableName.getTableName(), partitionNames, PARTITION_URI, filterExpression, sort, pageable, partitionsRequest.getIncludeAuditOnly()); } else { final ResultSetExtractor<List<String>> handler = rs -> { final List<String> uris = Lists.newArrayList(); while (rs.next()) { uris.add(rs.getString(PARTITION_URI)); } return uris; }; result = getHandlerResults(tableName.getDatabaseName(), tableName.getTableName(), null, partitionNames, SQL.SQL_GET_PARTITIONS_URI, handler, sort, pageable, partitionsRequest.getIncludeAuditOnly()); } this.fastServiceMetric.recordTimer( HiveMetrics.TagGetPartitionKeys.getMetricName(), registry.clock().wallTime() - start); return result; }
Example #23
Source File: TransactionLogStorage.java From dts with Apache License 2.0 | 5 votes |
public BranchLog getBranchLog(long branchId) { return jdbcTemplate.query("select * from dts_branch_record where branch_id = ?", new Object[] {branchId}, new ResultSetExtractor<BranchLog>() { @Override public BranchLog extractData(ResultSet rs) throws SQLException, DataAccessException { if (!rs.next()) { return null; } return rowToObject(rs); } }); }
Example #24
Source File: MysqlUserMetadataService.java From metacat with Apache License 2.0 | 5 votes |
@Override @Transactional(readOnly = true) public List<QualifiedName> searchByOwners(final Set<String> owners) { final List<QualifiedName> result = Lists.newArrayList(); final StringBuilder query = new StringBuilder(SQL.SEARCH_DEFINITION_METADATA_NAMES); final List<SqlParameterValue> paramList = Lists.newArrayList(); query.append(" where 1=0"); owners.forEach(s -> { query.append(" or data like ?"); paramList.add(new SqlParameterValue(Types.VARCHAR, "%\"userId\":\"" + s.trim() + "\"%")); }); final SqlParameterValue[] params = new SqlParameterValue[paramList.size()]; try { // Handler for reading the result set final ResultSetExtractor<Void> handler = rs -> { while (rs.next()) { final String definitionName = rs.getString("name"); result.add(QualifiedName.fromString(definitionName, false)); } return null; }; jdbcTemplate.query(query.toString(), paramList.toArray(params), handler); } catch (Exception e) { log.error("Failed to search by owners", e); throw new UserMetadataServiceException("Failed to search by owners", e); } return result; }
Example #25
Source File: JdbcDataflowTaskExecutionDao.java From spring-cloud-dataflow with Apache License 2.0 | 5 votes |
@Override public Set<Long> findChildTaskExecutionIds(Set<Long> taskExecutionIds) { final MapSqlParameterSource queryParameters = new MapSqlParameterSource() .addValue("parentTaskExecutionIds", taskExecutionIds); Set<Long> childTaskExecutionIds; try { childTaskExecutionIds = this.jdbcTemplate.query( getQuery(SELECT_CHILD_TASK_EXECUTION_IDS), queryParameters, new ResultSetExtractor<Set<Long>>() { @Override public Set<Long> extractData(ResultSet resultSet) throws SQLException, DataAccessException { Set<Long> jobExecutionIds = new TreeSet<>(); while (resultSet.next()) { jobExecutionIds .add(resultSet.getLong("TASK_EXECUTION_ID")); } return jobExecutionIds; } }); } catch (DataAccessException e) { childTaskExecutionIds = Collections.emptySet(); } if (!childTaskExecutionIds.isEmpty()) { childTaskExecutionIds.addAll(this.findChildTaskExecutionIds(childTaskExecutionIds)); } return childTaskExecutionIds; }
Example #26
Source File: PaginationHelper.java From diamond with Apache License 2.0 | 5 votes |
/** * 取分页 * * @param jt * jdbcTemplate * @param sqlCountRows * 查询总数的SQL * @param sqlFetchRows * 查询数据的sql * @param args * 查询参数 * @param pageNo * 页数 * @param pageSize * 每页大小 * @param rowMapper * @return */ public Page<E> fetchPage(final JdbcTemplate jt, final String sqlCountRows, final String sqlFetchRows, final Object args[], final int pageNo, final int pageSize, final ParameterizedRowMapper<E> rowMapper) { if (pageSize == 0) { return null; } // 查询当前记录总数 final int rowCount = jt.queryForInt(sqlCountRows, args); // 计算页数 int pageCount = rowCount / pageSize; if (rowCount > pageSize * pageCount) { pageCount++; } // 创建Page对象 final Page<E> page = new Page<E>(); page.setPageNumber(pageNo); page.setPagesAvailable(pageCount); page.setTotalCount(rowCount); if (pageNo > pageCount) return null; // 取单页数据,计算起始位置 final int startRow = (pageNo - 1) * pageSize; // TODO 在数据量很大时, limit效率很低 final String selectSQL = sqlFetchRows + " limit " + startRow + "," + pageSize; jt.query(selectSQL, args, new ResultSetExtractor() { public Object extractData(ResultSet rs) throws SQLException, DataAccessException { final List<E> pageItems = page.getPageItems(); int currentRow = 0; while (rs.next()) { pageItems.add(rowMapper.mapRow(rs, currentRow++)); } return page; } }); return page; }
Example #27
Source File: NamedParameterJdbcTemplate.java From spring-analysis-note with MIT License | 5 votes |
@Override @Nullable public <T> T query(String sql, Map<String, ?> paramMap, ResultSetExtractor<T> rse) throws DataAccessException { return query(sql, new MapSqlParameterSource(paramMap), rse); }
Example #28
Source File: PaginationHelper.java From diamond with Apache License 2.0 | 5 votes |
/** * 取分页 * * @param jt * jdbcTemplate * @param sqlCountRows * 查询总数的SQL * @param sqlFetchRows * 查询数据的sql * @param args * 查询参数 * @param pageNo * 页数 * @param pageSize * 每页大小 * @param rowMapper * @return */ public Page<E> fetchPage(final JdbcTemplate jt, final String sqlCountRows, final String sqlFetchRows, final Object args[], final int pageNo, final int pageSize, final ParameterizedRowMapper<E> rowMapper) { if (pageSize == 0) { return null; } // 查询当前记录总数 final int rowCount = jt.queryForInt(sqlCountRows, args); // 计算页数 int pageCount = rowCount / pageSize; if (rowCount > pageSize * pageCount) { pageCount++; } // 创建Page对象 final Page<E> page = new Page<E>(); page.setPageNumber(pageNo); page.setPagesAvailable(pageCount); page.setTotalCount(rowCount); if (pageNo > pageCount) return null; // 取单页数据,计算起始位置 final int startRow = (pageNo - 1) * pageSize; // TODO 在数据量很大时, limit效率很低 final String selectSQL = sqlFetchRows + " limit " + startRow + "," + pageSize; jt.query(selectSQL, args, new ResultSetExtractor() { public Object extractData(ResultSet rs) throws SQLException, DataAccessException { final List<E> pageItems = page.getPageItems(); int currentRow = 0; while (rs.next()) { pageItems.add(rowMapper.mapRow(rs, currentRow++)); } return page; } }); return page; }
Example #29
Source File: NamedParameterJdbcTemplate.java From spring-analysis-note with MIT License | 5 votes |
@Override @Nullable public <T> T query(String sql, SqlParameterSource paramSource, ResultSetExtractor<T> rse) throws DataAccessException { return getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rse); }
Example #30
Source File: JdbcQueryService.java From poli with MIT License | 5 votes |
public QueryResult executeQuery(DataSource dataSource, String sql, String contentType) { JdbcTemplate jt = new JdbcTemplate(dataSource); final int maxQueryResult = JdbcQueryServiceHelper.calculateMaxQueryResultLimit(appProperties.getMaximumQueryRecords(), Constants.QUERY_RESULT_NOLIMIT); QueryResult result = jt.query(sql, new Object[] {}, new ResultSetExtractor<QueryResult>() { @Nullable @Override public QueryResult extractData(ResultSet rs) { try { ResultSetMetaData metadata = rs.getMetaData(); String[] columnNames = getColumnNames(metadata); List<Column> columns = getColumnList(metadata); String data; if (Constants.CONTENT_TYPE_CSV.equals(contentType)) { data = resultSetToCsvString(rs, columnNames, maxQueryResult); } else { data = resultSetToJsonString(rs, metadata, maxQueryResult); } return QueryResult.ofData(data, columns); } catch (Exception e) { String error = CommonUtils.getSimpleError(e); return QueryResult.ofError(error); } } }); return result; }