直接跳到内容

代码生成器

技术交流QQ群:3549030

代码生成器的作用?

代码生成器是一种工具,可以根据用户的需求自动生成 Java Web 应用程序的代码,它能够大幅度地减少开发人员的工作量,提高开发效率和代码质量。主要的作用有以下几个方面:

  1. 加速项目开发:代码生成器可以根据用户提供的数据模型、数据表结构和配置文件等信息,自动生成相应的 Java 类、配置文件和 SQL 脚本等,大幅度减少开发人员的工作量,缩短项目开发周期。

  2. 降低代码错误率:代码生成器可以自动生成符合编码规范和最佳实践的代码,避免因编码不规范而引入的常见错误,如空指针、类型转换等。

  3. 提高代码可维护性:代码生成器可以生成结构清晰、易于理解和维护的代码,使得后续的维护和升级更加容易和快速。

  4. 可定制化和可扩展性:代码生成器通常提供了丰富的配置选项和插件机制,开发人员可以根据自己的需求进行自定义配置和功能扩展,以满足不同的业务需求和技术场景。

  5. 促进团队协作:代码生成器可以帮助团队建立一致的编码风格和技术规范,减少因个人编码风格不同而引发的代码冲突和维护困难等问题,有助于提高团队协作效率。

综上所述,Java Web 代码生成器可以极大地提高项目开发效率和代码质量,减少开发人员的工作量,是现代软件开发过程中必不可少的工具之一。

代码生成器的使用配置

数据库配置

包括数据库服务器的IP地址及端口、数据库名称、数据库用户及数据库密码几个配置项。如下:

java
String databaseHost = "127.0.0.1:3306";
String databaseName = "database-name";
String username = "database-user";
String password = "database-password";

后端服务包与模块配置

基础包名和模块名两个配置项。如下:

java
String basePackage = "io.kungfu.panda";
String moduleName = "sys";

数据库表查询配置

配置本次生成包含的表,及本次生成排出的表。支持单个或多个。如果不填,查询数据库所有的表。可以灵活配置本次 代码生成的数据表范围。

  • 情况一:支持精准匹配
java
String includeTables = "sys_menu,sys_user,sys_role";
String excludeTables = "sys_role_menu,sys_user_role";
  • 情况二:支持模糊匹配: %mid%、prefix%、%suffix三种情况均支持。
java
String includeTables = "sys_%";
String excludeTables = "%_copy";

生成代码层配置

配置本次生成代码层级范围,可以具体指定一个或多个层的代码生成。做到灵活方便。示例如下:

java
String[] genLayers = new String[]{"base", "model", "dto", 
"validate", "service", "controller"};
//genLayers = new String[]{"dto"};
//genLayers = new String[]{"service", "controller"};
//genLayers = new String[]{"base", "model", "dto", "validate"};
//genLayers = new String[]{"dto", "validate"};
genLayers = new String[]{"validate"};
//genLayers = new String[]{"web_api"};

自定义代码模板配置

Kungfu开发库内置了一套标准的代码生成模板,为了更方便扩展,支持本地自定义代码模板。来满足代码的定制性。

java
Map<String, String> templateMap = new HashMap<>();
templateMap.put("base", "code/basemodel.tpl");
templateMap.put("model", "code/model.tpl");
templateMap.put("dto", "code/dto.tpl");
templateMap.put("validate", "code/validate.tpl");
templateMap.put("service", "code/service.tpl");
templateMap.put("controller", "code/controller.tpl");
//templateMap.put("service", "code/tree_service.tpl");
//templateMap.put("controller", "code/tree_controller.tpl");
templateMap.put("web_api", "code/web_api.tpl");
templateMap.put("web_index", "code/web_index.tpl");
templateMap.put("web_popup", "code/web_popup.tpl");

完整的代码生成示例

java
public class CodeGenerator {

    public static void main(String[] args) {
        String databaseHost = "127.0.0.1:3306";
        String databaseName = "database-name";
        String username = "database-user";
        String password = "database-password";

        String basePackage = "io.kungfu.panda";
        String moduleName = "system";

        String includeTables = "sys_%";
        String excludeTables = "";

        String[] genLayers = new String[]{"base", "model", "dto", 
        "validate", "service", "controller", "web_api"};
        //genLayers = new String[]{"dto"};
        //genLayers = new String[]{"service", "controller"};
        //genLayers = new String[]{"base", "model", "dto", "validate"};
        //genLayers = new String[]{"dto", "validate"};
        genLayers = new String[]{"validate"};
        //genLayers = new String[]{"web_api"};

        // 自定义模板
        Map<String, String> templateMap = new HashMap<>();
        templateMap.put("base", "code/basemodel.tpl");
        templateMap.put("model", "code/model.tpl");
        templateMap.put("dto", "code/dto.tpl");
        templateMap.put("validate", "code/validate.tpl");
        templateMap.put("service", "code/service.tpl");
        templateMap.put("controller", "code/controller.tpl");
        //templateMap.put("service", "code/tree_service.tpl");
        //templateMap.put("controller", "code/tree_controller.tpl");
        templateMap.put("web_api", "code/web_api.tpl");
        templateMap.put("web_index", "code/web_index.tpl");
        templateMap.put("web_popup", "code/web_popup.tpl");

        KungfuGenerator generator = new KungfuGenerator();

        generator.init(databaseHost, databaseName, username, password);

        generator.doGenerate(databaseName, basePackage, moduleName, 
        includeTables, excludeTables, genLayers, templateMap);

    }
}

简单的数行代码,业务代码就可以运行生成了。

代码生成器已经加载完毕