java - mybatis中动态使用HashMap进行参数映射

标签 java hashmap mybatis

好吧,这有点重新发布这个问题Inserting HashMap Values to a table using ibatis (但我正在寻找一种不同的方式 - 答案对我不起作用)..

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(此查询不同的数据库)

<?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

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..

PS:请记住,当您使用较少的列时,此解决方案效果更好 - 但如果您进行批量插入,则效果不佳 - 它会影响性能。

最佳答案

使用<foreach />迭代 map 时,键和值被分配给 index 中指定的变量和item分别。
因此,您的插入语句应该如下所示。

<insert id="insertStudent">
  INSERT INTO STUDENT (
    <foreach collection="stdMap" index="col" separator=",">
      ${col}
    </foreach>
  ) VALUES (
    <foreach collection="stdMap" item="val" separator=",">
      #{val}
    </foreach>
  )
</insert>
  • 您必须使用 ${}对于列名称和 #{}为了值(value)观。请参阅FAQ了解详情。
  • 要以相同的顺序迭代 map ,您应该使用 java.util.LinkedHashMap作为 <select /> 的结果类型.

关于java - mybatis中动态使用HashMap进行参数映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68213950/

相关文章:

java - 是否可以在 IntelliJ IDEA 2016 调试器中一键将变量对象列表折叠到特定深度?

java - Tomcat 应用程序部署失败,STRICT_SERVLET_COMPLIANCE=true

java - 如何按插入顺序添加和删除 HashMap 值?

java - HashMap<UUID,ArrayList<>>, arraylist 不会存储值

scala - 为什么 Scala HashMap 很慢?

integer - 为什么MyBatis countByExample默认的resultType是java.lang.Long

java - 如何在mybatis中为Set<MyEnum>指定typeHandler?

java - 使用 if 语句更改图像

java - Magento XML-RPC API - 从 Java 创建货件