Java code generator

Full automatic code generator

Project address

https://github.com/GitHub-Laziji/mybatis-generator

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

spring:
  datasource:
    url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxx?characterEncoding=utf-8
    username: xxxxxx
    password: xxxxxx

generator:
  package: com.xxx.xxx
  template:
    path: mybatis
    mapping: |
      Model.java.vm: main/java/{packageFilePath}/database/model/{className}.java
      Query.java.vm: main/java/{packageFilePath}/database/query/{className}Query.java
      Dao.java.vm: main/java/{packageFilePath}/database/dao/{className}.java
      Service.java.vm: main/java/{packageFilePath}/database/service/{className}Service.java
      Mapper.xml.vm: main/resources/mapper/{className}Mapper.xml

Use

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;
import java.io.IOException;

@ActiveProfiles("example")
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExampleTest {

    @Resource
    private GeneratorService generatorService;

    @Test
    public void test(){
        String zipPath = "/home/code.zip";

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

        TableItem[] tableItems = new TableItem[]{
                new TableItem("table1", "TableA"),
                new TableItem("table2", "TableB")
        };
        generatorService.generateZip(tableItems,zipPath);
    }
}

Welcome to my blog public address.

Tags: Java Mybatis Database github

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