Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MyBatis java and MySql local variables

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?

like image 985
wassertim Avatar asked Sep 17 '25 03:09

wassertim


1 Answers

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

jdbc:mysql://localhost:3306/DBS?allowMultiQueries=true
like image 140
wassertim Avatar answered Sep 19 '25 17:09

wassertim