[英]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)..好的,所以这是使用 ibatis 将 HashMap 值插入表这个问题的重新发布(但我正在寻找不同的方式 - 答案对我不起作用)。
DB1GetStudentDataMapper.xml (this queries to one database) DB1GetStudentDataMapper.xml (查询一个数据库)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.testing.db1.DB1GetStudentDataMapper">
<select id="selectAllStudents" resultType="java.util.Map">
SELECT STUDENT_CD, STUDENT_NM, PARENT_CD, CREATED_DATE
FROM STUDENT
WHERE STD_STATUS='ACT'
</select>
</mapper>
DB2InsertStudentMapper.xml (this queries to different database) DB2InsertStudentMapper.xml (这个查询到不同的数据库)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.testing.db2.DB2InsertStudentMapper">
<insert id="insertStudent" parameterType="java.util.HashMap">
INSERT INTO STUDENT
<!-- dynamically select column names from hashmap -->
(#{stdMap.keySet}) // this is not working - its coming as null
<!-- dynamically select values for the above columns from hashmap -->
VALUES (#{stdMap.values}) // this is not working - its coming as null
</insert>
</mapper>
DB2InsertStudentMapper.java DB2InsertStudentMapper.java
public interface TMODSBDataRefreshMapper {
void insertStudent(@Param("stdMap") HashMap stdMap);
}
StudentDataProcess.java学生数据处理程序
public class Student {
// I have defaultExecutorType as BATCH in my mapper config file
private DB1GetStudentDataMapper db1Mapper; // Interface Mapper for first data source
private DB2InsertStudentMapper db2Mapper; // Interface Mapper for second data source
public processStudent() throws Exception {
List<HashMap> rs = db1Mapper.selectAllStudents(); // Gets some 15k+ records
for(int i =0; i < rs.size(); i++) { // so this will loop through 15k+ records
HashMap result = rs.get(i);
System.out.println(result.keySet()); // prints column names from select query [STUDENT_CD, STUDENT_NM, PARENT_CD, CREATED_DATE]
System.out.println(result.values()); // prints above column values of first data set [1001, Mike, 5001, 2021-07-01]
// All I am trying is to insert above 15k records into different database dynamically rather than creating POJO
db2Mapper.insertStudent(result);
}
}
}
Note : Just for example I used 4 columns - I have some 150+ columns to work with..注意:例如,我使用了 4 列 - 我有大约 150 多列可以使用..
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. PS:请记住,当您使用较少的列时,此解决方案效果更好 - 但如果您有批量插入,则效果不佳 - 它会影响性能。
When iterating a map using <foreach />
, the key and the value are assigned to the variables specified in index
and item
respectively.使用<foreach />
迭代映射时,键和值分别分配给index
和item
指定的变量。
So, your insert statement should look something like this.所以,你的插入语句应该是这样的。
<insert id="insertStudent">
INSERT INTO STUDENT (
<foreach collection="stdMap" index="col" separator=",">
${col}
</foreach>
) VALUES (
<foreach collection="stdMap" item="val" separator=",">
#{val}
</foreach>
)
</insert>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.