How to pass dynamic fields and values using mybatis 3

381 views
Skip to first unread message

Shakil Ahmed

unread,
Sep 16, 2014, 4:13:15 PM9/16/14
to mybati...@googlegroups.com
I am new to Java and mybatis3. On a project I am using mybatis3..

say I have database table named "t". There are several columns.

In the project I will send a hashmap (contains 2 ArrayList of key, value) to mapper.xml. From there it will get 2 array contain keys of columns names, and values of columns...

 I want to inset into that table... by that, I think I will able to dynamically insert data and partially update some column data... with update... but getting sql syntax error...

My existing code of 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>



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


But when I hard code the column name its working correctly... How can I insert dynamically...

Note: I googled, but unable to find... I don't want to use any pojo or annotation... thanks in advance...
Message has been deleted

Ikchan Sim

unread,
Sep 16, 2014, 9:05:34 PM9/16/14
to mybati...@googlegroups.com
Run directly result was not a problem. 
The running result is as follows.

 


Reply all
Reply to author
Forward
0 new messages