我有一个案例,我通过一系列查询构建一个对象。对于优化,对象的构造被批处理,查询使用IN
子句来处理一批对象。
在下面的示例中,给定MyObject
列表,我想根据MyObject.c
和MyObject.a
的值填充MyObject.b
。使用skife.jdbi库,我的实现看起来如下所示。
有任何改进建议吗?
如果我解析原始结果集可能会更有效(虽然我无法找到如何让我的界面对结果集进行映射)?
如果有一些方法可以直接将查询参数映射到结果(但我还没有找到方法),那么它会更加整洁吗?
class MyObject {
String a;
String b;
String c;
// ....
String z;
}
void populateC(Set<MyObject> objects) {
// parse the list, populating Lists which will be used for the query and a
// Map which will be used to map query results back to the MyObjects in the Set param
Map<PopulateCQuerydata, MyObject> queryDataToParamMap = new HashMap<>;
List<String> aList = new ArrayList<>;
List<String> bList = new ArrayList<>;
for (MyObject object : objects) {
queryDataToParamMap.put(new CResult(object), object);
aList.add(object.a);
bList.add(object.b);
}
Set<CResult> results = dao.getC(aList, bList);
for (CResult result : results) {
queryDataToParamMap.get(result).c = result.c;
}
}
@RegisterMapper(CResultMapper.class)
@UseStringTemplate3StatementLocator("/sql/PopulateCDao.sql.stg")
interface PopulateCDao {
@SqlQuery
List<CResults> getC(@BindIn("aList") List<String> a, @BindIn("bList") List<String> b);
}
class CResults {
String a;
String b;
String c;
CResults(MyObject object) {
a = object.a;
b = object.b;
}
// implement these so this type can be used in the HashMap
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (!(other instanceof CResults )) {
return false;
} else {
CResults otherQueryData = (CResults)other;
return a.equals(otherQueryData.a) && b.equals(otherQueryData.b);
}
}
@Override
public int hashCode() {
int hash = 1;
hash = hash * 31 + a.hashCode() + b.hashCode();
return hash;
}
}
public class CResultMapper implements ResultSetMapper<CResult> {
@Override
public CResult map(int index, ResultSet resultSet, StatementContext statementContext) throws SQLException {
CResult result = new CResult();
result.a = resultSet.getInt("a");
result.b = resultSet.getInt("b");
result.c = resultSet.getInt("c");
return result;
}
}
//sql/PopulateCDao.sql.stg
group PopulateCDao;
getC(aList, bList) ::= <<
SELECT a, b, c FROM tblJoinC WITH (NOLOCK) WHERE a IN (<aList>) AND b IN (<bList>)
>>