我是Java和mybatis3的新手.在一个项目我使用mybatis3 ..
说我有名为"t"的数据库表.有几列.
在项目中,我将向mapper.xml发送一个hashmap(包含2个keyList的值,值).从那里它将获得2个数组包含列名称的键和列的值...
我想插入那个表...我认为我能够动态插入数据并部分更新一些列数据...更新...但是得到sql语法错误...
我现有的mapper.xml代码
<insert id="createNews" parameterType="map" useGeneratedKeys="true" keyColumn="id">
INSERT INTO t
<foreach item="key" collection="Key" index="index" open="(" separator="," close=")">
#{key}
</foreach>
VALUES
<foreach item="value" collection="Value" index="index" open="(" separator="," close=")">
#{value}
</foreach>
;
</insert>
Run Code Online (Sandbox Code Playgroud)
部分错误stackTrace ....
### 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 ''name'
)
VALUES
(
'some value'
' at line 3
Run Code Online (Sandbox Code Playgroud)
但是,当我对列名进行硬编码时,它的工作正常...如何动态插入...
注意:我用Google搜索,但无法找到...我不想使用任何pojo或注释...在此先感谢...
不确定,但我会开枪.当您使用#{key},MyBatis的投入额外''围绕它,如果它是一个String,Date等等.如果你需要使用直接的变量给你列名String更换在${key}.
错误日志说的是 ...right syntax to use near ''name') VALUES ('some value'...
你能试一下吗
<insert id="createNews" parameterType="map" useGeneratedKeys="true" keyColumn="id">
INSERT INTO t
<foreach item="key" collection="Key" index="index" open="(" separator="," close=")">
${key}
</foreach>
VALUES
<foreach item="value" collection="Value" index="index" open="(" separator="," close=")">
#{value}
</foreach>
</insert>
Run Code Online (Sandbox Code Playgroud)