JAvaインポートcsvファイル反射呼び出しMapperインタフェース

6753 ワード

1.pom依存の導入

       org.apache.commons
       commons-csv
       1.6

2.ツールクラスの作成 CsvUtils
自分でネット上でテンプレートを探すこともできます.私もネット上で参考にしています.
public class CsvUtils {

    /**
     * io   
     * @author kpzc
     * @date 2018 12 29    3:48:34
     * @param file csv  (  +   ),csv          
     * @param dataList   ,        
     * @return          true   false  
     */
    public static boolean exportCsv(File file, List dataList){
        boolean isSucess=false;

        FileOutputStream out=null;
        OutputStreamWriter osw=null;
        BufferedWriter bw=null;
        try {
            out = new FileOutputStream(file);
            //  FileOutputStream          MS office    
            osw = new OutputStreamWriter(out, "GBK");
            bw =new BufferedWriter(osw);
            if(dataList!=null && !dataList.isEmpty()){
                for(String data : dataList){
                    bw.append(data).append("\r");
                }
            }
            isSucess=true;
        } catch (Exception e) {
            isSucess=false;
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return isSucess;
    }

    /**
     *   
     * @author kpzc
     * @date 2018 12 29    3:48:11
     * @param file  csv  (  +  )
     * @return   List  
     */
    public static List importCsv(MultipartFile file){
        List dataList=new ArrayList();
        BufferedReader br=null;
        try {
            br = new BufferedReader(new InputStreamReader(file.getInputStream(), "UTF-8"));
            String line = "";
            while ((line = br.readLine()) != null) {
                dataList.add(line.replace("\"",""));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(br!=null){
                try {
                    br.close();
                    br=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return dataList;
    }

    /**
     * apache commons-csv  
     *   jdk  1.7     
     * map       header            ,   main  
     * @author kpzc
     * @date 2019 1 4    10:12:20
     * @param filePath       
     * @param list     
     * @param header   
     */
    public static void write(String filePath, List> list, String... header) {
        try {
            FileOutputStream fos = new FileOutputStream(filePath);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
            CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(header);
            CSVPrinter csvPrinter = new CSVPrinter(osw, csvFormat);
            //           
            //CSVPrinter csvPrinter = CSVFormat.DEFAULT.withHeader(header).print(osw);
            for (Map map : list) {
                csvPrinter.printRecord(map.values());
            }
            csvPrinter.flush();
            csvPrinter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

3.SpringContextUtilの作成コンテキストオブジェクトの取得
@Component
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext appCtx = null;

    /**
     * @param applicationContext
     *            ApplicationContext.
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        appCtx = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return appCtx;
    }

    public static Object getBean(String beanName) {
        return appCtx.getBean(beanName);
    }
}

4.ファイルクラスのインポート
@Override
    public ResultInfo importFile(ImportRo importRo, HttpServletRequest request) {
        String path = "com.senhua.dao." + importRo.getEntityName() + "Mapper";//Mapper          ,     
        List list = CsvUtils.importCsv(importRo.getFile());
        //System.out.println("list = " + list.get(1));
        try{
            if(!list.isEmpty()){
                Class c = Class.forName(path);
                //           
                Method[] methods = c.getMethods();
                Class param = null;
                Field[] fields = null;

                for (int i = 0; i < methods.length; i++) {
                    if(methods[i].getName().equals("insertSelective")){
                        Class[] paramTypes = methods[i].getParameterTypes();//        
                        param = paramTypes[0];
                        //           
                        fields = param.getDeclaredFields();
                    }
                }
                //       
                Method m = c.getMethod("insertSelective",param);
                boolean flag = false;
                for(int i=1;i