项目中的代码生成器怎么做?

先po张效果图

项目中的代码生成器怎么做?

如图

  • 输入entity名字
  • 选择项目, 具体可以用项目中的模块来做

    这个工具目前生成的内容包含:DTO,Controller, Service, Mapper, sqlmap 另外还会根据yaml配置文件在dev环境下生成基本的数据表结构,其中包含了几个常见接口,比如,添加、更新、删除、根据id查询,列表查询这些接口。

使用方法:

    1. 独立启动,浏览器访问项目根目录/

    2. 添入实体名,属性,并选择对应的项目 

    3. (可选)在高级选项添加入注释的作者或者表前缀等信息 

    4. 点击一键生成

关键代码

 @Override
    public void generate(GenerateConfig cfg) throws Exception {
        long startTime = System.currentTimeMillis();
        if (StringUtils.isEmpty(cfg.getModelName())) {
            ExceptionHolder.throwParaErrorException();
        }

        Map<String, String> bindMap = getBindMap(cfg);

        //生成ResultDto ExceptionHolder etc..
        createCommonClass(bindMap, cfg);
        //生成model
        createModel(bindMap, cfg);

        //生成控制器
        createController(bindMap, cfg);
        //生成service
        //createService(bindMap, cfg);
        //生成service 实现类

        createServiceImpl(bindMap, cfg);
        //生成dao
        createMapper(bindMap, cfg);

        //生成sqlmap
        if (cfg.getIsCreateSqlMap()) {
            createSqlMapper(bindMap, cfg);
        }
        //建表
        if (cfg.getIsCreateTable()) {
            createTable(cfg);
        }

        double s = (System.currentTimeMillis() - startTime) / 1000D;
        System.out.println("Generate use " + s + " seconds");
    }
 private void createModel(Map bindMap, GenerateConfig cfg) throws FileNotFoundException {
        String model = cfg.getModelName();
        Template template = groupTemplate.getTemplate("/Model.tpl");
        String modelPackage = getModelPackageToFileSeparator(cfg);

        String packagePath = cfg.getBasePackage() + "entity" + modelPackage;
        String modelPath = CHAR_SEPARATOR + model + ".java";

        String path = getCreatePath(cfg, packagePath);

        //fields
        String fieldDisplay = getFieldDisplay(cfg);
        bindMap.put("fieldDefinition", fieldDisplay);
        //setter and getter
        String[] split = cfg.getFieldSplit();
        StringBuilder setget = new StringBuilder();
        for (String e : split) {
            if (e.length() > 3) {
                String[] field = e.trim().replaceAll(LINE_RN, "").split(LINE_SS);
                String methodName = StringUtil.toFirstUpCase(field[1]);
                setget.append("\t\tpublic void set" + methodName + "(" + field[0] + " " + field[1] + ")");
                setget.append("{\n\t\tthis." + field[1] + " = " + field[1] + ";\n\t}\n");

                setget.append("\t\tpublic " + field[0] + " get" + methodName + "()");
                setget.append("{\n\t\treturn this." + field[1] + ";\n\t}\n");
            }
        }
        bindMap.put("setget", setget);
        rend(template, path, modelPath, bindMap);
    }
private void createController(Map<String, String> bindMap, GenerateConfig cfg) throws Exception {
        try {

            Template template = groupTemplate.getTemplate("/Controller.tpl");

            String model = cfg.getModelName();
            String modelPackage = getModelPackageToFileSeparator(cfg);

            String packagePath = cfg.getBasePackage() + "controller" + modelPackage;
            String modelPath = CHAR_SEPARATOR + model + "Controller.java";

            String path = getCreatePath(cfg, packagePath);

            rend(template, path, modelPath, bindMap);

        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 生成service
     *
     * @param bindMap
     * @param cfg
     * @throws IOException
     */
    @Deprecated
    private void createService(Map<String, String> bindMap, GenerateConfig cfg) throws IOException {
        try {

            Template template = groupTemplate.getTemplate("/Service.tpl");

            String model = cfg.getModelName();
            String modelPackage = getModelPackageToFileSeparator(cfg);

            String packagePath = cfg.getBasePackage() + "service" + modelPackage;
            String modelPath = CHAR_SEPARATOR + model + "Service.java";

            String path = getCreatePath(cfg, packagePath);

            rend(template, path, modelPath, bindMap);

        } catch (Exception e) {
            throw e;
        }
    }
/**
     * 生成service实现类
     *
     * @param bindMap
     * @param cfg
     * @throws IOException
     */
    private void createServiceImpl(Map<String, String> bindMap, GenerateConfig cfg) throws IOException {
        try {

            Template template = groupTemplate.getTemplate("/ServiceImpl.tpl");

            String model = cfg.getModelName();
            String modelPackage = getModelPackageToFileSeparator(cfg);

            String packagePath = cfg.getBasePackage() + "service" + modelPackage;
            String modelPath = CHAR_SEPARATOR + model + "Service.java";

            String path = getCreatePath(cfg, packagePath);
            rend(template, path, modelPath, bindMap);

        } catch (Exception e) {
            throw e;
        }
    }

    private void createMapper(Map bindMap, GenerateConfig cfg) throws Exception {
        try {

            Template template = groupTemplate.getTemplate("/Mapper.tpl");

            String model = cfg.getModelName();
            String modelPackage = getModelPackageToFileSeparator(cfg);
            String packagePath = cfg.getBasePackage() + "mapper" + modelPackage;
            String modelPath = CHAR_SEPARATOR + model + "Mapper.java";

            String path = getCreatePath(cfg, packagePath);

            rend(template, path, modelPath, bindMap);

        } catch (Exception e) {
            throw e;
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章