was*_*tim 3 java mysql ibatis jdbc 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>
Run Code Online (Sandbox Code Playgroud)
当我尝试使用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
Run Code Online (Sandbox Code Playgroud)
如果我删除此行:
set @catId := (select categoryId from Categories limit 1);
Run Code Online (Sandbox Code Playgroud)
那一切都还好 我究竟做错了什么?jdbc或mybatis是否有问题?如何在Mybatis中使用mysql @variables?有人有在Mybatis中使用MySql局部变量的示例吗?
我发现了如何使其工作。只需设置数据源属性allowMultiQueries = true
jdbc:mysql://localhost:3306/DBS?allowMultiQueries=true
Run Code Online (Sandbox Code Playgroud)