net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: “)“ “)“ at line 3, column
在使用mybatis-plus进行条件查询时,遇到一个需求:使用一个输入框查询多个属性。乍一看这不挺简单的嘛,于是一通操作代码如下:LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<Student>();queryWrapper.and(wrapper -> wrapper.like(S
·
在使用mybatis-plus进行条件查询时,遇到一个需求:使用一个输入框查询多个属性。乍一看这不挺简单的嘛,于是一通操作代码如下:
LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<Student>();
queryWrapper.and(wrapper -> wrapper.like(Strings.isNotEmpty(student.getStudentName()),Student::getStudentName,student.getStudentName())
.or().like(Strings.isNotEmpty(student.getStudentName()),Student::getStudentNumber, student.getStudentName()));
然后结果尴尬了:
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. 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 ') ORDER BY student_id ASC) TOTAL' at line 3
### The error may exist in com/chaopei/cxjw/mapper/StudentMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT COUNT(*) FROM (SELECT student_id,student_number,student_name,student_sex,faculty_id,major_id,class_id,grade_id,isdel FROM student WHERE () ORDER BY student_id ASC) TOTAL
### 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 ') ORDER BY student_id ASC) TOTAL' at line 3
; 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 ') ORDER BY student_id ASC) TOTAL' at line 3
但是去页面测试了一下发现虽然报错但是功能已经实现了,继续往上翻错误信息还有一段
WHERE () ORDER BY student_id ASC", exception:
net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: ")" ")"
at line 3, column 9.
Was expecting one of:
"!"
"("
"NOT"
百度了好多案例清一色都是说mybatis-plus的多租户过滤的问题,像下面给的方案,可是我mapper层只实现了mybatis-plus接口并没有写一个自己的方法,试了一大堆根本不管用。
最后,静下心来仔细分析了错误信息,Encountered unexpected token: ")" ")"这不就是说多了两个括号的问题么,想想突然明白了,把拼接条件的判断语句写在里面,当条件为空时虽然逻辑上正确不会执行,但是拼接的语句()却保留在了里面,而单个条件时就不会有这种情况,于是修改代码如下:
//多条件查询判断要写外面,写在里面会报错!!
if(Strings.isNotEmpty(student.getStudentName())){
queryWrapper.and(wrapper -> wrapper.like(Student::getStudentName,student.getStudentName())
.or().like(Student::getStudentNumber, student.getStudentName()));
}
成功运行!!!
==> Preparing: SELECT student_id,student_number,student_name,student_sex,faculty_id,major_id,class_id,grade_id,isdel FROM student WHERE ((student_name LIKE ? OR student_number LIKE ?) AND isdel = ?) ORDER BY student_id ASC LIMIT ?
==> Parameters: %测试%(String), %测试%(String), 0(Integer), 10(Long)
<== Columns: student_id, student_number, student_name, student_sex, faculty_id, major_id, class_id, grade_id, isdel
<== Row: 8, 20220006, 测试, 女, 1, 2, 2, 3, 0
<== Row: 9, 20220007, 测试, 女, 1, 2, 1, 1, 0
<== Row: 11, 20220009, 测试, 女, 1, 3, 2, 2, 0
<== Total: 3
总结:条件判断一定要在外层做判断。欢迎分享哦~
更多推荐
已为社区贡献1条内容
所有评论(0)