为什么mybatis在执行查询时抱怨SQL语法错误?

问题描述 投票:0回答:1

我有一个Mybatis映射器,应该在数据库表中创建一个条目。映射器定义如下-

    <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>

存储过程在下面定义-

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 ;

mybatis给出的错误声明在下面-

### 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', '[email protected]', '$2a$10$JlIb3BRXffYhMV8j9Wy2OuG2Wu53lGrHKexKspErzhi/JGwMhsvoO');

但是当我在mysql中执行相同的查询时,它会愉快地执行。

enter image description here

如果mysql执行相同查询没有任何问题,为什么mybatis抱怨相同?

mysql spring-boot mybatis spring-mybatis
1个回答
0
投票

糟糕,这是一个令人难以置信的搜索。

[@Akina指出,jdbc mysql out不支持多查询。

如果有人遇到相同的问题,请查看此solution

© www.soinside.com 2019 - 2024. All rights reserved.