net.sf.jsqlparser.parser.ParseException: 遇到意外的标记:”@”

huangapple go评论67阅读模式
英文:

net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "@" "@"

问题

在我的SQL语句中添加变量语句时,类似于这样在MyBatis中:

set @update_id := 0;

这是完整的语句:

set @update_id := 0;
UPDATE r_room_seat s
SET s.status = 1,
    s.user_id = (
        CASE WHEN s.seat_num = 1
            THEN #{user1.id,jdbcType=BIGINT}
            WHEN s.seat_num = 2
            THEN #{user2.id,jdbcType=BIGINT}
            ELSE -1
        END
    ),
    s.robot_flag = (
        CASE WHEN s.seat_num = 1
            THEN #{user1.isRobot,jdbcType=INTEGER}
            WHEN s.seat_num = 2
            THEN #{user2.isRobot,jdbcType=INTEGER}
            ELSE 0
        END
    )
WHERE s.status = 0
    AND s.online = 1
    AND s.tenant_id = #{queryParam.tenantId,jdbcType=BIGINT}
    AND s.room_play_id IN (
        SELECT room_play_id
        FROM (
            SELECT room_play_id AS room_play_id
            FROM r_room_seat
            WHERE status = 0
                AND app_id = 4
                AND tenant_id = #{queryParam.tenantId,jdbcType=BIGINT}
            GROUP BY room_play_id
            HAVING COUNT(*) = 2
            LIMIT 1
        ) a
    )
    AND s.room_id = #{queryParam.roomTypeId,jdbcType=BIGINT}
LIMIT 2;
SELECT
    <include refid="Base_Column_List"/>
FROM r_room_seat
WHERE room_play_id = 5
LIMIT 1;

但是会抛出以下错误:

### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Failed to process, please exclude the tableName or statementId.
Error SQL: set @update_id := 0;
        UPDATE r_room_seat s
        ...
Caused by: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Failed to process, please exclude the tableName or statementId.
Error SQL: set @update_id := 0;
        UPDATE r_room_seat s
        ...

我使用的包是:

api "com.baomidou:mybatis-plus-boot-starter:3.2.0"

我该如何解决这个问题?MyBatis-Plus 不支持用户变量吗?

英文:

When I add variable statement like this in my SQL in mybatis:

set @update_id := 0;

this is my full statement:

set @update_id := 0;
        UPDATE r_room_seat s
        SET s.status = 1,
        s.user_id = (
            case when s.seat_num = 1
            then #{user1.id,jdbcType=BIGINT}
            when s.seat_num = 2
            then #{user2.id,jdbcType=BIGINT}
            else -1 end
        ),
        s.robot_flag = (
            case when s.seat_num = 1
            then #{user1.isRobot,jdbcType=INTEGER}
            when s.seat_num = 2
            then #{user2.isRobot,jdbcType=INTEGER}
            else 0 end
        )
        WHERE s.status = 0
        and s.online = 1
        and s.tenant_id = #{queryParam.tenantId,jdbcType=BIGINT}
        and s.room_play_id in (
            select room_play_id
            from (
                select room_play_id as room_play_id
                from r_room_seat
                where status = 0
                and app_id = 4
                and tenant_id = #{queryParam.tenantId,jdbcType=BIGINT}
                group by room_play_id
                having count(*) = 2
                limit 1
            ) a
        )
        and s.room_id = #{queryParam.roomTypeId,jdbcType=BIGINT}
        LIMIT 2;
        select
        &lt;include refid=&quot;Base_Column_List&quot;/&gt;
        from r_room_seat
        where room_play_id = 5
        limit 1;

but throw this error:

### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Failed to process, please exclude the tableName or statementId.
Error SQL: set @update_id := 0;
UPDATE r_room_seat s
SET s.status = 1,
room_play_id = (SELECT @update_id := max(room_play_id)),
s.user_id = (
case when s.seat_num = 1
then ?
when s.seat_num = 2
then ?
else -1 end
),
s.robot_flag = (
case when s.seat_num = 1
then ?
when s.seat_num = 2
then ?
else 0 end
)
WHERE s.status = 0
and s.online = 1
and s.room_play_id in (
select room_play_id
from (
select room_play_id as room_play_id
from r_room_seat
where status = 0
and app_id = 4
group by room_play_id
having count(*) = 2
limit 1
) a
)
and s.room_id = ?
LIMIT 2;
select
id, room_play_id, room_id, user_id, creator, recent_active, deleted, sort, updated_time,
created_time, `status`, robot_flag, app_id, app_mark, `online`, version
from r_room_seat
where room_play_id = @update_id
limit 1;
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) ~[mybatis-3.5.2.jar!/:3.5.2]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149) ~[mybatis-3.5.2.jar!/:3.5.2]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.2.jar!/:3.5.2]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:76) ~[mybatis-3.5.2.jar!/:3.5.2]
at sun.reflect.GeneratedMethodAccessor358.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_252]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_252]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426) ~[mybatis-spring-2.0.2.jar!/:2.0.2]
... 114 common frames omitted
Caused by: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Failed to process, please exclude the tableName or statementId.
Error SQL: set @update_id := 0;
UPDATE r_room_seat s
SET s.status = 1,
room_play_id = (SELECT @update_id := max(room_play_id)),
s.user_id = (
case when s.seat_num = 1
then ?
when s.seat_num = 2
then ?
else -1 end
),
s.robot_flag = (
case when s.seat_num = 1
then ?
when s.seat_num = 2
then ?
else 0 end
)
WHERE s.status = 0
and s.online = 1
and s.room_play_id in (
select room_play_id
from (
select room_play_id as room_play_id
from r_room_seat
where status = 0
and app_id = 4
group by room_play_id
having count(*) = 2
limit 1
) a
)
and s.room_id = ?
LIMIT 2;
select
id, room_play_id, room_id, user_id, creator, recent_active, deleted, sort, updated_time,
created_time, `status`, robot_flag, app_id, app_mark, `online`, version
from r_room_seat
where room_play_id = @update_id
limit 1;
at com.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:39) ~[mybatis-plus-core-3.2.0.jar!/:3.2.0]
at com.baomidou.mybatisplus.core.parser.AbstractJsqlParser.parser(AbstractJsqlParser.java:74) ~[mybatis-plus-core-3.2.0.jar!/:3.2.0]
at com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler.sqlParser(AbstractSqlParserHandler.java:76) ~[mybatis-plus-extension-3.2.0.jar!/:3.2.0]
at com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor.intercept(PaginationInterceptor.java:155) ~[mybatis-plus-extension-3.2.0.jar!/:3.2.0]
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61) ~[mybatis-3.5.2.jar!/:3.5.2]
at com.sun.proxy.$Proxy169.prepare(Unknown Source) ~[na:na]
at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.prepareStatement(MybatisSimpleExecutor.java:94) ~[mybatis-plus-core-3.2.0.jar!/:3.2.0]
at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doQuery(MybatisSimpleExecutor.java:66) ~[mybatis-plus-core-3.2.0.jar!/:3.2.0]
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324) ~[mybatis-3.5.2.jar!/:3.5.2]
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) ~[mybatis-3.5.2.jar!/:3.5.2]
at sun.reflect.GeneratedMethodAccessor201.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_252]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_252]
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63) ~[mybatis-3.5.2.jar!/:3.5.2]
at com.sun.proxy.$Proxy168.query(Unknown Source) ~[na:na]
at sun.reflect.GeneratedMethodAccessor201.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_252]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_252]
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63) ~[mybatis-3.5.2.jar!/:3.5.2]
at com.sun.proxy.$Proxy168.query(Unknown Source) ~[na:na]
at sun.reflect.GeneratedMethodAccessor201.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_252]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_252]
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63) ~[mybatis-3.5.2.jar!/:3.5.2]
at com.sun.proxy.$Proxy168.query(Unknown Source) ~[na:na]
at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:108) ~[pagehelper-5.1.11.jar!/:na]
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61) ~[mybatis-3.5.2.jar!/:3.5.2]
at com.sun.proxy.$Proxy168.query(Unknown Source) ~[na:na]
at sun.reflect.GeneratedMethodAccessor205.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_252]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_252]
at org.apache.ibatis.plugin.Invocation.proceed(Invocation.java:49) ~[mybatis-3.5.2.jar!/:3.5.2]
at tk.mybatis.orderbyhelper.OrderByHelper.intercept(OrderByHelper.java:115) ~[orderby-helper-0.0.2.jar!/:na]
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61) ~[mybatis-3.5.2.jar!/:3.5.2]
at com.sun.proxy.$Proxy168.query(Unknown Source) ~[na:na]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147) ~[mybatis-3.5.2.jar!/:3.5.2]
... 120 common frames omitted
Caused by: net.sf.jsqlparser.JSQLParserException: null
at net.sf.jsqlparser.parser.CCJSqlParserUtil.parseStatements(CCJSqlParserUtil.java:128) ~[jsqlparser-2.1.jar!/:na]
at com.baomidou.mybatisplus.core.parser.AbstractJsqlParser.parser(AbstractJsqlParser.java:60) ~[mybatis-plus-core-3.2.0.jar!/:3.2.0]
... 154 common frames omitted
Caused by: net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: &quot;@&quot; &quot;@&quot;
at line 1, column 5.
Was expecting one of:
&quot;ACTION&quot;
&quot;ANY&quot;
&quot;BYTE&quot;
&quot;CASCADE&quot;
&quot;CAST&quot;
&quot;CHAR&quot;
&quot;COLUMN&quot;
&quot;COLUMNS&quot;
&quot;COMMENT&quot;
&quot;COMMIT&quot;
&quot;DESCRIBE&quot;
&quot;DO&quot;
&quot;DOUBLE&quot;
&quot;ENABLE&quot;
&quot;END&quot;
&quot;EXTRACT&quot;
&quot;FIRST&quot;
&quot;FN&quot;
&quot;FOLLOWING&quot;
&quot;IF&quot;
&quot;INDEX&quot;
&quot;INSERT&quot;
&quot;INTERVAL&quot;
&quot;ISNULL&quot;
&quot;KEY&quot;
&quot;LAST&quot;
&quot;LEFT&quot;
&quot;MATERIALIZED&quot;
&quot;NEXTVAL&quot;
&quot;NO&quot;
&quot;NULLS&quot;
&quot;OPEN&quot;
&quot;OPTIMIZE&quot;
&quot;OVER&quot;
&quot;PARTITION&quot;
&quot;PATH&quot;
&quot;PERCENT&quot;
&quot;PRECISION&quot;
&quot;PRIMARY&quot;
&quot;PRIOR&quot;
&quot;RANGE&quot;
&quot;REPLACE&quot;
&quot;RIGHT&quot;
&quot;ROW&quot;
&quot;ROWS&quot;
&quot;SEPARATOR&quot;
&quot;SET&quot;
&quot;SIBLINGS&quot;
&quot;TABLE&quot;
&quot;TEMP&quot;
&quot;TEMPORARY&quot;
&quot;TOP&quot;
&quot;TRUNCATE&quot;
&quot;TYPE&quot;
&quot;UNSIGNED&quot;
&quot;VALUE&quot;
&quot;VALUES&quot;
&quot;XML&quot;
&quot;ZONE&quot;
&lt;K_DATETIMELITERAL&gt;
&lt;K_DATE_LITERAL&gt;
&lt;S_IDENTIFIER&gt;
&lt;S_QUOTED_IDENTIFIER&gt;
at net.sf.jsqlparser.parser.CCJSqlParser.generateParseException(CCJSqlParser.java:20951) ~[jsqlparser-2.1.jar!/:na]
at net.sf.jsqlparser.parser.CCJSqlParser.jj_consume_token(CCJSqlParser.java:20798) ~[jsqlparser-2.1.jar!/:na]
at net.sf.jsqlparser.parser.CCJSqlParser.RelObjectNameExt(CCJSqlParser.java:3033) ~[jsqlparser-2.1.jar!/:na]
at net.sf.jsqlparser.parser.CCJSqlParser.Set(CCJSqlParser.java:612) ~[jsqlparser-2.1.jar!/:na]
at net.sf.jsqlparser.parser.CCJSqlParser.SingleStatement(CCJSqlParser.java:185) ~[jsqlparser-2.1.jar!/:na]
at net.sf.jsqlparser.parser.CCJSqlParser.Statements(CCJSqlParser.java:466) ~[jsqlparser-2.1.jar!/:na]
at net.sf.jsqlparser.parser.CCJSqlParserUtil.parseStatements(CCJSqlParserUtil.java:126) ~[jsqlparser-2.1.jar!/:na]
... 155 common frames omitted

