前言
在我们开发项目的时候,controller、service、serviceImpl、mapper、entity每次都要手动敲一遍,太不方便了,所以本次我写了一个mybatis-plus 自动生成代码器,提供开发效率。
依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
代码
package com.ymz.t31.ticket.config;
import com.baomidou.mybatisplus.annotation.IdType;
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.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
import java.util.List;
/**
* 自动生成框架结构
*
* @author yumingzhi
* @date 2021/11/7 0007 下午 1:46
*/
public class GeneratorCodeConfig {
/**
* 表名,需要填写
*/
private static final String TABLE_NAME = "train_passenger";
/**
* 模块名,需要填写
*/
private static final String MODULE_NAME = "ticket";
/**
* 存放文件目录,需要填写
*/
private static final String PROJECT_PATH = "E:\\Java\\project\\t31\\t31-parent\\t31-ticket\\t31-ticket-service";
/**
* 数据库,需要填写
*/
private static final String URL = "jdbc:mysql://localhost:3306/t31?useUnicode=true&useSSL=false&characterEncoding=utf8";
private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
private static final String USER_NAME = "root";
private static final String PASSWORD = "root";
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 1、全局配置
GlobalConfig gc = initGlobalConfig();
mpg.setGlobalConfig(gc);
// 2、数据源配置
DataSourceConfig dsc = initDataSourceConfig();
mpg.setDataSource(dsc);
// 3、生成文件包配置
PackageConfig pc = initPackageConfig();
mpg.setPackageInfo(pc);
// 4、自定义配置
InjectionConfig cfg = initInjectionConfig();
mpg.setCfg(cfg);
// 5、配置模板
TemplateConfig templateConfig = initTemplateConfig();
mpg.setTemplate(templateConfig);
// 6、策略配置
StrategyConfig strategy = initStrategyConfig();
mpg.setStrategy(strategy);
//执行
mpg.execute();
}
/**
* 全局配置
*
* @return
*/
private static GlobalConfig initGlobalConfig() {
GlobalConfig gc = new GlobalConfig();
// 当前项目路径
gc.setOutputDir(PROJECT_PATH + "/src/main/java");
gc.setDateType(DateType.ONLY_DATE);
gc.setAuthor("yumingzhi");
gc.setOpen(false);
//生成resultMap
gc.setBaseResultMap(true);
// 自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setControllerName("%sController");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
return gc;
}
/**
* 数据源配置
*
* @return
*/
private static DataSourceConfig initDataSourceConfig() {
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(URL);
dsc.setDriverName(DRIVER_NAME);
dsc.setUsername(USER_NAME);
dsc.setPassword(PASSWORD);
return dsc;
}
/**
* 生成文件包配置
*
* @return
*/
private static PackageConfig initPackageConfig() {
PackageConfig pc = new PackageConfig();
pc.setModuleName(MODULE_NAME);
pc.setParent("com.ymz.t31");
pc.setEntity("po");
pc.setMapper("dao");
pc.setService("service");
pc.setServiceImpl("service.impl");
return pc;
}
/**
* 自定义配置
*
* @return
*/
private static InjectionConfig initInjectionConfig() {
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
String templatePathXml = "/templates/mapper.xml.vm";
focList.add(new FileOutConfig(templatePathXml) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return PROJECT_PATH + "/src/main/resources/mapper/"
+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
return cfg;
}
/**
* 自定义模板配置
* 注意,名称不能和默认的一致
*
* @return
*/
private static TemplateConfig initTemplateConfig() {
TemplateConfig templateConfig = new TemplateConfig();
// 使用自定义模板,不想要生成就设置为null,如果不设置null会使用默认模板
templateConfig.setEntity("templates/entity1.java");
templateConfig.setController("templates/controller1.java");
templateConfig.setMapper("templates/mapper1.java");
templateConfig.setService("templates/service1.java");
templateConfig.setServiceImpl("templates/serviceImpl1.java");
templateConfig.setXml(null);
return templateConfig;
}
/**
* 策略配置
*
* @return
*/
private static StrategyConfig initStrategyConfig() {
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setSuperEntityClass("com.ymz.t31.core.po.BaseEntity");
// 自定义 mapper 父类
strategy.setSuperMapperClass("com.ymz.t31.core.dao.ICrudDao");
// 自定义 service 父类
strategy.setSuperServiceClass("com.ymz.t31.core.service.ICrudService");
// 自定义 service 实现类父类
strategy.setSuperServiceImplClass("com.ymz.t31.core.service.impl.CrudServiceImpl");
// 自定义 controller 父类
strategy.setSuperControllerClass("com.ymz.t31.core.controller.BaseController");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 使用lombok
strategy.setEntityLombokModel(true);
strategy.setInclude(TABLE_NAME);
strategy.setControllerMappingHyphenStyle(true);
return strategy;
}
}
总结
1、代码里都有注释,可以直接读代码
2、模板不会写,可以抄默认的模板,再改
3、注意:自定义模板不能和默认模板命名的一致,不然会被默认的覆盖!!!
天道酬勤,地道酬善,人道酬诚,商道酬信,业道酬精! --周易