java - 在spring jdbcTemplate中选择查询where in子句问题
问题描述
公共类 CountryMasterModel { 私有字符串 countryNames[];
public String[] getCountryNames() {
return countryNames;
}
public void setCountryNames(String[] countryNames) {
this.countryNames = countryNames;
}
}
公共接口 CountryMasterService {
List<String> getCountryData(String compCode, CountryMasterModel model) throws SQLException, DataAccessException;
}
@Component 公共类 CountryMasterDAO 实现 CountryMasterService{
@Autowired
private JdbcTemplate template;
@Override
public List<String> getCountryData(CountryMasterModel model) throws SQLException, DataAccessException {
this.template = new JdbcTemplate(DBMSSQL.getDBCon());
System.out.println(template.getDataSource().getConnection().getCatalog());
List<String> ids = Arrays.asList(model.getCountryNames());
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("countryName", ids);
List<String> list = new ArrayList<String>();
list = this.template.query("SELECT NAME FROM country_master WHERE NAME IN (?)", new Object[] { ids },
new CountryMasterResultSetExtrator());
System.out.println("ids = " + ids);
System.out.println("list = " + list.get(0));
return list;
}
public static void main(String[] args) throws DataAccessException, SQLException {
CountryMasterModel bean = new CountryMasterModel();
String a[] = { "India", "Pakistan", "Russia", "America",};
bean.setCountryNames(a);
System.out.println(StringUtil.getStringArray(a));
List<String> list = new CountryMasterDAO().getCountryData(bean);
System.out.println("List Size = "+list.size());
}
}
解决方案
检查这一行:
list = this.template.query(
"SELECT NAME FROM country_master WHERE NAME IN (?)",
new Object[] { ids },
new CountryMasterResultSetExtrator()
);
NAME IN (?)
需要一个参数并且new Object[] { ids }
有 3 个参数。尝试这个NAME IN (?,?,?)
查看此链接以获取更多信息: https ://www.baeldung.com/spring-jdbctemplate-in-list
推荐阅读
- python - 将不同 Python 模块中的函数导入 Apache Spark
- scheme - 在球拍方案或其他方案中解码十六进制格式字符串的惯用方法
- docker - 无法在 docker 图像中看到图像名称和标签
- azure - 将数据插入 cosmosdb 时,有没有办法增加请求大小限制?
- docker-compose - Kong:docker-compose [PostgreSQL 错误] 无法检索 PostgreSQL server_version_num:主机或服务未提供,或未知
- sql - 即使在使用 to_date(to_char(JOB_CLOSED_DATE,'dd-mon-yy')) 之后,日期列仍显示重复记录,
- c# - 用于在 Build Pipeline VSTS 中添加 timeoutinminutes 的 Yaml 脚本
- ruby-on-rails - Rails 渲染 HTML 也会在相同的操作中下载 CSV 文件
- html - 保存/恢复会影响 HTML5 画布中的 clip() 吗?
- rxjs - RxJS 在属性中的对象数组上不同