All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cifaz.tools.dao.MybatisGeneratorUtil Maven / Gradle / Ivy

The newest version!
package com.cifaz.tools.dao;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.ProgressCallback;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.*;
import java.util.*;

public class MybatisGeneratorUtil {
    static String xmlRelativeDir = "/src/test/resources/xml";
    static String xmlFileSnapShotRelativeDir = "/src/test/resources/xml_file_snapshot.txt";

    public MybatisGeneratorUtil() {
    }

    private Map xmlFileSnapshot;

    private Map generatedFileSnapshot;

    public Map scanDir(String dirName) {
        Map snapshot = new HashMap();
        File dir = null;
        try {
            dir = new File(dirName);
            if (dir.isDirectory()) {
                File[] files = dir.listFiles();
                for (File file : files) {
                    snapshot.put(file.getName(), file.lastModified());
                }
            } else {
                snapshot.put(dir.getName(), dir.lastModified());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return snapshot;
    }

    public Map scanFile(String fileLoc) {
        Map snapshot = new HashMap();
        File file = new File(fileLoc);
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] records = line.split(" ");
                if (records.length == 2) {
                    snapshot.put(records[0], Long.valueOf(records[1]));
                }
            }
        } catch (FileNotFoundException e) {
            System.err.println("file not found");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return snapshot;
    }

    public void saveMap(Map snapshot, String filename) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(filename));
            for(Map.Entry entry : snapshot.entrySet()) {
                bw.write(entry.getKey() + " " + entry.getValue().toString());
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public String getXmlDir(Class clazz) {
        String xmlDir = getProjectPath(clazz) + xmlRelativeDir;
        return xmlDir;
    }

    public String getXmlFileSnapShotDir(Class clazz) {
        String xmlDir = getProjectPath(clazz) + xmlFileSnapShotRelativeDir;
        return xmlDir;
    }

    public String getProjectPath(Class clazz) {
        File thisfilepath = new File(clazz.getClass().getResource("/").getPath());
        String parent = thisfilepath.getParent();
        String projectPath = parent.substring(0, parent.lastIndexOf(File.separator));
        return projectPath;
    }

    public void generateAll(Class clazz) throws Throwable {
        String dir = getXmlDir(clazz);
        String snapshotFile = getXmlFileSnapShotDir(clazz);
        System.setProperty("xml.dir", dir);
        System.setProperty("user.dir", getProjectPath(clazz));

        generatedFileSnapshot = scanFile(snapshotFile);

        xmlFileSnapshot = scanDir(dir);

        Map newSnapshot = new HashMap();
        for(Map.Entry entry : xmlFileSnapshot.entrySet()) {
            newSnapshot.put(entry.getKey(), entry.getValue());
        }

        for(Map.Entry entry : generatedFileSnapshot.entrySet()) {
            if(xmlFileSnapshot.containsKey(entry.getKey())) {
                if(xmlFileSnapshot.get(entry.getKey()).longValue() <= entry.getValue().longValue()) {
                    System.out.println(entry.getKey() + " was skipped.");
                    xmlFileSnapshot.remove(entry.getKey());
                }
            }
        }

        saveMap(newSnapshot, snapshotFile);

        if (xmlFileSnapshot.size() > 0) {
            System.out.println("files to be used for generating: ");
            String[] xmls = new String[xmlFileSnapshot.size()];
            xmlFileSnapshot.keySet().toArray(xmls);
            System.out.println(Arrays.toString(xmls));
            generate(xmls);
        }
    }

    @Deprecated
    public void generate1(String... xmllist) throws Throwable {
        if (null != xmllist) {
            for(int i = 0; i < xmllist.length; ++i) {
                List warnings = new ArrayList();
                boolean overwrite = true;
                String path = (new File("./src/main/resources/")).getAbsolutePath() + "/" + xmllist[i];
                System.out.println(path);
                File configFile = new File(path);
                ConfigurationParser cp = new ConfigurationParser(warnings);
                Configuration config = cp.parseConfiguration(configFile);
                DefaultShellCallback callback = new DefaultShellCallback(overwrite);
                MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
                myBatisGenerator.generate((ProgressCallback)null);
            }

            System.out.println("生成结束");
        }
    }

    public void generate(String... xmllist) throws Throwable {
        if (null == xmllist) {
            return;
        }

        for (int i = 0; i < xmllist.length; i++) {
            List warnings = new ArrayList();
            boolean overwrite = true;
            String path = System.getProperty("xml.dir") + "/" + xmllist[i];
            System.out.println(path);
            File configFile = new File(path);
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        }

        System.out.println("生成结束");
    }

    /*public static void main(String[] args) {
        Object obj = null;
        System.out.println("".equals(obj));

        //        String homeDir = "E:\\5project\\idea\\201801\\";
//        String xmlDir = homeDir + "java-panda-crm-admin-dubbo/crm-admin-dao/src/test/resources/xml";
//        String snapshotFileLoc = homeDir + "java-panda-crm-admin-dubbo/crm-admin-dao/src/test/resources/xml_file_snapshot.txt";
//        System.setProperty("xml.dir", xmlDir);
//        System.setProperty("user.dir", homeDir + "java-panda-crm-admin-dubbo/crm-admin-dao");
//        new MybatisGeneratorUtil().generateAll(snapshotFileLoc, xmlDir);
//        new MybatisGeneratorUtil().generate1("t_template.xml");
    }*/
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy