当前位置: 移动技术网 > IT编程>开发语言>Java > MyBatis-Plus Generator配置详解

MyBatis-Plus Generator配置详解

2020年08月27日  | 移动技术网IT编程  | 我要评论
本文仅对使用mybatis-plus的代码生成器配置做保存,适合使用了该插件的童鞋做参考。内部有大量默认配置,有性趣的童鞋可以研究下源码。ps:官方文档更齐全package com.kichun.uc

本文仅对使用mybatis-plus的代码生成器配置做保存,适合使用了该插件的童鞋做参考。

内部有大量默认配置,有性趣的童鞋可以研究下源码。

ps:官方文档更齐全

package com.kichun.ucenter.service;

import com.baomidou.mybatisplus.generator.autogenerator;
import com.baomidou.mybatisplus.generator.injectionconfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.mysqltypeconvert;
import com.baomidou.mybatisplus.generator.config.po.tableinfo;
import com.baomidou.mybatisplus.generator.config.rules.dbcolumntype;
import com.baomidou.mybatisplus.generator.config.rules.dbtype;
import com.baomidou.mybatisplus.generator.config.rules.namingstrategy;
import com.baomidou.mybatisplus.generator.engine.freemarkertemplateengine;

import java.io.file;
import java.util.*;

/**
 * created by wangqichang on 2018/6/1.
 */
public class mabatisplusgenerator {

  //生成文件所在项目路径
  private static string baseprojectpath = "d:\\git\\strandrd_official_website\\kichun\\kichun-ucenter\\kichun-ucenter-entity";

  //基本包名
  private static string basepackage="com.kichun.ucenter";
  //作者
  private static string authorname="wangqichang";
  //要生成的表名
  private static string[] tables= {"t_role","t_resource","t_role_resource","t_user_role"};
  //table前缀
  private static string prefix="t_";

  //数据库配置四要素
  private static string drivername = "net.sf.log4jdbc.driverspy";
  private static string url = "jdbc:log4jdbc:mysql://127.0.0.1:3306/kichun_dev?useunicode=true&characterencoding=utf8&zerodatetimebehavior=converttonull&allowmultiqueries=true";
  private static string username = "不告诉你";
  private static string password = "密码也不告诉你";


  public static void main(string[] args) {


    autogenerator gen = new autogenerator();

    /**
     * 数据库配置
     */
    gen.setdatasource(new datasourceconfig()
        .setdbtype(dbtype.mysql)
        .setdrivername(drivername)
        .seturl(url)
        .setusername(username)
        .setpassword(password)
        .settypeconvert(new mysqltypeconvert() {
          // 自定义数据库表字段类型转换【可选】
          @override
          public dbcolumntype processtypeconvert(string fieldtype) {
            system.out.println("转换类型:" + fieldtype);
            // if ( fieldtype.tolowercase().contains( "tinyint" ) ) {
            //  return dbcolumntype.boolean;
            // }
            return super.processtypeconvert(fieldtype);
          }
        }));

    /**
     * 全局配置
     */
    gen.setglobalconfig(new globalconfig()
        .setoutputdir( baseprojectpath + "/src/main/java")//输出目录
        .setfileoverride(true)// 是否覆盖文件
        .setactiverecord(true)// 开启 activerecord 模式
        .setenablecache(false)// xml 二级缓存
        .setbaseresultmap(true)// xml resultmap
        .setbasecolumnlist(true)// xml columlist
        .setopen(false)//生成后打开文件夹
        .setauthor(authorname)
        // 自定义文件命名,注意 %s 会自动填充表实体属性!
        .setmappername("%smapper")
        .setxmlname("%smapper")
        .setservicename("%sservice")
        .setserviceimplname("%sserviceimpl")
        .setcontrollername("%scontroller")
    );

    /**
     * 策略配置
     */
    gen.setstrategy(new strategyconfig()
        // .setcapitalmode(true)// 全局大写命名
        //.setdbcolumnunderline(true)//全局下划线命名
        .settableprefix(new string[]{prefix})// 此处可以修改为您的表前缀
        .setnaming(namingstrategy.underline_to_camel)// 表名生成策略
        .setinclude(tables) // 需要生成的表
        .setrestcontrollerstyle(true)
        //.setexclude(new string[]{"test"}) // 排除生成的表
        // 自定义实体父类
        // .setsuperentityclass("com.baomidou.demo.testentity")
        // 自定义实体,公共字段
        //.setsuperentitycolumns(new string[]{"test_id"})
        //.settablefilllist(tablefilllist)
        // 自定义 mapper 父类 默认basemapper
        //.setsupermapperclass("com.baomidou.mybatisplus.mapper.basemapper")
        // 自定义 service 父类 默认iservice
        // .setsuperserviceclass("com.baomidou.demo.testservice")
        // 自定义 service 实现类父类 默认serviceimpl
        // .setsuperserviceimplclass("com.baomidou.demo.testserviceimpl")
        // 自定义 controller 父类
        //.setsupercontrollerclass("com.kichun."+packagename+".controller.abstractcontroller")
        // 【实体】是否生成字段常量(默认 false)
        // public static final string id = "test_id";
        // .setentitycolumnconstant(true)
        // 【实体】是否为构建者模型(默认 false)
        // public user setname(string name) {this.name = name; return this;}
        // .setentitybuildermodel(true)
        // 【实体】是否为lombok模型(默认 false)<a href="https://projectlombok.org/" rel="external nofollow" >document</a>
        .setentitylombokmodel(true)
        // boolean类型字段是否移除is前缀处理
        // .setentitybooleancolumnremoveisprefix(true)
        // .setrestcontrollerstyle(true)
        // .setcontrollermappinghyphenstyle(true)
    );

    /**
     * 包配置
     */
    gen.setpackageinfo(new packageconfig()
            //.setmodulename("user")
            .setparent(basepackage)// 自定义包路径
            .setcontroller("controller")// 这里是控制器包名,默认 web
            .setentity("entity")
            .setmapper("dao")
            .setservice("service")
            .setserviceimpl("service.impl")
            .setxml("mapper")
            );

    /**
     * 注入自定义配置
     */
    // 注入自定义配置,可以在 vm 中使用 cfg.abc 设置的值
    injectionconfig abc = new injectionconfig() {
      @override
      public void initmap() {
        map<string, object> map = new hashmap<>();
        map.put("abc", this.getconfig().getglobalconfig().getauthor() + "-mp");
        this.setmap(map);
      }
    };
    //自定义文件输出位置(非必须)
    list<fileoutconfig> fileoutlist = new arraylist<>();
    fileoutlist.add(new fileoutconfig("/templates/mapper.xml.ftl") {
      @override
      public string outputfile(tableinfo tableinfo) {
        return baseprojectpath + "/src/main/resources/mappers/" + tableinfo.getentityname() + ".xml";
      }
    });
    abc.setfileoutconfiglist(fileoutlist);
    gen.setcfg(abc);

    /**
     * 指定模板引擎 默认是velocitytemplateengine ,需要引入相关引擎依赖
     */
    gen.settemplateengine(new freemarkertemplateengine());

    /**
     * 模板配置
     */
    gen.settemplate(
        // 关闭默认 xml 生成,调整生成 至 根目录
        new templateconfig().setxml(null)
        // 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy
        // 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置:
        // .setcontroller("...");
        // .setentity("...");
        // .setmapper("...");
        // .setxml("...");
        // .setservice("...");
        // .setserviceimpl("...");
    );

    // 执行生成
    gen.execute();
  }
}

到此这篇关于mybatis-plus generator配置详解的文章就介绍到这了,更多相关mybatis-plus generator配置内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网