JDBCTemplate 传递带/多个参数
JBDCTemplate Pass w/ multiple parameters
事情是这样的...我无法通过以下代码传递以下语句 "this" 永远不会打印,因此结果集为 0,但查询似乎是正确的。
查询:
select * from opportunities where title = 1 and (zipcode = 11738 or zipcode = 11720 or zipcode = 11727 or zipcode = 11741 or zipcode = 11742 or zipcode = 11755 or zipcode = 11763 or zipcode = 11776 or zipcode = 11779 or zipcode = 11784 or zipcode = 11953)
上面的查询有 return 个结果。***
代码(只是切换了标题和邮政编码位置仍然会 return 0 结果当你 运行 代码)
public Opportunity[] getOpportunitiesBy(String title, String zipcode, double miles) {
title = ""+Constants.TITLES_MAP.get(title.toLowerCase());
String[] nearbyZipcodes = getZipcodesWithinRadius(zipcode, miles);
StringBuilder builder = new StringBuilder();
builder.append("(zipcode = "+zipcode+" or zipcode = ");
for(String otherZips : nearbyZipcodes) {
builder.append(otherZips+" or zipcode = ");
}
String formattedZips = Utilities.replaceLast(builder.toString(), " or zipcode = ", ")");
System.out.println(title+","+formattedZips);
List<Opportunity> opportunities = this.jdbcTemplate.query("select * from opportunities where ? and title = ?",
new Object[] { formattedZips, title}, new RowMapper<Opportunity>() {
public Opportunity mapRow(ResultSet rs, int rowNum) throws SQLException {
Opportunity temp = new Opportunity();
System.out.println("this");
String[] candidateIds = rs.getString("candidateIds").split(",");
temp.setCandidateIds(Utilities.StringToIntArray(candidateIds));
temp.setCompany(rs.getString("company"));
temp.setId(rs.getLong("id"));
temp.setHtml(rs.getString("post_data"));
temp.setZipcode(rs.getString("zipcode"));
temp.setTitle(rs.getInt("title"));
try {
temp.setLogoImg(new URI(rs.getString("logo_img")));
} catch (Exception e) {
}
return temp;
}
});
return opportunities.toArray(new Opportunity[opportunities.size()]);
}
初始 println(title+","+formattedZips) 的输出
1,(zipcode = 11738 or zipcode = 11720 or zipcode = 11727 or zipcode = 11741 or zipcode = 11742 or zipcode = 11755 or zipcode = 11763 or zipcode = 11776 or zipcode = 11779 or zipcode = 11784 or zipcode = 11953)
您的设置有两处错误。
首先,您不应该使用串联来创建(部分)查询,其次,这不是参数化查询的工作方式。参数在放入之前被转义,所以我怀疑查询是否符合您的预期。
SQL 有 in
子句而不是 zipcode or zipcode or zipcode
) 在查询中使用单个 in
子句。然而,当你想传入一个数组时,你又遇到了问题。要解决这个问题,请使用 NamedParameterJdbcTemplate
而不是普通的 JdbcTemplate
。然后重写您的查询以使用命名参数和 in
子句。
public Opportunity[] getOpportunitiesBy(String title, String zipcode, double miles) {
String sql = "select * from opportunities where title = :title and zipcode in (:zips)";
title = ""+Constants.TITLES_MAP.get(title.toLowerCase());
String[] nearbyZipcodes = getZipcodesWithinRadius(zipcode, miles);
Map<String, Object> params = new HashMap<>();
params.put("title", nearbyZipcodes);
params.put("zips", near)
return this.jdbcTemplate.query(sql, params, new RowMapper<Opportunity>() {
public Opportunity mapRow(ResultSet rs, int rowNum) throws SQLException {
Opportunity temp = new Opportunity();
System.out.println("this");
String[] candidateIds = rs.getString("candidateIds").split(",");
temp.setCandidateIds(Utilities.StringToIntArray(candidateIds));
temp.setCompany(rs.getString("company"));
temp.setId(rs.getLong("id"));
temp.setHtml(rs.getString("post_data"));
temp.setZipcode(rs.getString("zipcode"));
temp.setTitle(rs.getInt("title"));
try {
temp.setLogoImg(new URI(rs.getString("logo_img")));
} catch (Exception e) {
}
return temp;
});
}
类似的东西应该可以解决问题,但是如果您的 getZipCodesWithinRadius
也在使用查询,您甚至可以将其用作 in 子句中的子 select 并简单地传递在给定的 zipcode
和 miles
中,这样您将有一个查询一次性获得结果(而不是 2 个查询和所有 jdbc 的东西)。
事情是这样的...我无法通过以下代码传递以下语句 "this" 永远不会打印,因此结果集为 0,但查询似乎是正确的。
查询:
select * from opportunities where title = 1 and (zipcode = 11738 or zipcode = 11720 or zipcode = 11727 or zipcode = 11741 or zipcode = 11742 or zipcode = 11755 or zipcode = 11763 or zipcode = 11776 or zipcode = 11779 or zipcode = 11784 or zipcode = 11953)
上面的查询有 return 个结果。***
代码(只是切换了标题和邮政编码位置仍然会 return 0 结果当你 运行 代码)
public Opportunity[] getOpportunitiesBy(String title, String zipcode, double miles) {
title = ""+Constants.TITLES_MAP.get(title.toLowerCase());
String[] nearbyZipcodes = getZipcodesWithinRadius(zipcode, miles);
StringBuilder builder = new StringBuilder();
builder.append("(zipcode = "+zipcode+" or zipcode = ");
for(String otherZips : nearbyZipcodes) {
builder.append(otherZips+" or zipcode = ");
}
String formattedZips = Utilities.replaceLast(builder.toString(), " or zipcode = ", ")");
System.out.println(title+","+formattedZips);
List<Opportunity> opportunities = this.jdbcTemplate.query("select * from opportunities where ? and title = ?",
new Object[] { formattedZips, title}, new RowMapper<Opportunity>() {
public Opportunity mapRow(ResultSet rs, int rowNum) throws SQLException {
Opportunity temp = new Opportunity();
System.out.println("this");
String[] candidateIds = rs.getString("candidateIds").split(",");
temp.setCandidateIds(Utilities.StringToIntArray(candidateIds));
temp.setCompany(rs.getString("company"));
temp.setId(rs.getLong("id"));
temp.setHtml(rs.getString("post_data"));
temp.setZipcode(rs.getString("zipcode"));
temp.setTitle(rs.getInt("title"));
try {
temp.setLogoImg(new URI(rs.getString("logo_img")));
} catch (Exception e) {
}
return temp;
}
});
return opportunities.toArray(new Opportunity[opportunities.size()]);
}
初始 println(title+","+formattedZips) 的输出
1,(zipcode = 11738 or zipcode = 11720 or zipcode = 11727 or zipcode = 11741 or zipcode = 11742 or zipcode = 11755 or zipcode = 11763 or zipcode = 11776 or zipcode = 11779 or zipcode = 11784 or zipcode = 11953)
您的设置有两处错误。
首先,您不应该使用串联来创建(部分)查询,其次,这不是参数化查询的工作方式。参数在放入之前被转义,所以我怀疑查询是否符合您的预期。
SQL 有 in
子句而不是 zipcode or zipcode or zipcode
) 在查询中使用单个 in
子句。然而,当你想传入一个数组时,你又遇到了问题。要解决这个问题,请使用 NamedParameterJdbcTemplate
而不是普通的 JdbcTemplate
。然后重写您的查询以使用命名参数和 in
子句。
public Opportunity[] getOpportunitiesBy(String title, String zipcode, double miles) {
String sql = "select * from opportunities where title = :title and zipcode in (:zips)";
title = ""+Constants.TITLES_MAP.get(title.toLowerCase());
String[] nearbyZipcodes = getZipcodesWithinRadius(zipcode, miles);
Map<String, Object> params = new HashMap<>();
params.put("title", nearbyZipcodes);
params.put("zips", near)
return this.jdbcTemplate.query(sql, params, new RowMapper<Opportunity>() {
public Opportunity mapRow(ResultSet rs, int rowNum) throws SQLException {
Opportunity temp = new Opportunity();
System.out.println("this");
String[] candidateIds = rs.getString("candidateIds").split(",");
temp.setCandidateIds(Utilities.StringToIntArray(candidateIds));
temp.setCompany(rs.getString("company"));
temp.setId(rs.getLong("id"));
temp.setHtml(rs.getString("post_data"));
temp.setZipcode(rs.getString("zipcode"));
temp.setTitle(rs.getInt("title"));
try {
temp.setLogoImg(new URI(rs.getString("logo_img")));
} catch (Exception e) {
}
return temp;
});
}
类似的东西应该可以解决问题,但是如果您的 getZipCodesWithinRadius
也在使用查询,您甚至可以将其用作 in 子句中的子 select 并简单地传递在给定的 zipcode
和 miles
中,这样您将有一个查询一次性获得结果(而不是 2 个查询和所有 jdbc 的东西)。