在Mybatis中动态使用HashMap进行参数映射

Using HashMap dynamically for parameter mapping in mybatis(在Mybatis中动态使用HashMap进行参数映射)

本文介绍了在Mybatis中动态使用HashMap进行参数映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是对这个问题的重新发布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);
            }
            
        }
            
}

注意:仅举个例子,我使用了4列-我有大约150多列要处理..

PS:请记住,此解决方案在使用较少列时效果较好,但如果使用大容量插入则效果不佳,它会影响性能。

推荐答案

使用<foreach />迭代映射时,键和值会分别赋给indexitem中指定的变量。
因此,您的INSERT语句应该如下所示。

<insert id="insertStudent">
  INSERT INTO STUDENT (
    <foreach collection="stdMap" index="col" separator=",">
      ${col}
    </foreach>
  ) VALUES (
    <foreach collection="stdMap" item="val" separator=",">
      #{val}
    </foreach>
  )
</insert>
  • 必须使用${}表示列名,使用#{}表示值。有关详细信息,请参阅FAQ。
  • 若要以相同的顺序迭代映射,应使用java.util.LinkedHashMap作为<select />的结果类型。

这篇关于在Mybatis中动态使用HashMap进行参数映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在Mybatis中动态使用HashMap进行参数映射

基础教程推荐