前言: 今天搭建项目的使用了Mybatis-plus的代码生成,所以进行一个总结,以便下次使用。官网也有相应教程!
Mybatis-plus代码生成器的步骤
这里为了以后能够方便的复用,我把单独创建了一个项目用来生成代码,所以如果想要只在本项目中使用可以忽略创建项目的步骤。
创建项目
导入依赖
<!--mybatis-plus生成代码的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!--mybatis-plus生成代码的模板引擎-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<!--Mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
创建代码生成类
public class CodeGenerator {
/**
* <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.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator generator = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
// 设置作者
gc.setAuthor("Owen");
// 打开输出目录
gc.setOpen(false);
// xml开启 BaseResultMap
gc.setBaseResultMap(true);
// xml开启 BaseColumnList
gc.setBaseColumnList(true);
// 实体属性 Swagger2 注解
// gc.setSwagger2(true);
// 设置日期格式,采用Date
gc.setDateType(DateType.ONLY_DATE);
generator.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://192.168.62.19:3306/seckill?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=Asia/Shanghai");
// dsc.setSchemaName("public");
// 此处是mysql8.0版本
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
generator.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// 设置生成的代码对应的报名
pc.setParent("com.seckill")
.setEntity("pojo")
.setMapper("mapper")
.setService("service")
.setServiceImpl("service.impl")
.setController("controller");
generator.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<FileOutConfig> 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);
generator.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
generator.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// 表名下划线转驼峰
strategy.setNaming(NamingStrategy.underline_to_camel);
// 设置忽略表名的前缀
strategy.setTablePrefix("t_");
// 列名下划线转驼峰
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
// 使用LomBok
strategy.setEntityLombokModel(true);
// 生成的Controller层使用Rest风格 默认不使用
//strategy.setRestControllerStyle(true);
// 公共父类
//strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
// 写于父类中的公共字段
//strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
generator.setStrategy(strategy);
// 设置模板引擎使用FreemarkerTemplateEngine
generator.setTemplateEngine(new FreemarkerTemplateEngine());
generator.execute();
}
代码生成类中必须修改的点
这里讲一下必须要修改的点:
- 此处设置了日期转换,如果不需要记得修改
gc.setDateType(DateType.ONLY_DATE);
- 修改数据库连接部分(这里我使用的是Mysql 8.0版本)
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://192.168.62.19:3306/seckill?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=Asia/Shanghai");
// dsc.setSchemaName("public");
// 此处是mysql8.0版本
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
generator.setDataSource(dsc);
- 设置生成代码所在的位置及包名
PackageConfig pc = new PackageConfig();
// 设置生成的代码对应的包名
pc.setParent("com.seckill")
.setEntity("pojo")
.setMapper("mapper")
.setService("service")
.setServiceImpl("service.impl")
.setController("controller");
generator.setPackageInfo(pc);
- 设置忽略表前缀
strategy.setTablePrefix("t_");
- 使用LomBok
strategy.setEntityLombokModel(true);
上述内容可能会造成生成的代码与你期望的不一致,可根据个人需求修改。
生成代码
- 运行该代码,并输入表名即可生成相应代码