欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自动生成facade,service,dao 工具

程序员文章站 2022-03-04 18:59:40
...

自动生成facade,service,dao 工具

工具类

package com.cmb.ccrm.customer;

import com.cmb.ccrm.tool.base.util.StringUtil;
import com.google.common.collect.ImmutableList;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;



public class MyUtil {

    //model 所在包名
    private static String modelPath = "com.cmb.ccrm.customer.model";
    //生成的facade,service,dao 所在包名
    private static String basePath = "com.cmb.ccrm.customer";
    //facade的父类 类名
    private static String facadeParent = "CcrmCustomerBaseFacade";
    //dao的父类 类名
    private static String daoParent = "CcrmCustomerBaseDao";
    //4位项目编号
    private static String projectCd = "MY01";

    public static void main(String[] args) {
      /*  MyModel myModel = new MyModel(
                "com.cmb.ccrm.customer.testD",
                "MY01UserFacade",
                "CcrmCustomerBaseFacade",
                "CcrmCustomerBaseDao",
                "User"
        );
        MyModel myModel2 = new MyModel(
                "com.cmb.ccrm.customer.testA",
                "MY01OrderFacade",
                "CcrmCustomerBaseFacade",
                "CcrmCustomerBaseDao",
                "Order"
        );

        MyUtil util = new MyUtil();
        util.getJavaFacade(ImmutableList.<MyModel>of(myModel, myModel2));*/

        MyUtil util = new MyUtil();
        List<String> fileNames = getFileNames(modelPath);
        List<MyModel> models = getModels(basePath, facadeParent, daoParent, fileNames);
        util.getJavaBatch(models);

    }


   // @Test
    public void test() {
        List<String> fileNames = getFileNames(modelPath);
        for (String file : fileNames) {
            System.out.println(file);
        }
    }

    private static List<MyModel> getModels(String basePath,String facadeParent,String daoParent,List<String> modelNames) {
        List<MyModel> modelList = new ArrayList();

        for (String modelName:modelNames) {
            MyModel myModel = new MyModel();
            myModel.setClassName(projectCd+modelName+facadePost);
            myModel.setDaoParentClass(daoParent);
            myModel.setFacadeParentClass(facadeParent);
            myModel.setFileModelName(modelName);
            myModel.setPackagePath(basePath+"."+modelName);
            modelList.add(myModel);
        }

        return modelList;
    }

    private static List<String> getFileNames(String modelPath) {
        List<String> fileNames = new ArrayList();
        //todo
        String path = getPath(modelPath, "");
        File direct = new File(path);
        File[] files = direct.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                String name = file.getName();
                String modelName = name.substring(0, name.lastIndexOf("."));
                fileNames.add(modelName);
            }
        }
        return fileNames;
    }

    void getJavaBatch(List<MyModel> myModelList) {

        for (MyModel myModel : myModelList) {
            getJava(myModel);
        }

    }

    private static String filePath = "src/main/java/";
    private static String postFile = ".java";
    private static String facadePost = "Facade";
    public void getJava(MyModel myModel) {
        try {
            String packagePath = myModel.getPackagePath();//"com.cmb.ccrm.customer.testD";//所在包
            String className = myModel.getClassName();//"MY01UserFacade";//facade Bean Name  要求长度大于11,前4位为工程编码,后6位为Facade,中间位数驼峰命名
            String fileModelName = myModel.getFileModelName();//"User";//模型

            String serviceName = fileModelName + "Service";
            String daoName = fileModelName + "Dao";
            String daoImplName = fileModelName + "DaoImpl";


            //获得路径
            String facadeTruePath = getPath(packagePath, ".facade");
            String serviceTruePath = getPath(packagePath, ".service");
            String daoTruePath = getPath(packagePath, ".dao");
            String modelTruePath = getPath(packagePath, ".model");

            System.out.println(facadeTruePath);
            //创建文件
            File facadeFile = createFile(facadeTruePath, className.substring(4, className.length()));
            File serviceFile = createFile(serviceTruePath, serviceName);
            File daoFile = createFile(daoTruePath, daoName);
            File daoImplFile = createFile(daoTruePath + "/impl", daoImplName);
            createFile(modelTruePath, null);

            //获得数据
            String facadeData = getFacadeData(myModel);
            String serviceData = getServiceData(myModel);
            String daoData = getDaoData(myModel);
            String daoImplData = getDaoImplData(myModel);

            //facade
            writeData(facadeFile, facadeData);

            //service
            writeData(serviceFile, serviceData);

            //dao
            writeData(daoFile, daoData);

            //daoImpl
            writeData(daoImplFile, daoImplData);

            System.out.println("Done:" + myModel.getFileModelName());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getDaoImplData(MyModel myModel) {
        String packagePath = myModel.getPackagePath();
        String daoName = myModel.getFileModelName() + "Dao";
        String daoParentClass = myModel.getDaoParentClass();// "CcrmCustomerBaseDao";//dao父类
        String daoImplName = myModel.getFileModelName() + "DaoImpl";

        StringBuffer data = new StringBuffer();

        data.append("package " + packagePath + ".dao.impl" + ";\n");

        data.append("import " + packagePath + ".dao." + daoName + ";\n" +
                "import org.springframework.stereotype.Repository;\n");
        data.append("\n");

        data.append("@Repository\n" +
                "public class " + daoImplName + " extends " + daoParentClass + " implements " + daoName + " {\n" +
                "\n" +
                "    private static final String BASE_PATH = \"" + packagePath + ".dao.\";\n" +
                "\n" +
                "    //todo\n" +
                "\n" +
                "}\n");

        return data.toString();
    }

    private void writeData(File facadeFile, String facadeData) throws IOException {
        FileWriter fileWritter = new FileWriter(facadeFile, false);
        fileWritter.write(facadeData);
        fileWritter.close();
    }

    private String getDaoData(MyModel myModel) {
        String packagePath = myModel.getPackagePath();
        String daoName = myModel.getFileModelName() + "Dao";
        StringBuffer data = new StringBuffer();

        data.append("package " + packagePath + ".dao;\n");

        data.append("\n");
        data.append("\n");

        data.append("public interface " + daoName + " {\n" +
                "\n" +

                "\n" +
                "}");
        return data.toString();
    }


    private File createFile(String truePath, String fileName) throws IOException {

        File file1 = new File(truePath);

        if (!file1.exists()) {
            file1.mkdirs();
        }
        if (fileName == null || StringUtil.isEmpty(fileName)) {
            return null;
        }
        File file = new File(truePath, fileName + postFile);


        if (!file.exists()) {

            file.createNewFile();

        }
        return file;
    }

    private String getServiceData(MyModel myModel) {
        String packagePath = myModel.getPackagePath();
        String serviceName = myModel.getFileModelName() + "Service";
        String daoName = myModel.getFileModelName() + "Dao";

        StringBuffer data = new StringBuffer();

        data.append("package " + packagePath + ".service;\n");
        data.append("\n");
        data.append("\n");
        data.append("import org.springframework.beans.factory.annotation.Autowired;\n" +
                "import org.springframework.stereotype.Service;\n");
        data.append("import " + packagePath + ".dao." + daoName + ";\n");
        data.append("\n");

        data.append("@Service\n" +
                "public class " + serviceName + " {\n" +
                "\n" +
                "    @Autowired\n" +
                "    private " + daoName + " " + getStringFirst2lower(daoName) + ";\n" +
                "\n" +
                "    //todo \n" +
                "\n" +
                "}");

        return data.toString();

    }

    private static String getPath(String packagePath, String name) {
        String resource = Thread.currentThread().getContextClassLoader().getResource("").toString();
        String truePath = resource.substring(resource.indexOf("/") + 1, resource.indexOf("target"));
        packagePath += name;
        String path = packagePath.replaceAll("\\.", "/");
        return truePath + filePath + path;
    }

    private String getFacadeData(MyModel myModel) throws Exception {
        String className = myModel.getClassName();
        String packagePath = myModel.getPackagePath();
        String serviceName = myModel.getFileModelName() + "Service";
        String facadeParentClass = myModel.getFacadeParentClass();

        if (className.length() < 11) {
            throw new Exception(className + "长度需要大于10");
        }
        String xz = className.substring(4, className.length() - 6).toUpperCase();
        StringBuffer data = new StringBuffer();

        data.append("package " + packagePath + ".facade;\n");

        data.append("\n");
        data.append("\n");

        data.append("import com.cmb.framework.exception.FrameworkException;\n" +
                "import org.slf4j.Logger;\n" +
                "import org.slf4j.LoggerFactory;\n" +
                "import org.springframework.beans.factory.annotation.Autowired;\n" +
                "import org.springframework.stereotype.Component;\n" +
                "\n" +
                "import java.util.HashMap;\n" +
                "import java.util.List;\n" +
                "import java.util.Map;\n");

        data.append("import com.cmb.ccrm.customer.base.ErrorCode;\n" +
                "import com.cmb.ccrm.tool.base.util.MsgConstants;\n" +
                "import " + packagePath + ".service." + serviceName + ";\n" +
                "import com.cmb.framework.core.PRCModel;\n");
        data.append("\n");


        data.append("@Component(\"" + className.toUpperCase() + "\")\n" +
                "public class " + className.substring(4, className.length()) + " extends " + facadeParentClass + " {\n" +
                "    private static Logger logger = LoggerFactory.getLogger(" + className.substring(4, className.length()) + ".class);\n" +
                "\n" +
                "    private static final String " + xz + "X1 = \"" + xz + "X1\";\n" +
                "    private static final String " + xz + "Z1 = \"" + xz + "Z1\";\n" +
                "\n" +
                "    @Autowired\n" +
                "    private " + serviceName + " " + getStringFirst2lower(serviceName) + ";\n" +
                "\n" +
                "    @Override\n" +
                "    public PRCModel doBusiness(PRCModel prcModel) throws FrameworkException {\n" +
                "        try {\n" +
                "\n" +
                "            Map<String, Object> paramMap = new HashMap<>();\n" +
                "            List<Map<String, Object>> paramsList = prcModel.getInputInterface(" + xz + "X1);\n" +
                "            paramMap.putAll(paramsList.get(0));\n" +
                "\n" +
                "\n" +
                "            //todo \n" +
                "\n" +
                "            Map<String, Object> result = new HashMap<>();\n" +
                "            //todo \n" +
                "\n" +
                "            prcModel.createOuputInterface(" + xz + "Z1);\n" +
                "            prcModel.putOutputInterfaceData(" + xz + "Z1, result);\n" +
                "\n" +
                "            prcModel.setRtncod(MsgConstants.SUC0000);\n" +
                "        } catch (Exception e) {\n" +
                "            logger.error(\"出错信息: \", e);\n" +
                "            prcModel.setErrmsg(\"系统操作异常,请稍后再试!\");\n" +
                "            prcModel.setRtncod(MsgConstants.ERRMSG);\n" +
                "            throw new FrameworkException(ErrorCode.ERROR_CODE_0000001, e);\n" +
                "        }\n" +
                "        return prcModel;\n" +
                "    }\n" +
                "}");


        return data.toString();
    }


    String getStringFirst2lower(String name) {
        char[] chars = name.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }


}

MyModel.java

package com.cmb.ccrm.customer;


public class MyModel {

    private String packagePath;//所在包
    private String className;//facade Bean Name  要求长度大于11,前4位为工程编码,后6位为Facade,中间位数驼峰命名
    private String facadeParentClass;//facade 父类
    private String daoParentClass;//dao父类
    private String fileModelName;//模型


    public MyModel() {
    }

    public MyModel(String packagePath, String className, String facadeParentClass, String daoParentClass, String fileModelName) {
        this.packagePath = packagePath;
        this.className = className;
        this.facadeParentClass = facadeParentClass;
        this.daoParentClass = daoParentClass;
        this.fileModelName = fileModelName;
    }

    public String getPackagePath() {
        return packagePath;
    }

    public void setPackagePath(String packagePath) {
        this.packagePath = packagePath;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getFacadeParentClass() {
        return facadeParentClass;
    }

    public void setFacadeParentClass(String facadeParentClass) {
        this.facadeParentClass = facadeParentClass;
    }

    public String getDaoParentClass() {
        return daoParentClass;
    }

    public void setDaoParentClass(String daoParentClass) {
        this.daoParentClass = daoParentClass;
    }

    public String getFileModelName() {
        return fileModelName;
    }

    public void setFileModelName(String fileModelName) {
        this.fileModelName = fileModelName;
    }
}
相关标签: 工具