首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用HashMap动态映射万花莲的参数

利用HashMap动态映射万花莲的参数
EN

Stack Overflow用户
提问于 2021-07-01 16:38:39
回答 1查看 806关注 0票数 2

好的,这是对使用ibatis将HashMap值插入到表中这个问题的重新发布(但我正在寻找另一种方法--答案对我不起作用)。

DB1GetStudentDataMapper.xml (对一个数据库的查询)

代码语言:javascript
复制
<?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 (对不同数据库的查询)

代码语言:javascript
复制
<?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

代码语言:javascript
复制
public interface TMODSBDataRefreshMapper {
    
    void insertStudent(@Param("stdMap") HashMap stdMap);
}

StudentDataProcess.java

代码语言:javascript
复制
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:请记住,当您使用较少的列时,此解决方案工作得更好--但如果您有批量插入,则不能很好地工作--它会影响性能。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-01 19:54:47

当使用<foreach />迭代映射时,键和值分别分配给indexitem中指定的变量。

因此,insert语句应该如下所示。

代码语言:javascript
复制
<insert id="insertStudent">
  INSERT INTO STUDENT (
    <foreach collection="stdMap" index="col" separator=",">
      ${col}
    </foreach>
  ) VALUES (
    <foreach collection="stdMap" item="val" separator=",">
      #{val}
    </foreach>
  )
</insert>
  • 列名必须使用${},值必须使用#{}。有关细节,请参见常见问题
  • 要按照相同的顺序迭代映射,应该使用java.util.LinkedHashMap作为<select />的结果类型。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68213950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档