代码生成器

参考:

https://blog.csdn.net/u014745069/article/details/80720980

https://mp.baomidou.com/config/generator-config.html#%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD%AE

需要考虑的

问题:生成的dao层文件需要自己添加@Mapper注解 @Service,@Dao 需要自己添加(不添加也没事)
生成的代码都是在一个模块下,而我们的实体类其实是放在另外一个工程中(没有解决,生成后需要自己手动去拷贝过去)
需要引用到其他的工程里的基类(可以实现)

读取控制台的内容

/**
 * <p>
 * 读取控制台内容
 * </p>
 */
public static String scanner(String tip) {
    Scanner scanner = new Scanner(System.in);
    StringBuilder help = new StringBuilder();
    help.append("请输入" + tip + ":");
    System.out.println(help.toString());
    if (scanner.hasNext()) {
        String ipt = scanner.next();
        if (StringUtils.isNotEmpty(ipt)) {
            return ipt;
        }
    }
    throw new MybatisPlusException("请输入正确的" + tip + "!");
}
View Code

全局策略globalConfig 配置

outputDir 生成文件的输出目录 默认值:D 盘根目录
#fileOverride 是否覆盖已有文件 默认值:false
#open 是否打开输出目录 默认值:true
#enableCache 是否在xml中添加二级缓存配置 默认值:`false
#author 开发人员 默认值:null
#kotlin 开启 Kotlin 模式 默认值:false
#swagger2 开启 swagger2 模式 默认值:false (这里swagger只能在Entity的实体类上加一个注解)
#activeRecord 开启 ActiveRecord 模式 默认值:false
#baseResultMap 开启 BaseResultMap 默认值:false
#baseColumnList 开启 baseColumnList 默认值:false
#dateType 时间类型对应策略 默认值:TIME_PACK 注意事项: 如下配置 %s 为占位符
#entityName 实体命名方式 默认值:null 例如:%sEntity 生成 UserEntity
#mapperName mapper 命名方式 默认值:null 例如:%sDao 生成 UserDao
#xmlName Mapper xml 命名方式 默认值:null 例如:%sDao 生成 UserDao.xml 
#serviceName service 命名方式 默认值:null 例如:%sBusiness 生成 UserBusiness
#serviceImplName service impl 命名方式 默认值:null 例如:%sBusinessImpl 生成 UserBusinessImpl
#controllerName controller 命名方式 默认值:null 例如:%sAction 生成 UserAction
#idType 指定生成的主键的ID类型 默认值:null
View Code
// 代码生成器
AutoGenerator mpg = new AutoGenerator();

// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/mybatis-plus-sample-generator/src/main/java");
gc.setAuthor("uih");
gc.setOpen(false);
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);

// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://10.3.13.250:3306/service_center?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC&allowMultiQueries=true");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("111111");
mpg.setDataSource(dsc);
View Code

包配置

parent 父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
#moduleName 父包模块名
#entity Entity包名
#service Service包名
#serviceImpl Service Impl包名
#mapper Mapper包名
#xml Mapper XML包名
#controller Controller包名
#pathInfo 路径配置信息

// 包配置
PackageConfig pc = new PackageConfig();
//pc.setModuleName(scanner("模块名"));//输入模块名
pc.setParent("com.baomidou.mybatisplus.samples.generator");
mpg.setPackageInfo(pc);
View Code

注入 injectionConfig 配置

这里配置自动生成****Mapper.xml文件
fileOutConfigList
自定义输出文件
配置 FileOutConfig 指定模板文件、输出文件达到自定义文件生成目的

// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
    @Override
    public void initMap() {
        // to do nothing
    }
};
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
    @Override
    public String outputFile(TableInfo tableInfo) {
        // 自定义输入文件名称
        return projectPath + "/mybatis-plus-sample-generator/src/main/resources/mapper/"
                + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
    }
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
mpg.setTemplate(new TemplateConfig().setXml(null));
View Code

数据库表配置

通过该配置,可指定需要生成哪些表或者排除哪些表
isCapitalMode 是否大写命名
#skipView 是否跳过视图
naming 数据库表映射到实体的命名策略
columnNaming 数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
#tablePrefix 表前缀
#fieldPrefix 字段前缀
superEntityClass 自定义继承的Entity类全称,带包名
superEntityColumns 自定义基础的Entity类,公共字段
#superMapperClass 自定义继承的Mapper类全称,带包名
#superServiceClass 自定义继承的Service类全称,带包名
#superServiceImplClass 自定义继承的ServiceImpl类全称,带包名
#superControllerClass 自定义继承的Controller类全称,带包名
include 需要包含的表名,允许正则表达式(与exclude二选一配置)
exclude 需要排除的表名,允许正则表达式
#entityColumnConstant 【实体】是否生成字段常量(默认 false)
#entityBuilderModel【实体】是否为构建者模型(默认 false)
entityLombokModel【实体】是否为lombok模型(默认 false)
#entityBooleanColumnRemoveIsPrefix Boolean类型字段是否移除is前缀(默认 false)
#restControllerStyle 生成 @RestController 控制器
#controllerMappingHyphenStyle 驼峰转连字符
#entityTableFieldAnnotationEnable 是否生成实体时,生成字段注解
#versionFieldName 乐观锁属性名称
#logicDeleteFieldName 逻辑删除属性名称
#tableFillList 表填充字段

 1 // 策略配置
 2 StrategyConfig strategy = new StrategyConfig();
 3 strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
 4 strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
 5 //strategy.setSuperEntityClass("com.baomidou.mybatisplus.samples.generator.common.BaseEntity");//自定义继承的Entity类全称,带包名
 6 strategy.setEntityLombokModel(true);
 7 //strategy.setSuperControllerClass("com.baomidou.mybatisplus.samples.generator.common.BaseController");//自定义继承的Controller类全称,带包名
 8 strategy.setInclude(scanner("表名"));//需要包含的表名,允许正则表达式(与exclude二选一配置)
 9 //strategy.setSuperEntityColumns("id");//自定义基础的Entity类,公共字段
10 strategy.setControllerMappingHyphenStyle(true);
11 //strategy.setTablePrefix( "GGG_"); //这个应该意思是比如GGG_Employ表对应的实体类为Employ
12 mpg.setStrategy(strategy);
13 // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
14 mpg.setTemplateEngine(new FreemarkerTemplateEngine());
15 mpg.execute();
View Code

 

posted @ 2019-11-28 21:39  弱水三千12138  阅读(332)  评论(0编辑  收藏  举报