请用
public List listByBreedIdAndGender(long breedId, String gender) {
return jdbcTemplate.query("SELECT * FROM dog_entity WHERE breed__id = :breedId AND gender =:gender",
new MapSqlParameterSource()
.addValue("breedId", breedId)
.addValue("gender", gender));
}
public List listByBreedIdAndGender(long breedId, String gender) {
return jdbcTemplate.query
("SELECT * FROM dog_entity WHERE breed__id = ? AND gender = ?",
new Object[] { breedId, gender },
new DogRowMapper());
}
或者如果您坚持使用私有查询方法
public List listByBreedIdAndGender(long breedId, String gender) {
return query
("SELECT * FROM dog_entity WHERE breed__id = ? AND gender = ?",
new Object[] { breedId, gender });
}
private List query(String sql, Object[] parameters) {
List dogs = jdbcTemplate.query(sql, parameters, new DogRowMapper());
return dogs;
}
请确保breed__id具有正确的_字符数.
概念是使用NamedParameterJdbcTemplate和参数指定的名称:name(例如:gender)或带有位置参数的简单JdbcTemplate(例如new Object [] {breedId,gender}其中breedId匹配第一个?性别是第二个?) .