一、依赖
代码生成器需要添加一下依赖
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.0.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity-engine-core -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.31</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
</dependencies>
二、代码实现
定义参数体:
@Data
@Accessors(chain = true)
public class XxxParam {
private String userName;
private String password;
private String auth;
private String host;
private String port;
private String dbName;
/**
* 多个表用逗号分割
*/
private String tableNames;
private String outPutDir = UserPortalConstant.OUT_PUT_DIR;
private String packagePath = UserPortalConstant.PACKAGE_PATH;
}
代码生成类:
public class BaseGenerator {
private static final String SERVICE_IMPL_NAME = "ServiceImpl";
private static final String SERVICE_NAME = "Service";
private static final String ENTITY_NAME = "Po";
private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
private static final String URL_FORMAT = "jdbc:mysql://%s:%s/%s";
public void execute(XxxParam generatorParam) {
// 1、创建代码生成器
AutoGenerator autoGenerator = new AutoGenerator();
// 2、全局配置
GlobalConfig globalConfig = new GlobalConfig();
autoGenerator.setGlobalConfig(globalConfig);
globalConfig.setOutputDir(generatorParam.getOutPutDir())
.setAuthor(generatorParam.getAuth())
.setOpen(Boolean.FALSE) //生成后是否打开资源管理器
.setFileOverride(Boolean.TRUE) //重新生成时文件是否覆盖
.setServiceImplName("%s" + SERVICE_IMPL_NAME)
.setServiceName("%s" + SERVICE_NAME) //去掉Service接口的首字母I
.setEntityName("%s" + ENTITY_NAME)
.setIdType(IdType.ID_WORKER) //主键策略
.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
//.setSwagger2(true);//开启Swagger2模式
// 3、数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
autoGenerator.setDataSource(dataSourceConfig);
dataSourceConfig.setUrl(String.format(URL_FORMAT, generatorParam.getHost(), generatorParam.getPort(), generatorParam.getDbName()))
.setDriverName(DRIVER_NAME)
.setUsername(generatorParam.getUserName())
.setPassword(generatorParam.getPassword())
.setDbType(DbType.MYSQL);
// 4、包配置
PackageConfig packageConfig = new PackageConfig();
//packageConfig.setModuleName("table"); //模块名
autoGenerator.setPackageInfo(packageConfig);
packageConfig.setParent(generatorParam.getPackagePath())
.setEntity("entity")
.setService("service")
.setServiceImpl("service.impl")
.setMapper("dao")
.setXml(null);
// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
autoGenerator.setStrategy(strategy);
strategy.setInclude(generatorParam.getTableNames()) // 设置需要生成的表名
.setNaming(NamingStrategy.underline_to_camel) //数据库表映射到实体的命名策略
//.setTablePrefix(packageConfig.getModuleName() + "_"); //生成实体时去掉表前缀
.setColumnNaming(NamingStrategy.underline_to_camel) //数据库表字段映射到实体的命名策略
.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
strategy.setRestControllerStyle(true); //restful api风格控制器
strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
autoGenerator.execute();
}
}