简体   繁体   中英

Why does mybatis complain for SQL syntax error while executing query?

I have a Mybatis mapper which is supposed to create an entry in Database table. The mapper is defined below-

    <select id="saveUser" parameterType="User" resultMap="UserResultMap">
        DROP TEMPORARY TABLE IF EXISTS temp_role_id;
        CREATE TEMPORARY TABLE temp_role_id(id int);
        INSERT INTO temp_role_id VALUES
        <foreach collection="roles" item="role" separator=",">
            (${role.id})
        </foreach>;
        call insert_user_sp('${name}', '${username}', '${email}', '${password}');
    </select>

The stored procedure is defined below-

DELIMITER //
CREATE PROCEDURE insert_user_sp(
  u_name VARCHAR(40),
  u_username VARCHAR(20),
  u_email VARCHAR(40),
  u_password VARCHAR(100)
)
BEGIN
  INSERT INTO users(name, username, email, password)
  VALUES(u_name, u_username, u_email, u_password);

  SET @v_user_id = last_insert_id();

  INSERT INTO user_roles(user_id, role_id)
    SELECT @v_user_id, id from temp_role_id;

  DROP TEMPORARY TABLE IF EXISTS temp_role_id;

  SELECT id, name, username, email, password, created_at, modified_at
    FROM users where id = @v_user_id;
END //

DELIMITER ;

The error statement given by mybatis is below-

### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: 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 'CREATE TEMPORARY TABLE temp_role_id(id int);
        INSERT INTO temp_role_id VA' at line 2
### The error may exist in mybatis/mappers/UserMapper.xml
### The error may involve UserMapper.saveUser-Inline
### The error occurred while setting parameters
### SQL: DROP TEMPORARY TABLE IF EXISTS temp_role_id;         CREATE TEMPORARY TABLE temp_role_id(id int);         INSERT INTO temp_role_id VALUES                        (4)          ;         call insert_user_sp('Ujjal Das', 'ujjaldas223', 'ujjaldas223@gmail.com', '$2a$10$JlIb3BRXffYhMV8j9Wy2OuG2Wu53lGrHKexKspErzhi/JGwMhsvoO');

But when I execute the same query in mysql, it executes happily.

在此处输入图片说明

If mysql doesn't have any problem in executing the same query, why is mybatis complaining about the same?

Oops, it's a hell lot of a searching.

As @Akina pointed it out, jdbc mysql out didn't support multiqueries.

In case if anyone faces the same problem, have a look at this solution .

if you are using xml, you have to use it like this. <![CDATA[your_symbol_that_error_came]]> as an example, you want to use <= symbol, the code will be like below,

<![CDATA[<=]]>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM