上傳excel,批量修改數據 ,mybatis批量(四)
控制層 SpringMVC
@ResponseBody
@RequestMapping(value = "BankDataUpload.do", produces = "application/text; charset=utf-8")
public String BankDataUpload(HttpServletRequest request, HttpServletResponse response){
String str="";
try {
str=sysUploadServiceimpl.BankDataUpload_1(request, response);
//System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
實現類 SysUploadServiceimpl
public String BankDataUpload_1(HttpServletRequest request, HttpServletResponse response) throws Exception {
List<TemporaryClient> listofTemporaryClient_1 = new ArrayList<TemporaryClient>();
// 讀取Excel信息
List<List<Object>> listob = UploadExcelUtil.ExcelOfUpload(request, response);
String data="";
// 商品map<商品名稱,商品id>
List<Goodsmanage> listgoodsmanage = dao.getGoodsName();
Map<String, Integer> mapofgid = UploadExcelUtil.getMapOfGoodmanessage(listgoodsmanage); // 商品map
for (int i = 0; i < listob.size(); i++) {
try {
List<Object> lo = listob.get(i);
TemporaryClient temporary = new TemporaryClient();
temporary.setCheckTime(String.valueOf(lo.get(1)).substring(0, 10));
String cName=String.valueOf(lo.get(2)).substring(0, 1);
cName=cName+'%';
temporary.setCname(cName);
String ctel=String.valueOf(lo.get(3));
String ctel_1=ctel.substring(0,3);
String ctel_2=ctel.substring(7,11);
String tel=ctel_1+'%'+ctel_2;
temporary.setCtel(tel);
Integer g_id = mapofgid.get(String.valueOf(lo.get(4)));
temporary.setGid(g_id);
//將excel裏的信息轉化爲list對象
listofTemporaryClient_1.add(temporary);
} catch (Exception e) {
System.out.println("----Excel表出現異常 _____行數 " + i);
data=data+"----Excel表出現異常 _____行數 " + i;
e.printStackTrace();
}
}
int sun=listofTemporaryClient_1.size();
System.out.println("---------總的導入數量--------"+sun);
data=data+"---------總的導入數量--------"+sun;
try {
int num=dao.UpdateTemporaryClient_1(listofTemporaryClient_1);
System.out.println("---------成功修改的數據量--------"+num);
data=data+"---------成功修改的數據量--------"+num;
} catch (Exception e) {
System.out.println("---------導入失敗--------");
data=data+"---------導入失敗-------- ";
e.printStackTrace();
}
return data;
}
結果如圖顯示。
mapper層(Dao層)
//獲取信用卡信息(名字,id()
List<Goodsmanage> getGoodsName();
//根據比對出來的c_id,check_time修改客戶狀態爲status=1;
int UpdateTemporaryClient_1(@Param("listofTemporaryClient")List<TemporaryClient> listofTemporaryClient);
mybatis 裏的sql語句
將商品查出,結果集爲List< goodsmanagess>,再通過將該lis裏的gid,gnamet轉化爲map集,方便根據商品名字得出商品gid,gid=map.get(gname)。減少數據庫查詢。
<!-- 獲取商品信息 -->
<select id="getGoodsName" resultType="cn.sys.entity.Goodsmanage">
select g_id as gId, name from goodsmanage
</select>
將excel裏的數據轉化爲list< UpdateTemporaryClient>對象,利用mybatis批量修改數據庫裏的數據。sql語句如下。
<update id="UpdateTemporaryClient_1" parameterType="java.util.List">
update client
<trim prefix="set" suffixOverrides=",">
<trim prefix="state =case" suffix="end," >
<foreach collection="listofTemporaryClient" item="i" index="index">
when c_tel like #{i.ctel } then 1
</foreach>
</trim>
<trim prefix="check_time =case" suffix="end,">
<foreach collection="listofTemporaryClient" item="i" index="index">
when c_tel like #{i.ctel } then #{i.checkTime}
</foreach>
</trim>
</trim>
where
<foreach collection="listofTemporaryClient" separator="or" item="i" index="index" >
g_id=#{i.gid} and c_tel like #{i.ctel} and c_name like #{i.cname} and #{i.checkTime} >=created_time
</foreach>
</update>