package com.tianju.supermelon.controller;标签:java,tianju,数据库,Excel,插入,org,supermelon,import,com From: https://www.cnblogs.com/ZYJ-Breeze/p/16978782.html
import com.tianju.supermelon.common.dtos.ResponseResult;
import com.tianju.supermelon.domain.AvatarInfo;
import com.tianju.supermelon.mapper.AvatarInfomMapper;
import com.tianju.supermelon.mapper.UserInveraciousMapper;
import com.tianju.supermelon.util.ExcelImportSheet;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author 西红柿
* @version 1.0
*/
@Slf4j
@RequestMapping
@RestController
public class AvtualController {
@Autowired
private AvatarInfomMapper avatarInfomMapper;
@Autowired
private UserInveraciousMapper userInveraciousMapper;
@GetMapping("/avatarHandler")
public void avatarHandler (){
Map<String , String> map = new HashMap<>();
//表头与键值对的映射关系
map.put("openId", "openId");
map.put("avatarUrl" , "avatarUrl");
try(
//这里面的对象会自动关闭
InputStream in = new FileInputStream(new File("D:\\export_urls(5).xlsx"));
//用流来构建工作簿对象
Workbook workbook = ExcelImportSheet.getTypeFromExtends(in , "AvatarInfo.xlsx")
) {
//根据名称获取单张表对象 也可以使用getSheetAt(int index)获取单张表的对象 获取第一张表
Sheet sheet = workbook.getSheetAt(0);
List<AvatarInfo> list = ExcelImportSheet.getListFromExcel(sheet , AvatarInfo.class , map);
for (AvatarInfo student : list) {
//底层数据库操作 insert什么的
avatarInfomMapper.insert(student);
System.out.println(student.toString());
}
}catch(IOException exception) {
exception.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally {
//根据具体的业务逻辑进行操作
}
}
@GetMapping("/avatarUrl")
public void avatarUrl (){
for (int i = 1; i <=3000 ; i++) {
AvatarInfo avatarInfo = avatarInfomMapper.selectById(i);
String url = avatarInfo.getAvatarUrl();
userInveraciousMapper.updateUrl(url,i+"");
}
}
}