Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

java - Inserting HashMap Values to a table using ibatis

I found this on http://old.nabble.com/insert-statement-td21157498.html I want to do the same thing .I have two columns in my table .I am able to insert hash map values by mapping the hashmap key to the column name.Now i want put the key values pairs in the table irrespective of key name.

Pasted from the link above.

I would like to write a dynamic insert statement, but both fields and values are dynamic.

I mean

<insert id="someIDhere" parameterClass="java.util.HashMap">

    insert into table_one (

        !!! dynamic list of keys from the HashMap

    ) values (

        !!! values

    );

  </insert>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Hashmap could be:

    HashMap<String,Integer> hm = new HashMap<String, Integer>();
    hm.put("col1", 1);
    hm.put("col2", 23);
    hm.put("col3", 34);

then call the insert someIDhere with the hm as parameter.

insert into table_one (

    COLUMN1, COLUMN2, COLUMN3

) values (

    #col1#, #col2#, #col3#

);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...