如何在 JDBCTemplate 中使用 like 语句检索列表?
how to retrieve a list using like statement in JDBCTemplate ?
我尝试 return 包含已输入单词的数据列表,但它 return 为空
public List<people> peoples(String l) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
try {
String sql = "select * from people where peopleName like '?' ;";
List<people> peoples = jdbcTemplate.query(sql, new Object[]{"%"+l+"%"},
new BeanPropertyRowMapper<>(people.class));
return peoples;
} catch (DataAccessException e) {
}catch(NullPointerException nullPointer) {
}
return null;
}
请检查数据源是否正确实例化。
使用
select * from people where peopleName like ?
而不是
select * from people where peopleName like '?'
不要在占位符值前后使用“'”值吗? .它总是像这样搜索人名?不符合你的价值
我尝试 return 包含已输入单词的数据列表,但它 return 为空
public List<people> peoples(String l) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
try {
String sql = "select * from people where peopleName like '?' ;";
List<people> peoples = jdbcTemplate.query(sql, new Object[]{"%"+l+"%"},
new BeanPropertyRowMapper<>(people.class));
return peoples;
} catch (DataAccessException e) {
}catch(NullPointerException nullPointer) {
}
return null;
}
请检查数据源是否正确实例化。
使用
select * from people where peopleName like ?
而不是
select * from people where peopleName like '?'
不要在占位符值前后使用“'”值吗? .它总是像这样搜索人名?不符合你的价值