Skip to content

Commit

Permalink
- add generic support for ShardJdbcTemplate#query() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtyan authored and comnetwork committed Feb 15, 2022
1 parent e6e7ed7 commit 7528056
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/**
* 此处定义了ShardJdbcTemplate支持的基本jdbc操作
* @author yk
* @author yk | kurtyan777@gmail.com
* @since 1.0.0
*/
public interface ShardJdbcOperations {
Expand Down Expand Up @@ -55,7 +55,7 @@ public interface ShardJdbcOperations {
* @param rowMapper
* @return
*/
public List query(String sql, Object[] args, RowMapper rowMapper);
public <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper);


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
* 注1:只有用分表依据字段作为参数时,才支持形式2。对于非分表依据字段的批量操作,请使用形式1中的方式进行操作。
*
*
* @author yk
* @author yk | kurtyan777@gmail.com
* @since 1.0.0
*/
public class ShardJdbcTemplate extends ShardJdbcConfig implements ShardJdbcOperations, InitializingBean, DisposableBean {
Expand Down Expand Up @@ -223,13 +223,12 @@ public List<Map<String, Object>> query(String sql, Object[] args) {
return this.query(sql, args, new ColumnMapRowMapper());
}

@SuppressWarnings("rawtypes")
@Override
public List query(String sql, Object[] args, final RowMapper rowMapper) {
public <T> List<T> query(String sql, Object[] args, final RowMapper<T> rowMapper) {
Assert.notNull(sql, "sql must not be null");
Assert.notNull(rowMapper, "rowMapper must not be null");

ShardOperationProcessor<List> processor = this.getNonAggregationProcessor(rowMapper);
ShardOperationProcessor<List<T>> processor = this.getNonAggregationProcessor(rowMapper);
return this.process(sql, args, processor);
}

Expand Down Expand Up @@ -259,23 +258,22 @@ public List<Map<String, Object>> processOperations() {
return processor;
}

@SuppressWarnings("rawtypes")
private ShardOperationProcessor<List> getNonAggregationProcessor(final RowMapper rowMapper) {
final List<QueryCallable<List>> callableList = new ArrayList<ShardJdbcTemplate.QueryCallable<List>>();
private <T> ShardOperationProcessor<List<T>> getNonAggregationProcessor(final RowMapper<T> rowMapper) {
final List<QueryCallable<List<T>>> callableList = new ArrayList<ShardJdbcTemplate.QueryCallable<List<T>>>();

ShardOperationProcessor<List> processor = new ShardOperationProcessor<List>() {
ShardOperationProcessor<List<T>> processor = new ShardOperationProcessor<List<T>>() {
@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);
QueryCallable<List<T>> callable = new QueryCallable<List<T>>(shard, sql, args, extractor);

callableList.add(callable);
}

@Override
public List processOperations() {
List<List> rawResultList = ShardJdbcTemplate.this.executeQuery(callableList);
public List<T> processOperations() {
List<List<T>> rawResultList = ShardJdbcTemplate.this.executeQuery(callableList);
return AggregationUtil.aggregateObjectList(rawResultList);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AggregationUtil {

/**
* 基于Integer的聚合工具,通常用于update方法后计算总的affectedRows
* @param intsList
* @param intList
* @return
*/
public static Integer aggregateInteger(List<Integer> intList) {
Expand All @@ -34,11 +34,11 @@ public static Integer aggregateInteger(List<Integer> intList) {
* @param paramList
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List aggregateObjectList(List<List> paramList) {
List resultList = new ArrayList();
@SuppressWarnings({ "unchecked" })
public static <T> List<T> aggregateObjectList(List<List<T>> paramList) {
List<T> resultList = new ArrayList<T>();

for (List<?> list : paramList) {
for (List<T> list : paramList) {
resultList.addAll(list);
}

Expand Down

0 comments on commit 7528056

Please sign in to comment.