没有理由不根据自己的项目编写一套适合自己项目的代码生成工具,不是吗?
思路: 首先根据自己的项目情况,定义好代码模板与占位参数,生成的时候只需要将参数填进去,再在指定的位置生成相应的.java/.xml等文件即可
使用freemarker模板引擎 compile 'org.freemarker:freemarker:2.3.23'
# 核心源源码
package com.futao.springbootdemo.utils.generator;
import com.futao.springbootdemo.foundation.ApplicationException;
import com.futao.springbootdemo.utils.DateTools;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 代码生成器
*
* @author futao
* Created on 2019-03-08.
*/
public class CodeGeneratorManager {
private static final Logger LOGGER = LoggerFactory.getLogger(CodeGeneratorManager.class);
/**
* 项目基础路径
*/
private static final String PROJECT_BASE_PATH = System.getProperty("user.dir") + "/src/main/java/com/futao/springbootdemo";
/**
* 模板文件路径
*/
private static final String TEMPLATE_FILE_PATH = PROJECT_BASE_PATH + "/utils/generator/template";
/**
* Entity生成路径
*/
private static final String ENTITY_PATH = PROJECT_BASE_PATH + "/model/entity";
/**
* Controller路径
*/
private static final String CONTROLLER_PATH = PROJECT_BASE_PATH + "/controller/business";
/**
* Service实现类生成路径
*/
private static final String SERVICE_IMPL_PATH = PROJECT_BASE_PATH + "/service/impl";
/**
* Service接口生成路径
*/
private static final String SERVICE_FACE_PATH = PROJECT_BASE_PATH + "/service";
/**
* Dao Mapper路径
*/
private static final String DAO_PATH = PROJECT_BASE_PATH + "/dao";
/**
* Mybatis mapper xml文件路径
*/
private static final String MYBATIS_MAPPER_XML_PATH = System.getProperty("user.dir") + "/src/main/resources/mybatis/mapper";
private static Configuration configuration = null;
private CodeGeneratorManager() {
}
/**
* Freemarker 模板环境配置
*
* @return
* @throws IOException
*/
private Configuration initFreemarkerConfiguration() {
Configuration cfg;
try {
cfg = new Configuration(Configuration.VERSION_2_3_23);
cfg.setDirectoryForTemplateLoading(new File(TEMPLATE_FILE_PATH));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
} catch (IOException e) {
throw new RuntimeException("Freemarker 模板环境初始化异常!", e);
}
return cfg;
}
/**
* 获取模板文件
*
* @return
*/
private Template getTemplate(String templateName) {
if (configuration == null) {
configuration = initFreemarkerConfiguration();
}
try {
return configuration.getTemplate(templateName);
} catch (IOException e) {
e.printStackTrace();
throw ApplicationException.ae(e.getMessage());
}
}
/**
* 生成代码
*
* @param args
*/
public static void main(String[] args) {
CodeGeneratorManager codeGeneratorManager = new CodeGeneratorManager();
Map<String, Object> dataMap = new HashMap<>(6);
dataMap.put("className", "Address");
dataMap.put("classDesc", "地址");
dataMap.put("createDate", DateTools.dateToString(new Date(), DateTools.yyyyMMdd));
dataMap.put("controllerPackagePath", getPackagePath(CONTROLLER_PATH));
dataMap.put("entityPackagePath", getPackagePath(ENTITY_PATH));
dataMap.put("serviceFacePackagePath", getPackagePath(SERVICE_FACE_PATH));
dataMap.put("serviceImplPackagePath", getPackagePath(SERVICE_IMPL_PATH));
dataMap.put("daoPackagePath", getPackagePath(DAO_PATH));
Template entityTemplate = codeGeneratorManager.getTemplate("EntityTemplate.ftl");
Template controllerTemplate = codeGeneratorManager.getTemplate("ControllerTemplate.ftl");
Template serviceFaceTemplate = codeGeneratorManager.getTemplate("ServiceFaceTemplate.ftl");
Template serviceImplTemplate = codeGeneratorManager.getTemplate("ServiceImplTemplate.ftl");
Template daoTemplate = codeGeneratorManager.getTemplate("DaoTemplate.ftl");
Template mapperTemplate = codeGeneratorManager.getTemplate("MapperTemplate.ftl");
File entityFile = new File(ENTITY_PATH + "/" + dataMap.get("className") + ".java");
File controllerFile = new File(CONTROLLER_PATH + "/" + dataMap.get("className") + "Controller.java");
File serviceFaceFile = new File(SERVICE_FACE_PATH + "/" + dataMap.get("className") + "Service.java");
File serviceImplFile = new File(SERVICE_IMPL_PATH + "/" + dataMap.get("className") + "ServiceImpl.java");
File daoFile = new File(DAO_PATH + "/" + dataMap.get("className") + "Dao.java");
File mapperFile = new File(MYBATIS_MAPPER_XML_PATH + "/" + dataMap.get("className") + "Mapper.xml");
try {
entityTemplate.process(dataMap, new FileWriter(entityFile));
System.out.println(">>>> 实体类【" + dataMap.get("className") + "】生成成功");
controllerTemplate.process(dataMap, new FileWriter(controllerFile));
System.out.println(">>>> 控制器类【" + dataMap.get("className") + "Controller】生成成功");
serviceFaceTemplate.process(dataMap, new FileWriter(serviceFaceFile));
System.out.println(">>>> Service接口【" + dataMap.get("className") + "Service】生成成功");
serviceImplTemplate.process(dataMap, new FileWriter(serviceImplFile));
System.out.println(">>>> Service实现类【" + dataMap.get("className") + "ServiceImpl】生成成功");
daoTemplate.process(dataMap, new FileWriter(daoFile));
System.out.println(">>>> Dao接口【" + dataMap.get("className") + "Dao】生成成功");
mapperTemplate.process(dataMap, new FileWriter(mapperFile));
System.out.println(">>>> Mapper文件【" + dataMap.get("className") + "Mapper】生成成功");
System.out.println(StringUtils.repeat("=", 20) + "执行完毕" + StringUtils.repeat("=", 20));
} catch (TemplateException | IOException e) {
e.printStackTrace();
}
}
/**
* 根据文件系统路径获取包路径
*
* @param fileSysPath 文件系统路径
* @return
*/
private static String getPackagePath(String fileSysPath) {
return fileSysPath.substring(fileSysPath.indexOf("com")).replace("/", ".");
}
}
# 代码模板(只贴出两个示例)
- Controller.java
package ${controllerPackagePath};
import ${entityPackagePath}.${className};
import ${serviceFacePackagePath}.${className}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* ${classDesc}操作接口
*
* @author futao
* Created on ${createDate}.
*/
@Api("${classDesc}")
@RestController
@RequestMapping(path = "${className?lower_case}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ${className}Controller {
@Resource
private ${className}Service ${className?lower_case}Service;
/**
* 新增${classDesc}
*
* @return
*/
@ApiOperation("新增${classDesc}")
@PostMapping(path = "add")
public ${className} add(
) {
return ${className?lower_case}Service.add();
}
/**
* 删除${classDesc}
*
* @param id 要删除的${classDesc}的id
* @return
*/
@DeleteMapping("{id}")
public ${className} delete(
@PathVariable("id") String id
) {
return ${className?lower_case}Service.delete(id);
}
/**
* 更新${classDesc}
*
* @param id 要更新的${classDesc}的id
* @return
*/
@PutMapping("{id}")
public ${className} update(
@PathVariable("id") String id
) {
return ${className?lower_case}Service.update(id);
}
/**
* 查询${classDesc}列表
*
* @return
*/
@GetMapping("list")
public List<${className}> list() {
return ${className?lower_case}Service.list();
}
/**
* 获取${classDesc}详情
*
* @param id ${classDesc}id
* @return
*/
@GetMapping("{id}")
public ${className} byId(@PathVariable("id") String id) {
return ${className?lower_case}Service.byId(id);
}
}
- Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${daoPackagePath}.${className}Dao">
<!--配置一个LRU缓存(二级缓存),每隔60秒进行刷新,存储512个对象-->
<!--
LRU 最近最少使用策略,移除最长时间不被使用的对象
FIFO 先进先出策略
SOFT 软引用策略,移除基于垃圾回收器状态和软引用规则的对象
WEAK 弱引用策略,更积极地移除基于垃圾回收器状态和软引用规则的对象
-->
<cache eviction="LRU" flushInterval="60000" size="512" readOnly="true"/>
<insert id="add" parameterType="${className?lower_case}">
insert
into springmvcdemo.futao_${className?lower_case} ()
values ();
</insert>
<delete id="delete" parameterType="string">
delete
from springmvcdemo.futao_${className?lower_case}
where id = ${r"#{id}"}
</delete>
<update id="update" parameterType="${className?lower_case}">
update springmvcdemo.futao_${className?lower_case}
set
where id = ${r"#{id}"}
</update>
<select id="list">
select *
from springmvcdemo.futao_${className?lower_case}
</select>
<select id="byId" parameterType="string">
select *
from springmvcdemo.futao_${className?lower_case}
where id = ${r"#{id}"}
</select>
</mapper>
根据自己的项目情况创建模板即可,比如我就创建了6个
# 使用
直接执行 CodeGeneratorManager#main 方法即可
-
效果
无解~
快乐~
源代码: https://github.com/FutaoSmile/springbootFramework
我在这里等你: