公共类CountryMasterModel {private String 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 (?)
需要1个参数,new Object[] { ids }
需要3个参数。试试这个NAME IN (?,?,?)
查看此链接以获取更多信息:https://www.baeldung.com/spring-jdbctemplate-in-list