Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 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 Answer

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#

);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...