All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.slyang.devtool.MybatisGenerator Maven / Gradle / Ivy

There is a newer version: 1.3.8.RELEASE
Show newest version
package io.github.slyang.devtool;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import io.github.slyang.core.model.BaseEntity;
import lombok.Data;
import lombok.experimental.Accessors;


/**
 * Description:
 * Created by slyang 
 * Copyright (c) 2019, All Rights Reserved.
 */
public interface MybatisGenerator {

    @Data
    @Accessors(chain = true)
    class GeneratorConfig {
        private String outputDir = "out/mybatis_generate";
        private String dbUrl = "jdbc:mysql://localhost:3306/hyht_blockchain?useSSL=true";
        private String dbDiver = "com.mysql.jdbc.Driver";
        private String packageName = "com.example.slyangsecurity.modules.block";
        private String dbUserName = "root";
        private String dbPassword = "123456";
        private String author = "author";
        private String[] tableNames = new String[]{"bc_chaincode"};

        private String[] tablePrefix;
        private Class entityClass;
    }

    static AutoGenerator generateByTables(GeneratorConfig generatorConfig) {

        GlobalConfig config = new GlobalConfig()
                .setActiveRecord(false)
                .setAuthor(generatorConfig.getAuthor())
                .setDateType(DateType.ONLY_DATE)
                .setOutputDir(generatorConfig.getOutputDir())
                .setIdType(IdType.ASSIGN_UUID)
                .setServiceName("%sService")
                .setEntityName("%sEntity")
                .setBaseResultMap(true)   // BaseResultMap
                .setBaseColumnList(true)  // BaseColumnList
                .setFileOverride(true);

        DataSourceConfig dataSourceConfig = new DataSourceConfig()
                .setUrl(generatorConfig.getDbUrl())
                .setUsername(generatorConfig.getDbUserName())
                .setPassword(generatorConfig.getDbPassword())
                .setDriverName(generatorConfig.getDbDiver());

        StrategyConfig strategyConfig = new StrategyConfig()
                .setSuperEntityClass(generatorConfig.entityClass != null ?
                        generatorConfig.entityClass : BaseEntity.class) // base entity
                .setSuperMapperClass("io.github.slyang.core.components.mybatisplus.BaseCurdMapper")
                .setSuperServiceClass("io.github.slyang.core.components.mybatisplus.BaseService")
                .setSuperServiceImplClass("io.github.slyang.core.components.mybatisplus.BaseServiceImpl")
                .setEntityTableFieldAnnotationEnable(true) // 生成 字段注解
                .setCapitalMode(false)
                .setEntityLombokModel(true)
                .setTablePrefix(generatorConfig.tablePrefix)
                .setEntityBuilderModel(true)  // 对象为建造者模型
                .setRestControllerStyle(true)  // restcontroller
                .setNaming(NamingStrategy.underline_to_camel) // 下划线转驼峰命名 表名
                .setColumnNaming(NamingStrategy.underline_to_camel) // 下划线转驼峰命名 字段名
                .setInclude(generatorConfig.getTableNames());//修改替换成你需要的表名,多个表名传数组

        // InjectionConfig
        return new AutoGenerator()
                .setTemplateEngine(new FreemarkerTemplateEngine())
                .setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(generatorConfig.getPackageName())
                                .setController("controller")
                                .setEntity("entity")
                );
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy