工具方法
default ArrayList<HashMap<String,String>> txt2MapList(String[] fieldNames,File file){
ArrayList<HashMap<String,String>> entityMaps = new ArrayList<>();
try{
BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
String s = null;
while((s = br.readLine())!=null){//使用readLine方法,一次读一行
String[] sz = s.split("\\|");
HashMap<String, String> tmpMap = new HashMap<>();
for (int i=0;i<sz.length;i++) {
tmpMap.put(fieldNames[i], sz[i]);
}
entityMaps.add(tmpMap);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
return entityMaps;
}
ArrayList<HashMap<String, String>> inComeEntityMaps = handler.parseTxt(file);
根据上述方法解析出 inComeEntityMaps对象
void insert(@Param("lists") List<HashMap<String,String>> InComelist);
<!-- 保存新增数据 -->
<insert id="insert" parameterType="java.util.List">
<foreach collection="lists" item="maps" index="indexName" separator=";">
insert into table
<foreach item="key" index="keyIndex" collection="maps.keySet()" separator="," open="(" close=")">
${key}
</foreach>
values
<foreach item="value" index="valueIndex" collection="maps.entrySet()" separator="," open="(" close=")">
<choose>
<when test="valueIndex == 'month' or valueIndex == 'day'">
CAST(#{value} AS SMALLINT)
</when>
<otherwise>
#{value}
</otherwise>
</choose>
</foreach>
</foreach>
</insert>
根据对应的Dao和xml方法,代入即可实现动态参数入库