this is the package I am using:

 api &quot;com.baomidou:mybatis-plus-boot-starter:3.2.0&quot;

what should I do to fix it? mybatis-plus not support user variable?

答案1

得分: 1

这对我来说似乎是一个依赖性问题。这是我在旧版本的jsqlparser库上运行的查询:

"SELECT COLUMN FROM OLDTABLE a GROUP BY stringcol = 'AAA'"

旧版本的jsqlParser会抛出JSQLParserException错误:

在执行SQL时出错"SELECT stringCol = 'AAA' from DATASET group by stringCol = 'AAA'": 远程驱动程序错误:JDBCException: 解析SQL错误'SELECT stringCol = 'AAA' from DATASET group by stringCol = 'AAA'' -> JSQLParserException:遇到意外的标记:"=" "="

将jsqlParser库更新到最新版本,从'com.github.jsqlparser:jsqlparser:4.0'更新到'com.github.jsqlparser:jsqlparser:4.4'后,错误得到解决。

你可以在这个仓库链接中找到他们的发布文档:https://github.com/JSQLParser/JSqlParser

英文:

This seemed to be a dependency issue for me. This is the query I ran with the older version of the jsqlparser library:

&quot;SELECT COLUMN FROM OLDTABLE a GROUP BY stringcol = &#39;AAA&#39;&quot;

