Loading

MyBatis报错 net.sf.jsqlparser.statement.select.SetOperationList and net.sf.jsqlparser.statement.select.PlainSelect are in unnamed module of loader 'app'

完整的错误信息

2023-01-09 14:33:29.823 ERROR 6892 --- [io-8004-exec-10] c.g.c.config.GlobalExceptionHandler      : >>> mybatis操作出现异常,请求号为:null,具体信息为:nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.ClassCastException: class net.sf.jsqlparser.statement.select.SetOperationList cannot be cast to class net.sf.jsqlparser.statement.select.PlainSelect (net.sf.jsqlparser.statement.select.SetOperationList and net.sf.jsqlparser.statement.select.PlainSelect are in unnamed module of loader 'app')
### Cause: java.lang.ClassCastException: class net.sf.jsqlparser.statement.select.SetOperationList cannot be cast to class net.sf.jsqlparser.statement.select.PlainSelect (net.sf.jsqlparser.statement.select.SetOperationList and net.sf.jsqlparser.statement.select.PlainSelect are in unnamed module of loader 'app')

当时的场景是执行简单的查询报错,SQL本身没有问题,拿出来单独运行不会报错,并且可以查询到结果,SQL如下

@Select("""
        SELECT DEPT_ID FROM TABLE_A WHERE USER_ID = #{userId}
        UNION
        SELECT DEPT_ID FROM TABLE_B WHERE USER_ID = #{userId}
        """)
List<String> findWellScope(@Param("userId") String userId);

经过测试发现是UNION关键字的问题,在外面套一层查询即可解决问题

@Select("""
        SELECT DEPT_ID FROM (
            SELECT DEPT_ID FROM TABLE_A WHERE USER_ID = #{userId}
            UNION
            SELECT DEPT_ID FROM TABLE_B WHERE USER_ID = #{userId}
        )
        """)
List<String> findWellScope(@Param("userId") String userId);
posted @ 2023-01-09 14:40  Java小学生丶  阅读(2772)  评论(0)    收藏  举报