MyBatis java 和 MySql 局部变量

发布于 2024-10-19 14:29:27 字数 1327 浏览 2 评论 0原文

我是java世界的新手。我有一个简单查询的问题:

<insert id="create" parameterType="models.entities.CategoryEntity">

    set @catId := (select categoryId from Categories limit 1);
     insert into Categories(CategoryId, Title, LeftValue, RightValue)
    values(@catId, 'Test in', 1,2);
   ....
</insert>

当我尝试使用 mybatis 运行它时,它会失败:

PersistenceException occured : ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'insert into Categories(CategoryId, Title, LeftValue, RightValue) values(' at line 2 ### The error may involve Category.create-Inline ### The error occurred while setting parameters ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'insert into Categories(CategoryId, Title, LeftValue, RightValue) values(' at line 2

如果我删除这一行:

set @catId := (select categoryId from Categories limit 1);

那么一切都会好的。我做错了什么?是jdbc还是mybatis的问题?如何在 mybatis 中使用 mysql @variables?有人有在 mybatis 中使用 MySql 局部变量的示例吗?

I'm new to java world. And I have a problem with simple query:

<insert id="create" parameterType="models.entities.CategoryEntity">

    set @catId := (select categoryId from Categories limit 1);
     insert into Categories(CategoryId, Title, LeftValue, RightValue)
    values(@catId, 'Test in', 1,2);
   ....
</insert>

it simply fails when i trying to run it with mybatis:

PersistenceException occured : ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'insert into Categories(CategoryId, Title, LeftValue, RightValue) values(' at line 2 ### The error may involve Category.create-Inline ### The error occurred while setting parameters ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'insert into Categories(CategoryId, Title, LeftValue, RightValue) values(' at line 2

If I remove this line:

set @catId := (select categoryId from Categories limit 1);

then everything is ok. What am i doing wrong? Is it problem with jdbc or mybatis? How to use mysql @variables with mybatis? Does someone have examples of using MySql local variables with mybatis?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李不 2024-10-26 14:29:27

我找到了如何让它发挥作用。只需设置数据源属性allowMultiQueries = true

jdbc:mysql://localhost:3306/DBS?allowMultiQueries=true

I found out how to get it worked. Just set datasource property allowMultiQueries=true

jdbc:mysql://localhost:3306/DBS?allowMultiQueries=true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文