当前使用版本(必须填写清楚,否则不予处理)

v3.1.2

该问题是怎么引起的?(最新版上已修复的会直接close掉)

SelectBody强转PlainSelect不支持sql里面带union

重现步骤

  1. 使用分页 IPage<User> selectPageVo(Page page, @Param("state") Integer state);
  2. 自定义xml,select里面带union关键字
  <select id="selectPageVo" resultType="com.baomidou.cloud.entity.UserVo">
      SELECT * FROM user union SELECT * FROM user
  </select>
  1. 另一种重现
    @Test
    void testSelectBodyToPlainSelectThrowException() {
        Select selectStatement = null;
        String originalUnionSql = "select * from test union select * from test";
        try {
            selectStatement = (Select) CCJSqlParserUtil.parse(originalUnionSql);
        } catch (JSQLParserException e) {
            e.printStackTrace();
        }
        assert selectStatement != null;
        try {
            PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();
            assert false;
        } catch (Exception e) {
            assertThat(e.getMessage()).isEqualTo("net.sf.jsqlparser.statement.select.SetOperationList cannot be cast to net.sf.jsqlparser.statement.select.PlainSelect");
        }
    }
  1. 临时解决方法,在sql外层加入select
    @Test
    void testResoleSelectBodyToPlainSelectThrowException() {
        String originalSql = "select * from (select * from test union select * from test) as temp";

        Select selectStatement = null;
        try {
            selectStatement = (Select) CCJSqlParserUtil.parse(originalSql);
        } catch (JSQLParserException e) {
            e.printStackTrace();
        }
        assert selectStatement != null;

        try {
            PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();
            assertThat(plainSelect.toString())
                .isEqualTo("SELECT * FROM (SELECT * FROM test UNION SELECT * FROM test) AS temp");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

报错信息

The error may involve defaultParameterMap

The error occurred while setting parameters

Cause: java.lang.ClassCastException: net.sf.jsqlparser.statement.select.SetOperationList cannot be cast to net.sf.jsqlparser.statement.select.PlainSelect] with root cause

java.lang.ClassCastException: net.sf.jsqlparser.statement.select.SetOperationList cannot be cast to net.sf.jsqlparser.statement.select.PlainSelect at com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor.concatOrderBy(PaginationInterceptor.java:116) at com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor.intercept(PaginationInterceptor.java:199) at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61) at com.sun.proxy.$Proxy165.prepare(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63) at com.sun.proxy.$Proxy165.prepare(Unknown Source) at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.prepareStatement(MybatisSimpleExecutor.java:94) at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doQuery(MybatisSimpleExecutor.java:66) at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324) at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) at com.sun.proxy.$Proxy86.selectList(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForIPage(MybatisMapperMethod.java:115)

Comment From: qmdx

merged

Comment From: huazengguang

v3.4.1 union all 还是会出现此问题

Comment From: 2013108827

给后来的人提个醒,不要在IPage page = new Page(current, limit)之后,再去page.setSize(),会引发和题主一样的报错。至少v3.4.3版本会出现这种报错