BlueAroma

[MyBatis/SQL] 동적으로 map 에 담긴 key/value 이용하기 본문

내맘대로 프로그래밍/MariaDB&SQL

[MyBatis/SQL] 동적으로 map 에 담긴 key/value 이용하기

BlueAroma 2018. 2. 28. 20:24


down voteaccepted

Not sure, but I'll take a shot. When you use #{key}, MyBatis puts extra '' around it if it is a StringDate etc. If you give your column names with a variable you need to use direct Stringreplacement which is ${key}.

The error log says something like ...right syntax to use near ''name') VALUES ('some value'...

Can you try

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


참고 : https://stackoverflow.com/questions/25877458/how-to-pass-dynamic-fields-and-values-using-mybatis-3

Comments