欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot上傳excel并將表格數(shù)據(jù)導(dǎo)入或更新mySql數(shù)據(jù)庫的過程

 更新時(shí)間:2018年04月26日 12:00:24   作者:Hello小揚(yáng)子  
這篇文章主要介紹了Springboot上傳excel并將表格數(shù)據(jù)導(dǎo)入或更新mySql數(shù)據(jù)庫的過程 ,本文以Controller開始,從導(dǎo)入過程開始講述,其中包括字典表的轉(zhuǎn)換,需要的朋友可以參考下

本文主要描述,Springboot-mybatis框架下上傳excel,并將之導(dǎo)入mysql數(shù)據(jù)庫的過程,如果用戶id已存在,則進(jìn)行更新修改數(shù)據(jù)庫中該項(xiàng)信息,由于用到的是前后端分離技術(shù),這里記錄的主要是后端java部分,通過與前端接口進(jìn)行對接實(shí)現(xiàn)功能,使用layui等前端框架與之對接,也可以自己寫前端代碼,本文以Controller開始,從導(dǎo)入過程開始講述,其中包括字典表的轉(zhuǎn)換

1.在pom.xml文件中導(dǎo)入注解,主要利用POI

<dependency> 
  <groupId>org.apache.poi</groupId> 
  <artifactId>poi-ooxml</artifactId> 
  <version>3.9</version> 
</dependency> 
<dependency> 
  <groupId>commons-fileupload</groupId> 
  <artifactId>commons-fileupload</artifactId> 
  <version>1.3.1</version> 
</dependency> 
<dependency> 
  <groupId>commons-io</groupId> 
  <artifactId>commons-io</artifactId> 
  <version>2.4</version> 
</dependency> 

2.Controller接口

@PostMapping("/save") 
public String addUser(@RequestParam("file") MultipartFile file) { 
 String fileName = file.getOriginalFilename(); 
 try { 
  return sysService.batchImport(fileName, file); 
 } catch (MyException e) { 
  e.printStackTrace(); 
  return e.getMessage(); 
 }catch(Exception e){ 
  e.printStackTrace(); 
  return "文件異常,導(dǎo)入失敗"; 
   
 } 
} 

3.服務(wù)層接口

boolean import(String fileName, MultipartFile file) throws Exception; 

4.業(yè)務(wù)層實(shí)現(xiàn)類

@Transactional(readOnly = false,rollbackFor = Exception.class) 
@Override 
public boolean import(String fileName, MultipartFile file) throws Exception { 
 Map<String, Integer> departmentMap = findDepartment(); 
 Map<String, Integer> roleMap = findRole(); 
 boolean notNull = false; 
  List<User> userList = new ArrayList<User>(); 
 if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { 
   throw new MyException("上傳文件格式不正確"); 
 } 
 boolean isExcel2003 = true; 
 if (fileName.matches("^.+\\.(?i)(xlsx)$")) { 
  isExcel2003 = false; 
 } 
 InputStream is = file.getInputStream(); 
  Workbook wb = null; 
  if (isExcel2003) { 
   wb = new HSSFWorkbook(is); 
  } else { 
   wb = new XSSFWorkbook(is); 
  } 
  Sheet sheet = wb.getSheetAt(0); 
  if(sheet!=null){ 
   notNull = true; 
  } 
 User user; 
 for (int r = 1; r <= sheet.getLastRowNum(); r++) { 
  Row row = sheet.getRow(r); 
  if (row == null){ 
   continue; 
  } 
  user = new User(); 
  if( row.getCell(0).getCellType() !=1){ 
   throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,ID單元格格式請?jiān)O(shè)為文本格式)"); 
  } 
  String id = row.getCell(0).getStringCellValue(); 
  if(id==null || id.isEmpty()){ 
    throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,ID未填寫)"); 
  } 
  String name = row.getCell(1).getStringCellValue(); 
  if(name==null || name.isEmpty()){ 
    throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,姓名未填寫)"); 
  } 
  String department = row.getCell(2).getStringCellValue(); 
  if(departmentMap.get(department)==null){ 
    throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,不存在此單位或單位未填寫)"); 
  } 
  String role = row.getCell(3).getStringCellValue(); 
  if(roleMap.get(role)==null){ 
   throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,不存在此角色或角色未填寫)"); 
  } 
  Date date; 
  if(row.getCell(4).getCellType() !=0){ 
   throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,入職日期格式不正確或未填寫)"); 
   }else{ 
     date = row.getCell(4).getDateCellValue(); 
   } 
   user.setId(id); 
   user.setName(name); 
   user.setDepartmentId((int) departmentMap.get(department)); 
   user.setRoleId((int) roleMap.get(role)); 
   user.setDate(date); 
   userList.add(user); 
  } 
 for (User user : userList) { 
  String id = user.getId(); 
  int cnt = userMapper.selectById(id); 
  if (cnt == 0) { 
   userMapper.addUser(user); 
  } else { 
   userMapper.updateUserById(user); 
  } 
 } 
 return notNull; 
} 

