日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了在Mybatis中動態(tài)使用HashMap進(jìn)行參數(shù)映射的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

好的,這是對這個(gè)問題的重新發(fā)布Inserting HashMap Values to a table using ibatis(但我正在尋找不同的方法-答案對我不起作用)。

DB1GetStudentDataMapper.xml(這查詢一個(gè)數(shù)據(jù)庫)

<?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(這查詢到不同的數(shù)據(jù)庫)

<?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);
            }
            
        }
            
}

注意:僅舉個(gè)例子,我使用了4列-我有大約150多列要處理..

PS:請記住,此解決方案在使用較少列時(shí)效果較好,但如果使用大容量插入則效果不佳,它會影響性能。

推薦答案

使用<foreach />迭代映射時(shí),鍵和值會分別賦給indexitem中指定的變量。
因此,您的INSERT語句應(yīng)該如下所示。

<insert id="insertStudent">
  INSERT INTO STUDENT (
    <foreach collection="stdMap" index="col" separator=",">
      ${col}
    </foreach>
  ) VALUES (
    <foreach collection="stdMap" item="val" separator=",">
      #{val}
    </foreach>
  )
</insert>

必須使用${}表示列名,使用#{}表示值。有關(guān)詳細(xì)信息,請參閱FAQ。
若要以相同的順序迭代映射,應(yīng)使用java.util.LinkedHashMap作為<select />的結(jié)果類型。

這篇關(guān)于在Mybatis中動態(tài)使用HashMap進(jìn)行參數(shù)映射的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標(biāo)簽:HashMap mybatis 動態(tài) 參數(shù) 映射
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定