解決mybatis中#{}導致的The error may involve defaultParameterMap的問題


今天想實現給指定表插入數據,出現了

### Error updating 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 ''api_assets0' (id,name, level,type,file_id)
values
(3' at line 1
### The error may exist in file [E:\java\project\admin\devide\target\classes\mapper\ApiAssetsMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: insert into ? (id,name, level,type,file_id) values (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?) , (?,?, ?,?,?)
### 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 ''api_assets0' (id,name, level,type,file_id)
values
(3' at line 1
; bad SQL grammar []; nested exception is 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 ''api_assets0' (id,name, level,type,file_id)
values

mapper里

int insert(@Param("list") List<ApiAssets> list,@Param("one") String one);

XML里

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.abc.devide.demo.mapper.ApiAssetsMapper">
    ……
    <insert id="insert">
        insert into #{one} (id,name, level,type,file_id)
        values
        <foreach collection="list" item="item" index="index" separator=",">
        (#{item.id},#{item.name}, #{item.level},#{item.type},#{item.fileId})
        </foreach>
    </insert>

</mapper>

解決方法:

  使用 ${} 接受表名,#{}接受其他參數

將insert部分改為

<insert id="insert">
        insert into ${one} (id,name, level,type,file_id)
        values
        <foreach collection="list" item="item" index="index" separator=",">
        (#{item.id},#{item.name}, #{item.level},#{item.type},#{item.fileId})
        </foreach>
</insert>

就可以成功執行了

${} 同 #{} 的區別 查看


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM