java - MyBatis java 和 MySql 局部变量

标签 java mysql jdbc ibatis mybatis

我是 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 局部变量的例子吗?

最佳答案

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

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

关于java - MyBatis java 和 MySql 局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5144049/

相关文章:

sql - 语法错误 JMeter DB2

java - 抽象工厂模式和HikariCP

java - Excel 公式比较两列

php - 保存后下拉选择

c# - 使用 C# 与 Amazon EC2 中的 MySQL 的桌面连接(操作方法)

java - 使用jdbc在oracle中将InputStream保存为clob

java - Runnable/Thread 可能的误解

java - JSTL c :set not working as expected

java - 将 10^x 次方统一为数字序列

PHP/MYSQL 通过 id "duplicate ids"查询