Java code generator

Full automatic code generator

Project address

The code template is located under resources and can be modified as needed

The first template is based on commons-mybatis If it is not suitable, you can modify the template or select another template

configuration file

Create the application-${name}.yml file under resources, ${name} at will, for example: application-example.yml, you can create multiple

The contents of the configuration file are as follows, fill in the database configuration and the package name of the generated code

Template file mapping is used to customize the package format and file name of the generated file

Dynamic properties contain

  • {packageFilePath} package file path for example: com/xxx/xxx
  • The {className} class name is obtained by changing the table name to the hump name
  • {suffix} class name suffix DO or VO

Generally, it can be configured as follows

Now there are two templates in the project template.path. You can choose mybatis or mybatis default

It can also be expanded by itself

    url: jdbc:mysql://
    username: xxxxxx
    password: xxxxxx

    path: mybatis
    mapping: | main/java/{packageFilePath}/database/model/{className}.java main/java/{packageFilePath}/database/query/{className} main/java/{packageFilePath}/database/dao/{className}.java main/java/{packageFilePath}/database/service/{className}
      Mapper.xml.vm: main/resources/mapper/{className}Mapper.xml


Create a test class under the test file

  • @Fill in the name of the profile name in ActiveProfiles("example")
  • tableNames are the tables that need to be generated. There can be multiple
  • zipPath code export path

Call generatorService.generateZip and the incoming parameter can be the table name array String [] or TableItem []

Run test method

package pg.laziji.generator;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import pg.laziji.generator.mybatis.GeneratorService;

import javax.annotation.Resource;

public class ExampleTest {

    private GeneratorService generatorService;

    public void test(){
        String zipPath = "/home/";

//        String[] tableNames = new String[]{"table1","table2"};
//        generatorService.generateZip(tableNames,zipPath);

        TableItem[] tableItems = new TableItem[]{
                new TableItem("table1", "TableA"),
                new TableItem("table2", "TableB")

Welcome to my blog public address.

Tags: Java Mybatis Database github

Posted on Tue, 03 Dec 2019 10:28:04 -0500 by _OwNeD.YoU_