easygen generic code generation framework [open source]

What is it?

My classmates who have used mybatis know that it's painful to write mapper and xml by hand. Fortunately, Mybatis-Generator is provided by the government, but the things generated by this guy are not open and inconvenient to modify, and the code generation requirements of the project are not only data access layer, such as view, service, controller, which are full of duplicate code everywhere. What's a good way to do that? Anyway, I have little knowledge and can only do it by myself.

Design Thought

Simple, simple design ideas, only simple others are good at it.

  • 1. Loading model by template engine, rendering template and generating file.
  • 2. The model is loaded by the model builder. mysql metadata is built into the model to support customizing other model builders.
  • 3. Separate the model from the template file so that the model can be reused to other templates
  • 4. The model and template files are one-to-many relationships. Aspects generate po, mapper, service according to a table.
  • 5. Supporting batch generation of a model, such as database tables, can't each table correspond to a model.

Don't think so much about it first. That's all.

Design of usage mode

Many students write code as soon as they come up. Finally, according to the function design, I prefer to design the way of use first. As the saying goes, a good beginning is half the success.

  1. Code generation is a little simpler, so java -jar xxxx.jar, and then configure alias in ohmyzsh.
  2. Configuration files are easy to maintain with json. File names and paths are default and support modification.
  3. Publishing packages provide templates for easy learning and use.

Okay, first YY a configuration file, see:

{
  "modelBuilders": [
    {
      "name": "mysql", //Model builder name
      "type": "db:mysql", //Builder type
      "configPath": "modelBuilders/mysql.json" //Database Configuration
    }
  ],
  "templates": [
    {
      "name": "beanClass",
      "modelBuilderName": "mysql", //Associated model builder name
      "templateFilename": "beanClass.ftl",
      "outputPath": "output/bean"
    }
  ]
}

Well, that's it. Is it a little dizzy? Let me explain.

  • 1. First, our framework loads the configuration file.
  • 2. Then load the model builder of the specified type according to the model Builders in the configuration file
  • 3. Then pass the configuration file specified by configPath to the model builder for building the data model
  • 4. The framework associates the built model builder according to the modelBuilderName attribute of each element in the templates in the configuration file.
  • 5. The framework then builds the Model using the Model builder and renders it according to the template file specified by template Filename (using freemarker)
  • 6. Output file to outputPath

Final effect

github: https://github.com/BigMaMonkey/easygen

In fact, the code of the framework is very simple. The key point is to design a good way to use it. It is simple and extensible. In fact, some other features are needed in the process.

  • freemarker-based template grammar, easy to use
  • Built-in mysql, json model builder, and support customization
  • Support for generating specified tables, removing table prefixes, converting case, etc.
  • Support one-to-many relationship between model and model (separation of model and model)
  • Support List type model, batch generation.
  • Supporting custom filename generation rules
  • Support for custom output paths
  • Support template customization parameters

The following example demonstrates the code generation usage applied to mybatis.

1. First download the easygen distribution package download

After decompression, it contains the following parts:

  • easygen-1.0-SNAPSHOT.jar: This is the easygen main program, responsible for reading configuration files and generating code
  • config.json: This is the easy master configuration file
  • modelBuildrs: Here's the configuration file for the model builder
  • templates: Here's the template file
  • lib: The third-party jar package referenced by easygen is stored here

2. Write the master configuration file

Cong. JSON is the main configuration file in the directory. The distribution package has given a relatively complete configuration, including the type builder configuration and template configuration, including the generation of bean s, mapperclass, mapperxml, service interface, service Impl, view, dbtojson (generating database metadata). It's easy to see the configuration and code by yourself.

