Mybatis plus code generation

Previously, Maven generator was used to generate code. There was a problem in the configuration file from single module to multi module,
Use mybatis plus to generate code automatically instead.

Code cloud address:

github Address:

rely on


Single module generation

public class Generator {

    public static void main(String[] args) {
        String [] tableNames = new String[]{"Table name 1","Table name 2"};

        String location = "Code generation path location";//For example: com/cn/jzedy

    private static void generator(String location,String [] tableNames){

        GlobalConfig globalConfig = new GlobalConfig();// Global configuration
                globalConfig.setOpen(false)//Open output directory default true
                        .setOutputDir(location)//Output directory of the build file
                        .setFileOverride(true)//Whether to overwrite the existing file default false
                        .setBaseResultMap(true)//Enable BaseResultMap default false
                        .setBaseColumnList(true)//Enable baseColumnList default false
                        .setActiveRecord(false)//Enable ActiveRecord mode default false
                        .setServiceName("%sService");//service naming method for example:% sBusiness generates UserBusiness

        DataSourceConfig dataSourceConfig = new DataSourceConfig();// Data source configuration
                .setUsername("Database connection name")
                .setPassword("Database connection password")
                .setUrl("url address");

        PackageConfig packageConfig = new PackageConfig();// Packet configuration
                .setEntity("entity")//Entity package name
                .setMapper("mapper")//mapper package name

        StrategyConfig strategyConfig = new StrategyConfig();// Policy configuration
                        .setCapitalMode(true)//Hump naming
                        .setEntityLombokModel(false)//Whether [entity] is a lombok model (default false)
                        .setRestControllerStyle(false)//Generate @ RestController controller controller
                        .setNaming(NamingStrategy.underline_to_camel)//The naming strategy of database table mapping to entity, where underline is transferred to hump naming
                        .setInclude(tableNames);//The name of the table to be included. Regular expressions are allowed (either with exclude configuration)

        new AutoGenerator()////Code generator



Refer to specific configuration

Multi module configuration

For example, the following code will generate entity mapper service code in the corresponding location when the project is divided into modules
In the service module, the controller is generated in the web module. The module can be further subdivided by itself, only the code generation path is on the module
On the basis of the adjustment. At the same time, the code generation template of the following code is mybatis plus's own template. If you need to customize the template, it will not be discussed,

public class Generator {

    public static void main(String[] args) {
        String [] tableNames = new String[]{"users","roles"};

        String [] modules = new String[]{"service","web"};//Project module name, to be customized
        for (String module : modules) {

    private static void moduleGenerator(String module,String [] tableNames){

        GlobalConfig globalConfig = getGlobalConfig(module);// Global configuration

        DataSourceConfig dataSourceConfig = getDataSourceConfig();// Data source configuration

        PackageConfig packageConfig = getPackageConfig(module);// Packet configuration

        StrategyConfig strategyConfig = getStrategyConfig(tableNames);// Policy configuration

        TemplateConfig templateConfig = getTemplateConfig(module);// Configuration template

        new AutoGenerator()


    private static TemplateConfig getTemplateConfig(String module) {
        TemplateConfig templateConfig = new TemplateConfig();
        if ("service".equals(module)){
            templateConfig.setEntity(new TemplateConfig().getEntity(false))
                    .setMapper(new TemplateConfig().getMapper())//mapper template uses mybatis plus template
                    .setXml(new TemplateConfig().getXml())
                    .setService(new TemplateConfig().getService())
                    .setServiceImpl(new TemplateConfig().getServiceImpl())
                    .setController(null);//The service module does not generate controller code
        }else if ("web".equals(module)){//web module only generates controller code
                    .setController(new TemplateConfig().getController());
        }else throw new IllegalArgumentException("Parameter matching error, please check");
        return templateConfig;

    private static StrategyConfig getStrategyConfig(String[] tableNames) {
        StrategyConfig strategyConfig = new StrategyConfig();
                .setCapitalMode(true)//Hump naming
        return strategyConfig;

    private static PackageConfig getPackageConfig(String module) {
        PackageConfig packageConfig = new PackageConfig();
        String packageName = "";//User defined path for code generation of different modules
        if ("service".equals(module)){
        }else if ("web".equals(module)){

        return packageConfig;

    private static DataSourceConfig getDataSourceConfig() {
        String dbUrl = "jdbc:mysql://localhost:3306/z-blogs";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        return dataSourceConfig;

    private static GlobalConfig getGlobalConfig(String module) {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOpen(false)//new File(module).getAbsolutePath() gets the root directory path of the module. For Maven project, the code specifies the path for custom adjustment
                .setOutputDir(new File(module).getAbsolutePath()+"/src/main/java")//Output directory of the build file
                .setFileOverride(true)//Overwrite existing file or not
        return globalConfig;


Keywords: Java Mybatis MySQL Database Maven

Added by jennatar on Mon, 09 Dec 2019 02:47:50 +0200