项目层级:

引入依赖:
<dependencies>
<!-- spring-cloud -->
<!-- org.projectlombok-lombok -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>12.0</version>
</dependency>
<!-- org.projectlombok-lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.jeecgframework</groupId>
<artifactId>autopoi</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.jeecgframework</groupId>
<artifactId>autopoi-web</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
</dependencies>
生成器mian方法(如若无需自定义模板,红色移除即可,**内容自己替换):
public class AutoGenerater {
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();
final String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("**");
gc.setOpen(false);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:oracle:thin:@***.***.***.***:****/数据库用户名");
dsc.setDriverName("oracle.jdbc.driver.OracleDriver");
dsc.setUsername("***");
dsc.setPassword("***");
dsc.setTypeConvert(new MySqlTypeConvert() {
@Override
public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
//date转换成date
if ( fieldType.toLowerCase().contains( "date" ) ) {
return DbColumnType.DATE;
}
//将数据库中datetime转换成date
if ( fieldType.toLowerCase().contains( "datetime" ) ) {
return DbColumnType.DATE;
}
if ( fieldType.toLowerCase().contains( "timestamp" ) ) {
return DbColumnType.DATE;
}
return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
}
});
mpg.setDataSource(dsc);
// 包配置
final String packagePath = "com.jy.claim";
final String productName = scanner("请输入项目名称:");
final PackageConfig pc = new PackageConfig();
pc.setModuleName(productName);
pc.setParent(packagePath);
mpg.setPackageInfo(pc);
String [] strs = scanner("表名,多个英文逗号分割").split(",");
String [] tableName = new String[strs.length];
for(int j=0 ; j<4 ; j++){
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String mapperPath = "/template/mapper.xml.ftl";
String controllerPath = "/template/controller.java.ftl";
String servicePath = "/template/service.java.ftl";
String serviceImplpath = "/template/serviceImpl.java.ftl";
String daoPath = "/template/mapper.java.ftl";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
if(j == 0){
String entityPath = "/template/entity.java.ftl";
focList.add(new FileOutConfig(entityPath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/src/main/java/com/jy/claim/"+ productName +"/entity"
+ "/" + tableInfo.getEntityName() + StringPool.DOT_JAVA;
}
});
}else if(j == 1){
String entityPath = "/template/entityResult.java.ftl";
focList.add(new FileOutConfig(entityPath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/src/main/java/com/jy/claim/"+ productName +"/result"
+ "/" + tableInfo.getEntityName()+"Result" + StringPool.DOT_JAVA;
}
});
}else if(j == 2){
String entityPath = "/template/entityQuery.java.ftl";
focList.add(new FileOutConfig(entityPath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/src/main/java/com/jy/claim/"+ productName +"/query"
+ "/" + tableInfo.getEntityName()+ "Query" + StringPool.DOT_JAVA;
}
});
}else if(j == 3){
String entityPath = "/template/entityCommand.java.ftl";
focList.add(new FileOutConfig(entityPath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/src/main/java/com/jy/claim/"+ productName +"/command"
+ "/" + tableInfo.getEntityName() + "Command" + StringPool.DOT_JAVA;
}
});
}
// 自定义配置会被优先输出
focList.add(new FileOutConfig(mapperPath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/"
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
// 自定义配置会被优先输出
focList.add(new FileOutConfig(controllerPath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/java/com/jy/claim/"+ productName +"/controller"
+ "/" + tableInfo.getControllerName() + StringPool.DOT_JAVA;
}
});
// 自定义配置会被优先输出
focList.add(new FileOutConfig(servicePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/java/com/jy/claim/"+ productName +"/service"
+ "/" + tableInfo.getEntityName()+ "Service" + StringPool.DOT_JAVA;
}
});
// 自定义配置会被优先输出
focList.add(new FileOutConfig(serviceImplpath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/java/com/jy/claim/"+ productName +"/service/impl"
+ "/" + tableInfo.getServiceImplName() + StringPool.DOT_JAVA;
}
});
// 自定义配置会被优先输出
focList.add(new FileOutConfig(daoPath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/java/com/jy/claim/"+ productName +"/dao"
+ "/" + tableInfo.getEntityName() + "Dao" + StringPool.DOT_JAVA;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
//类名生成遵循驼峰命名
strategy.setNaming(NamingStrategy.underline_to_camel);
//字段生成遵循驼峰命
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setChainModel(true);
strategy.setEntitySerialVersionUID(false);
strategy.setEntityLombokModel(true);
//controller是否restful风格
strategy.setRestControllerStyle(true);
for(int i=0;i<strs.length;i++){
tableName[i] = strs[i].toUpperCase();
}
strategy.setInclude(tableName);
strategy.setControllerMappingHyphenStyle(true);
//过滤表前缀
strategy.setTablePrefix("C_LS_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
代码生成模板(需要自定义的模板举例):
引用路径:/template/entityResult.java.ftl
package ${package.Entity};
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
<#if entityLombokModel>
import lombok.Data;
import java.util.Date;
import lombok.EqualsAndHashCode;
<#if chainModel>
import lombok.experimental.Accessors;
</#if>
</#if>
/**
* @Description: ${table.comment!}
* @Author: ${author}
* @Date: ${date}
* @Version: V1.0
*/
<#if entityLombokModel>
@Data
<#if superEntityClass??>
@EqualsAndHashCode(callSuper = true)
<#else>
@EqualsAndHashCode(callSuper = false)
</#if>
<#if chainModel>
@Accessors(chain = true)
</#if>
</#if>
<#if swagger2>
@ApiModel(value="${entity}对象", description="${table.comment!}")
</#if>
<#if superEntityClass??>
public class ${entity} extends ${superEntityClass}<#if activeRecord><${entity}></#if> {
<#elseif activeRecord>
public class ${entity} extends Model<${entity}> {
<#else>
public class ${entity}Result {
</#if>
<#-- ---------- BEGIN 字段循环遍历 ---------->
<#list table.fields as field>
<#if field.keyFlag>
<#assign keyPropertyName="${field.propertyName}"/>
</#if>
<#if field.comment!?length gt 0>
<#if swagger2>
@ApiModelProperty(value = "${field.comment}")
<#else>
<#if field.keyFlag>
/**
* 主键id
*/
<#else>
/**
* ${field.comment}
*/
</#if>
</#if>
</#if>
<#-- 主键 -->
<#if field.propertyType == 'Date'>
@Excel(name = "${field.comment}", width = 15, format = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
<#else>
<#if field.keyFlag>
@Excel(name = "主键id", width = 15)
<#else>
@Excel(name = "${field.comment}", width = 15)
</#if>
</#if>
<#-- 普通字段 -->
<#-- 乐观锁注解 -->
<#if (versionFieldName!"") == field.name>
@Version
</#if>
<#-- 逻辑删除注解 -->
<#if (logicDeleteFieldName!"") == field.name>
@TableLogic
</#if>
private ${field.propertyType} ${field.propertyName};
</#list>
<#------------ END 字段循环遍历 ---------->
<#if !entityLombokModel>
<#list table.fields as field>
<#if field.propertyType == "boolean">
<#assign getprefix="is"/>
<#else>
<#assign getprefix="get"/>
</#if>
public ${field.propertyType} ${getprefix}${field.capitalName}() {
return ${field.propertyName};
}
<#if chainModel>
public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
<#else>
public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
</#if>
this.${field.propertyName} = ${field.propertyName};
<#if chainModel>
return this;
</#if>
}
</#list>
</#if>
<#if entityColumnConstant>
<#list table.fields as field>
public static final String ${field.name?upper_case} = "${field.name}";
</#list>
</#if>
<#if activeRecord>
@Override
protected Serializable pkVal() {
<#if keyPropertyName??>
return this.${keyPropertyName};
<#else>
return null;
</#if>
}
</#if>
<#if !entityLombokModel>
@Override
public String toString() {
return "${entity}{" +
<#list table.fields as field>
<#if field_index==0>
"${field.propertyName}=" + ${field.propertyName} +
<#else>
", ${field.propertyName}=" + ${field.propertyName} +
</#if>
</#list>
"}";
}
</#if>
}