{
  "modelBuilders": [
    {
      "name": "mysql", //Builder name should be unique
      "type": "db:mysql", //Builder types currently support only mysql and json
      "configPath": "modelBuilders/mysql.json" //Database Configuration and Generation Table Specification
    },
    {
      "name": "json",
      "type": "json",
      "configPath": "modelBuilders/data.json" //Unlike the mysql builder, here is the model data directly.
    },
    {
      "name": "http",
      "type": "custom", //Custom Builder
      "configPath": "modelBuilders/xxx.json", //This json deserialization type needs to be pointed out in the generic parameter of the IModelBuilder.
      "modelBuilderClassName": "org.BigMaMonkey.XXX.XXX" //Custom builders need to implement classes to implement IModelBuilder excuses
    }
  ],
  "templates": [
    {
      "name": "mapperXml",
      "modelBuilderName": "json", //Associated builder name, not type
      "templateFilename": "data.ftl", //ftl template file
      "outputPath": "output/data", //The output directory will automatically create the directory recursively.
      "outputFilenameRule": "data_{name}.xml", //Output filename rule,{}Fields with internal variables as models field
      "options": {} //Custom template parameters, can be used in the template, please play freely.
    },
    {
      "name": "mapperXml",
      "modelBuilderName": "mysql",
      "templateFilename": "mapperXml.ftl",
      "outputPath": "output/mapperXml",
      "outputFilenameRule": "{upperCaseName}Mapper.xml",
      "options": {
        "mapperns": "org.bigmonkey.robot.mapper",
        "pons": "org.bigmonkey.robot.entity.po"
      }
    },
    {
      "name": "beanClass",
      "modelBuilderName": "mysql",
      "templateFilename": "beanClass.ftl",
      "outputPath": "output/bean",
      "outputFilenameRule": "{upperCaseName}.java",
      "options": {
        "pons": "org.bigmonkey.robot.entity.po"
      }
    },
    {
      "name": "mapperClass",
      "modelBuilderName": "mysql",
      "templateFilename": "mapperClass.ftl",
      "outputPath": "output/mapper",
      "outputFilenameRule": "{upperCaseName}Mapper.java",
      "options": {
        "pons": "org.bigmonkey.robot.entity.po",
        "mpns": "org.bigmonkey.robot.mapper"
      }
    },
    {
      "name": "serviceInterface",
      "modelBuilderName": "mysql",
      "templateFilename": "serviceInterface.ftl",
      "outputPath": "output/service",
      "outputFilenameRule": "I{simpleName}Service.java",
      "options": {
        "pons": "org.bigmonkey.robot.entity.po",
        "itns": "org.bigmonkey.robot.service"
      }
    },
    {
      "name": "serviceImpl",
      "modelBuilderName": "mysql",
      "templateFilename": "serviceImpl.ftl",
      "outputPath": "output/service",
      "outputFilenameRule": "{simpleName}ServiceImpl.java",
      "options": {
        "pons": "org.bigmonkey.robot.entity.po",
        "imns": "org.bigmonkey.robot.service.impl"
      }
    },
    {
      "name": "view",
      "modelBuilderName": "mysql",
      "templateFilename": "view.ftl",
      "outputPath": "output/view",
      "outputFilenameRule": "{name}view.vue",
      "options": {}
    },
    {
      "name": "db-to-json",
      "modelBuilderName": "mysql",
      "templateFilename": "dbtojson.ftl",
      "outputPath": "output/json",
      "outputFilenameRule": "{name}.json",
      "options": {}
    }
  ]
}

3. Writing Templates
In fact, template writing is very simple, you just need to master freemarker ftl grammar, as well as basic json knowledge.
Here's just about the use of mybatis built-in builders

3.1. First, the builder configuration

{
  "name": "mysql", //Builder name should be unique
  "type": "db:mysql", //Builder types currently support only mysql and json
  "configPath": "modelBuilders/mysql.json" //Database Configuration and Generation Table Specification
}

db:mysql in type specifies the use of the built-in mysql builder, configPath specifies the configuration file for the builder, which is configured as follows:

{
  "dbUrl": "jdbc:mysql://localhost:3306/device_manage",
  "driverClassName": "com.mysql.jdbc.Driver",
  "username": "root",
  "password": "root",
  "tables": "sys_user,pub_dict" //use,Split the specified generated table name, or leave blank to generate all tables
}

3.2. What you need to give is the model structure of the built-in mysql builder so that you can use it in ftl

{
  "name": "sys_user", //Original table name
  "upperCaseName": "SYS_User", //Prefix capitalization + first character capitalization table name for creating PO, Mapper, etc.
  "simpleName": "User", // Remove prefix table names, currently only supported_Split table names, such as sys_user
  "pkgs": [
    "java.util.Date" // Fieldtype corresponding Java Bag, import reach java file
  ],
  "fields": [
    {
      "name": "name", //Original field name
      "upperCaseName": "Name", //Capital paragraph name
      "dataType": "12", // Field type value, corresponding java.sql.Types Enumeration values in
      "typeName": "VARCHAR", //Field database type, see source code for other types: TableField.java
      "columnSize": "32", //Field size
      "columnType": {
        "javaType": "String", //Corresponding java type
        "pkg": null  //The base type is null, no import is required
      }
    }
  ],
  "primaryKey": {
    //Element attributes in the same field
  }
}

In ftl, the model follows the model, for example, if you want to output a table name ${model.name} in ftl, or the name of the primary key ${model.primaryKey.name}

If you set custom template parameter options in the template configuration, use it in this way: ${options.xxx}

By using {XXX} in output Filename Rule, you can refer to the root attribute in the model to customize the output file name, such as {upperCaseName}Mapper.java.

4. The final usage is quite simple:

java -jar easygen-1.0-SNAPSHOT.jar 

Of course, you can also integrate code execution into your own projects or tools:

public class App {
    public static void main(String[] args) {
        GeneratorManager generatorManager = new GeneratorManager();
        try {
            generatorManager.Start();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

Keywords: Java MySQL JSON Database

Added by Ironphp on Sat, 22 Jun 2019 00:34:57 +0300