The older version of the jsqlParser, throws the JSQLParserException error:

Error while executing SQL &quot;SELECT stringCol = &#39;AAA&#39; from DATASET group by stringCol = &#39;AAA&#39;&quot;: Remote driver error: JDBCException: Error parsing SQL &#39;SELECT stringCol = &#39;AAA&#39; from DATASET group by stringCol = &#39;AAA&#39;&#39; -&gt; JSQLParserException: Encountered unexpected token: &quot;=&quot; &quot;=&quot;&#39;. 

After updating the jsqlParser library to the latest version from 'com.github.jsqlparser:jsqlparser:4.0' to 'com.github.jsqlparser:jsqlparser:4.4'. The error was resolved.

Here is a link to the repo where you can find their release docs - https://github.com/JSQLParser/JSqlParser

答案2

得分: 0

@SqlParser(filter = true)
RoomSeat selectMultiRoomSeatForUpdateEnhance(@Param("queryParam") Map<String, Object> queryParam,
                                             @Param("user1") User user1,
                                             @Param("user2") User user2);
fix it.
英文:

add sqlparse filter in your mapper:

@SqlParser(filter = true)
RoomSeat selectMultiRoomSeatForUpdateEnhance(@Param(&quot;queryParam&quot;) Map&lt;String, Object&gt; queryParam,
@Param(&quot;user1&quot;) User user1,
@Param(&quot;user2&quot;) User user2);

fix it.

huangapple
  • 本文由 发表于 2020年9月22日 12:04:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64002927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定