
- 【Spring Cloud Alibaba】Mybatis Plus 代码生成器
- 1、Mybatis Plus Generator
- 2、搭建微服务代码生成器
- 3、项目代码格式化
- 微信公众号
在编写微服务的时候,突然发现每个模块都需要使用到 Mybatis Plus,我又在每个服务中使用了代码生成器,复制粘贴太麻烦了,所以我就直接设计了一个独立的模块,用于专门处理 Mybatis Plus 的代码生成,这个模块没有使用 Spring Boot 的形式,直接使用的是 Main 方法运行,即可生成配置
Mybatis Plus 官网:https://baomidou.com/
由于我是用了 Swagger 生成接口,所以子模块需要增加相关的依赖
${swagger-spring-boot-starter.version}
的版本为
1.9.0.RELEASE
2、搭建微服务代码生成器com.spring4all swagger-spring-boot-starter${swagger-spring-boot-starter.version}
创建 spring-cloud-alibaba-mybatis-plus-generator 模块,主要增加生成器模块
com.baomidou mybatis-plus-boot-starter${mybatis-plus-boot-starter.version} mysql mysql-connector-javacom.baomidou mybatis-plus-generator${mybatis-plus-generator.version} org.freemarker freemarker${freemarker.version} org.projectlombok lombok${lombok.version} org.apache.commons commons-lang3org.springframework.boot spring-boot-starter-testtest org.junit.vintage junit-vintage-engine
接下来就直接是代码生成器的 Main 方法了,代码生成器是可以随便定制模板的,因为我写的微服务是最基本的方法就够了,所以这里使用默认的形式就可以了
使用方法,修改以下两点就可以了
- 修改数据库名称、账号、密码
- 修改模块根路径
package cn.tellsea.config;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CodeGenerator {
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.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir") + "/spring-cloud-alibaba-boot-seata-account";
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("Tellsea");
gc.setOpen(false);
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/alibaba-seata-account?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// pc.setModuleName(scanner("模块名"));
pc.setModuleName("");
pc.setParent("cn.tellsea");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(false);
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
执行 Main 方法
生成的效果
如果需要定制方法,直接修改他的定制模块即可
代码生成之后,由于个人有强迫症,所有代码必须格式化,按照标准格式书写空格等,所以这里使用 IDEA 的代码批量格式化功能,我的 IDEA 使用的是 Eclipse 的代码快捷键
Ctrl + Alt + L
一键格式化
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)