当前位置: 移动技术网 > IT编程>开发语言>Java > mybatis-plus 代码生成

mybatis-plus 代码生成

2019年01月14日  | 移动技术网IT编程  | 我要评论

之前采用maven-generator 生成代码,在配置文件上因为从单模块到多模块时出现问题,
改用mybatis-plus自动生成代码。

码云地址:

github地址:

依赖

<dependency>
    <groupid>com.baomidou</groupid>
    <artifactid>mybatis-plus</artifactid>
    <version>3.0.6</version>
</dependency>
<dependency>
    <groupid>org.apache.velocity</groupid>
    <artifactid>velocity</artifactid>
    <version>1.7</version>
</dependency>

单模块生成

public class generator {

    public static void main(string[] args) {
        string [] tablenames = new string[]{"表名1","表名2"};

        string location = "代码生成路径位置";//例如: com/cn/jzedy
        generator(location,tablenames);
    }

    private static void generator(string location,string [] tablenames){

        globalconfig globalconfig = new globalconfig();// 全局配置
                globalconfig.setopen(false)//是否打开输出目录 默认true
                        .setoutputdir(location)//生成文件的输出目录
                        .setfileoverride(true)//是否覆盖已有文件 默认false
                        .setbaseresultmap(true)//开启 baseresultmap 默认false
                        .setbasecolumnlist(true)//开启 basecolumnlist 默认false
                        .setactiverecord(false)//开启 activerecord 模式 默认false
                        .setauthor("jzedy")//开发人员
                        .setservicename("%sservice");//service 命名方式 例如:%sbusiness 生成 userbusiness

        
        datasourceconfig datasourceconfig = new datasourceconfig();// 数据源配置
        datasourceconfig.setdbtype(dbtype.mysql)
                .setdrivername(driver.class.getname())
                .setusername("数据库连接名称")
                .setpassword("数据库连接密码")
                .seturl("url地址");

        packageconfig packageconfig = new packageconfig();// 包配置
        packageconfig.setparent(location)
                .setentity("entity")//entity包名
                .setmapper("mapper")//mapper包名
                .setservice("service")
                .setcontroller("controller");

        strategyconfig strategyconfig = new strategyconfig();// 策略配置
                strategyconfig
                        .setcapitalmode(true)//驼峰命名
                        .setentitylombokmodel(false)//【实体】是否为lombok模型(默认 false)
                        .setrestcontrollerstyle(false)//生成 @restcontroller 控制器
                        .setnaming(namingstrategy.underline_to_camel)//数据库表映射到实体的命名策略,该处下划线转驼峰命名
                        .setinclude(tablenames);//需要包含的表名,允许正则表达式(与exclude二选一配置)


        new autogenerator()//// 代码生成器
                .setglobalconfig(globalconfig)
                .setdatasource(datasourceconfig)
                .setpackageinfo(packageconfig)
                .setstrategy(strategyconfig)
                .execute();

    }


}

具体配置查阅

多模块配置

因为项目分模块时候 想代码自动生成在对应位置,例如下面代码 将entity mapper service代码生成在
service模块中,controller生成在web模块中。可以自行模块进一步细分,只是对代码生成路径在但模块上
基础上调整而已。同时下述代码的代码生成模板是采用mybatis-plus自己的模板,若需自定义模板不再此论述,

public class generator {

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

        string [] modules = new string[]{"service","web"};//项目模块名,需自定义
        for (string module : modules) {
            modulegenerator(module,tablenames);
        }
    }

    private static void modulegenerator(string module,string [] tablenames){

        globalconfig globalconfig = getglobalconfig(module);// 全局配置

        datasourceconfig datasourceconfig = getdatasourceconfig();// 数据源配置

        packageconfig packageconfig = getpackageconfig(module);// 包配置

        strategyconfig strategyconfig = getstrategyconfig(tablenames);// 策略配置

        templateconfig templateconfig = gettemplateconfig(module);// 配置模板

        new autogenerator()
                .setglobalconfig(globalconfig)
                .setdatasource(datasourceconfig)
                .setpackageinfo(packageconfig)
                .setstrategy(strategyconfig)
                .settemplate(templateconfig)
                .execute();

    }

    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模板采用mybatis-plus自己模板
                    .setxml(new templateconfig().getxml())
                    .setservice(new templateconfig().getservice())
                    .setserviceimpl(new templateconfig().getserviceimpl())
                    .setcontroller(null);//service模块不生成controller代码
        }else if ("web".equals(module)){//web模块只生成controller代码
            templateconfig.setentity(null)
                    .setmapper(null)
                    .setxml(null)
                    .setservice(null)
                    .setserviceimpl(null)
                    .setcontroller(new templateconfig().getcontroller());
        }else throw new illegalargumentexception("参数匹配错误,请检查");
        return templateconfig;
    }

    private static strategyconfig getstrategyconfig(string[] tablenames) {
        strategyconfig strategyconfig = new strategyconfig();
        strategyconfig
                .setcapitalmode(true)//驼峰命名
                .setentitylombokmodel(false)
                .setrestcontrollerstyle(false)
                .setnaming(namingstrategy.underline_to_camel)
                .setinclude(tablenames);
        return strategyconfig;
    }

    private static packageconfig getpackageconfig(string module) {
        packageconfig packageconfig = new packageconfig();
        string packagename = "com.cn.jzedy";//不同模块 代码生成具体路径自定义指定
        if ("service".equals(module)){
            packagename+=".web";
        }else if ("web".equals(module)){

        }
        packageconfig.setparent(packagename)
                .setentity("entity")
                .setmapper("mapper")
                .setservice("service")
                .setcontroller("controller");
        return packageconfig;
    }

    private static datasourceconfig getdatasourceconfig() {
        string dburl = "jdbc:mysql://localhost:3306/z-blogs";
        datasourceconfig datasourceconfig = new datasourceconfig();
        datasourceconfig.setdbtype(dbtype.mysql)
                .setdrivername(driver.class.getname())
                .setusername("root")
                .setpassword("root")
                .seturl(dburl);
        return datasourceconfig;
    }

    private static globalconfig getglobalconfig(string module) {
        globalconfig globalconfig = new globalconfig();
        globalconfig.setopen(false)//new file(module).getabsolutepath()得到模块根目录路径,因事maven项目,代码指定路径自定义调整
                .setoutputdir(new file(module).getabsolutepath()+"/src/main/java")//生成文件的输出目录
                .setfileoverride(true)//是否覆盖已有文件
                .setbaseresultmap(true)
                .setbasecolumnlist(true)
                .setactiverecord(false)
                .setauthor("jzedy")
                .setservicename("%sservice");
        return globalconfig;
    }

}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网