java中使用pagehelper插件进行模糊查询时出现问题,问题如下
SELECT u.id,u.pwd,u.name,u.age,u.telephone,r.rule_name,p.project_name,u.state,u.create_time,
u.update_time
FROM user u
LEFT JOIN rule r ON u.rule_id = r.id
LEFT JOIN project p ON u.project_id = p.id
WHERE u.name LIKE CONCAT('%',?, '%')
OR u.id LIKE CONCAT('%',?, '%');
LIMIT ?
多出了一个 LIMIT ?并且报错
Cause: java.sql.SQLSyntaxErrorException: 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 'LIMIT 5' at line 16
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: 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 'LIMIT 5' at line 16
xml中代码
<select id="queryAllByIdName" resultMap="UserInfoMap" parameterType="string">
SELECT u.id,
u.pwd,
u.name,
u.age,
u.telephone,
r.rule_name,
p.project_name,
u.state,
u.create_time,
u.update_time
FROM user u
LEFT JOIN rule r ON u.rule_id = r.id
LEFT JOIN project p ON u.project_id = p.id
WHERE u.name LIKE CONCAT('%', #{value}, '%')
OR u.id LIKE CONCAT('%', #{value}, '%') ;
</select>
dao层代码
List<UserInfoVo> queryAllByIdName(String value);
server层代码
Map<String, Object> queryAllByIdName(String value,int currentPage, int pageSize);
对应的impl层代码
@Override
public Map<String, Object> queryAllByIdName(String value, int currentPage, int pageSize) {
PageHelper.startPage(currentPage, pageSize);
List<UserInfoVo> user = userDao.queryAllByIdName(value);
// 获取总页数 总条数
PageInfo<UserInfoVo> pageInfo = new PageInfo<>(user);
// 返回前端数据有 UserList total
HashMap<String, Object> map = new HashMap<>();
map.put("list", user);
map.put("total", pageInfo.getTotal());
return map;
}
controller层
@RequestMapping("/queryAllByIdName")
public R queryAllByIdName(Integer currentPage, Integer pageSize, String value) {
return R.ok(this.userService.queryAllByIdName(value,currentPage, pageSize));
}
报错一直都在
List<UserInfoVo> user = userDao.queryAllByIdName(value);
这个问题搞了好久,没想到在这里出问题,是startPage代码对sql语句的问题,如果startPage在代码写在查询语后面的话,分页不生效,但是报错消失了,写在前面就会报错,经过各种查询,发现是sql语句多了一个;,平时总是写这个符号,影响了分页插件的LIMIT ?语句,删除 ; 即可正常运行。