Since 3.4.1 (less or more), mybatis-plus bring in a bug: when parsing extra whitespaces in SQL, it throws net.sf.jsqlparser.parser.ParseException
. I solved the problem by finding relevant discussions and resources to gain insights into the issue.
Background
Mybatis-Plus is a powerful Java framework that provides enhanced functionalities on top of Mybatis, simplifying data access operations.
In a project named service-a
, we encountered the problem when parsing SQL statements that contain whitespace lines. The problem lead to unexpected errors during query execution, resulting in frustration and delays in the development process.
Related error log:
net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "\n\n\n" <ST_SEMICOLON>
Troubleshooting
First, I analyzed the exception stack trace, the log tells me the error was thrown when dao framework parsing sql which contains whitespaces. The log highlights an unexpected token consisting of three consecutive newlines encountered while attempting to parse the SQL statement.
And then I asked my colleagues if they had encountered similar error messages. It turns out no one I asked have seen this problem before.
So I just google the log message.
Google leads me to related issues that shed light on potential solutions:
- https://github.com/JSQLParser/JSqlParser/issues/1756
- https://github.com/baomidou/mybatis-plus/issues/5345
These discussions provided crucial information and suggestions from the community, showcasing different approaches to tackle the problem.
Solution
Temporary
By adjusting the mybatis-plus configuration, specifically the shrink-whitespaces-in-sql property, we were able to mitigate the problem.
|
|
Implementing this temporary solution offered relief, enabling successful parsing of SQL statements despite the presence of whitespace lines.
Long-term
While the temporary solution resolves the immediate issue, it’s important to track the progress of the related issues and stay updated on potential permanent fixes provided by the mybatis-plus and JSqlParser communities.
At the same time, I submitted feedback to the internal infrastructure team of the company(Keboot) to help them resolve this issue through iterative updates.
Conclusion
In this blog post, we explored a issue related to mybatis-plus and the parsing of SQL statements containing whitespace lines. By identifying the error message, researching related discussions, and implementing a temporary solution, we were able to overcome the problem and continue with the development process.
In the long run, we need to stay engaged with the community, keep an eye on related issues, and apply upcoming fixes to ensure a robust and seamless experience with mybatis-plus (rely on the infrastructure team).