總結(jié)

以上所述是小編給大家介紹的Springboot上傳excel并將表格數(shù)據(jù)導(dǎo)入或更新mySql數(shù)據(jù)庫,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • java Spring整合Freemarker的詳細(xì)步驟

    java Spring整合Freemarker的詳細(xì)步驟

    本文對Spring整合Freemarker步驟做了詳細(xì)的說明,按步驟操作一定可以整合通過,這里提供給大家做參考
    2013-11-11
  • Java使用Freemarker頁面靜態(tài)化生成的實(shí)現(xiàn)

    Java使用Freemarker頁面靜態(tài)化生成的實(shí)現(xiàn)

    這篇文章主要介紹了Java使用Freemarker頁面靜態(tài)化生成的實(shí)現(xiàn),頁面靜態(tài)化是將原來的動(dòng)態(tài)網(wǎng)頁改為通過靜態(tài)化技術(shù)生成的靜態(tài)網(wǎng)頁,FreeMarker?是一個(gè)用?Java?語言編寫的模板引擎,它基于模板來生成文本輸,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-06-06
  • SpringBoot集成echarts實(shí)現(xiàn)k線圖功能

    SpringBoot集成echarts實(shí)現(xiàn)k線圖功能

    ECharts是一款基于JavaScript的數(shù)據(jù)可視化圖表庫,提供直觀,生動(dòng),可交互,可個(gè)性化定制的數(shù)據(jù)可視化圖表,本文給大家介紹了SpringBoot集成echarts實(shí)現(xiàn)k線圖功能,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2024-07-07
  • 如何使用MyBatis框架實(shí)現(xiàn)增刪改查(CRUD)操作

    如何使用MyBatis框架實(shí)現(xiàn)增刪改查(CRUD)操作

    本文主要介紹了如何使用MyBatis框架實(shí)現(xiàn)增刪改查(CRUD)操作。首先介紹了MyBatis框架的基本概念和使用方法,然后分別介紹了如何使用MyBatis實(shí)現(xiàn)增刪改查操作。最后,通過一個(gè)簡單的示例演示了如何使用MyBatis框架實(shí)現(xiàn)CRUD操作。
    2023-05-05
  • Java?中導(dǎo)入excel時(shí)使用?trim()?無法去除空格的問題解決方案

    Java?中導(dǎo)入excel時(shí)使用?trim()?無法去除空格的問題解決方案

    這篇文章主要介紹了Java中導(dǎo)入excel時(shí)使用trim()無法去除空格的解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • Activiti開發(fā)環(huán)境的搭建過程詳解

    Activiti開發(fā)環(huán)境的搭建過程詳解

    這篇文章主要介紹了Activiti開發(fā)環(huán)境的搭建過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • WMTS中TileMatrix與ScaleDenominator淺析

    WMTS中TileMatrix與ScaleDenominator淺析

    這篇文章主要為大家介紹了WMTS中TileMatrix與ScaleDenominator淺析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Spring?Boot實(shí)現(xiàn)文件上傳下載

    Spring?Boot實(shí)現(xiàn)文件上傳下載

    這篇文章主要為大家詳細(xì)介紹了Spring?Boot實(shí)現(xiàn)文件上傳下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Java中自定義LRU緩存詳解

    Java中自定義LRU緩存詳解

    這篇文章主要介紹了Java中自定義LRU緩存詳解,基于LRU算法的緩存系統(tǒng),可以在達(dá)到緩存容量上限時(shí),清理最近最少使用的數(shù)據(jù),為新的數(shù)據(jù)的插入騰出空間,需要的朋友可以參考下
    2023-09-09

最新評(píng)論