Skip to content
Advertisement

Using HashMap dynamically for parameter mapping in mybatis

Okay so this is kinda re-posting of this question Inserting HashMap Values to a table using ibatis (but I am looking for a different way – the answer wont work for me)..

DB1GetStudentDataMapper.xml (this queries to one database)

JavaScript

DB2InsertStudentMapper.xml (this queries to different database)

JavaScript

DB2InsertStudentMapper.java

JavaScript

StudentDataProcess.java

JavaScript

Note: Just for example I used 4 columns – I have some 150+ columns to work with..

PS: Please remember that this solution works better when you work with fewer columns – but doesn’t work well if you have bulk insert – it effects performance.

Advertisement

Answer

When iterating a map using <foreach />, the key and the value are assigned to the variables specified in index and item respectively.
So, your insert statement should look something like this.

JavaScript
  • You must use ${} for column names and #{} for values. See the FAQ for the details.
  • To iterates the map in the same order, you should use java.util.LinkedHashMap as the result type of <select />.
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement