我使用LIKE
关键字查询带有单个搜索参数的表格。当我在MySQL Workbench中运行以下sql语句时,它按预期正常工作。
SET @search = 'B';
SELECT T.*,D.divisionname
FROM Township AS T INNER JOIN Division AS D ON T.divisionid=D.divisionid
WHERE T.townshipcode LIKE CONCAT('%', @search, '%') OR
T.townshipname LIKE CONCAT('%', @search, '%') OR
D.divisionname LIKE CONCAT('%', @search, '%')
ORDER BY T.townshipcode LIMIT 0,10
但是,当我使用JdbcTemplate
执行我的java代码时,我得到了BadSqlGrammarException
。以下是我的java代码:
public List<Township> getList(Integer pageNumber, Integer pageDisplayLength, String searchParameter) {
int start = ((pageNumber - 1) * pageDisplayLength);
String query = "";
if (null != searchParameter && !searchParameter.equals("")){
query = "SET @search = '" + searchParameter + "'; " +
"SELECT T.*,D.divisionname FROM Township AS T " +
"INNER JOIN Division AS D ON T.divisionid=D.divisionid " +
"WHERE T.townshipcode LIKE CONCAT('%', @search, '%') OR " +
"T.townshipname LIKE CONCAT('%', @search, '%') OR " +
"D.divisionname LIKE CONCAT('%', @search, '%') " +
"ORDER BY T.townshipcode LIMIT " + start + "," + pageDisplayLength;
}
else{
query = "SELECT T.*,D.divisionname FROM Township AS T " +
"INNER JOIN Division AS D ON T.divisionid=D.divisionid ORDER BY T.townshipcode LIMIT " + start + "," + pageDisplayLength;
}
List<Township> townshipList = getMultiRecords(query);
return townshipList;
}
以下是getMultiRecords
方法的基类:
protected List<T> getMultiRecords(String query){
List<T> listResult = jdbcTemplate.query(query, new RowMapper<T>(){
@Override
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
T entity = readRecord(rs);
return entity;
}
});
return listResult;
}
以下是我的BadSqlGrammarException
详细信息。
Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [SET @search = 'B'; SELECT T.*,D.divisionname FROM Township AS T INNER JOIN Division AS D ON T.divisionid=D.divisionid WHERE T.townshipcode LIKE CONCAT('%', @search, '%') OR T.townshipname LIKE CONCAT('%', @search, '%') OR D.divisionname LIKE CONCAT('%', @search, '%') ORDER BY T.townshipcode LIMIT 0,10]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT T.*,D.divisionname FROM Township AS T INNER JOIN Division AS D ON T.divis' at line 1
请帮我找出问题所在。
答案 0 :(得分:0)
我猜这个解析器对你在一个语句中使用的两个语句不满意。您应该使用parameter binding,而不是SET
并将所有参数添加到String
。如果不支持set语句,我不会感到惊讶。
你的陈述也错过了结束分号。由于您缺少确切/完整的错误消息,因此只能猜测。
答案 1 :(得分:0)
尝试将搜索特定查询更改为命名参数。并调用方法适当的参数。
query = "SELECT T.*,D.divisionname FROM Township AS T " +
"INNER JOIN Division AS D ON T.divisionid=D.divisionid " +
"WHERE T.townshipcode LIKE CONCAT('%', :search, '%') OR " +
"T.townshipname LIKE CONCAT('%', :search, '%') OR " +
"D.divisionname LIKE CONCAT('%', :search, '%') " +
"ORDER BY T.townshipcode LIMIT " + start + "," + pageDisplayLength;
Map<String, Object> queryParams = new HashMap<>();
queryParams.put("search", searchParameter);
更新方法代码以接受命名参数。
protected List<T> getMultiRecords(String query, Map<String,Object> namedParams){
List<T> listResult = new ArrayList();
if (namedParams != null && namedParams.size() > 0) {
listResult = jdbcTemplate.query(query, namedParams ,new RowMapper<T>(){
@Override
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
T entity = readRecord(rs);
return entity;
}
});
} else {
listResult = jdbcTemplate.query(query ,new RowMapper<T>(){
@Override
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
T entity = readRecord(rs);
return entity;
}
});
}
return listResult;
}