public class CodeGenerator {
private static final String TEMPLATE_PATH = "src/main/resources/templates";
private static final String JAVA_PATH = "src/main/java";
private static final String PACKAGE_PATH = "/com/example/demo/";
private static final String ENTITY_TEMPLATE = "entity.java.vm";
private static final String MAPPER_TEMPLATE = "mapper.java.vm";
private static final String SERVICE_TEMPLATE = "service.java.vm";
private static final String CONTROLLER_TEMPLATE = "controller.java.vm";
// 是否覆盖已有文件
private static final boolean FILE_OVERRIDE = true;
// 数据库配置
private static final String DB_URL = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&useSSL=false";
private static final String DB_USERNAME = "root";
private static final String DB_PASSWORD = "root";
// 包名
private static final String PACKAGE_NAME = "com.example.demo";
public static void main(String[] args) {
generate("my_table", "MyTable");
}
public static void generate(String tableName, String className) {
VelocityEngine velocityEngine = new VelocityEngine();
Properties properties = new Properties();
properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, TEMPLATE_PATH);
velocityEngine.init(properties);
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource());
// 获取表元数据
TableMetaData tableMetaData = getTableMetaData(jdbcTemplate, tableName);
// 生成Entity类
generateEntity(velocityEngine, tableMetaData, className);
// 生成Mapper接口
generateMapper(velocityEngine, tableMetaData, className);
// 生成Service接口
generateService(velocityEngine, tableMetaData, className);
// 生成Controller类
generateController(velocityEngine, tableMetaData, className);
}
private static void generateEntity(VelocityEngine velocityEngine, TableMetaData tableMetaData, String className) {
String targetPath = JAVA_PATH + PACKAGE_PATH + "entity/" + className + ".java";
if (!FILE_OVERRIDE && new File(targetPath).exists()) {
System.out.println(targetPath + " already exists! Skip generating entity.");
return;
}
Map model = new HashMap<>();
model.put("package", PACKAGE_NAME);
model.put("className", className);
model.put("tableMetaData", tableMetaData);
Template template = velocityEngine.getTemplate(ENTITY_TEMPLATE, "UTF-8");
String content = mergeTemplate(model, template);
writeToFile(targetPath, content);
}
private static void generateMapper(VelocityEngine velocityEngine, TableMetaData tableMetaData, String className) {
String targetPath = JAVA_PATH + PACKAGE_PATH + "mapper/" + className + "Mapper.java";
if (!FILE_OVERRIDE && new File(targetPath).exists()) {
System.out.println(targetPath + " already exists! Skip generating mapper.");
return;
}
Map model = new HashMap<>();
model.put("package", PACKAGE_NAME);
model.put("className", className);
model.put("tableMetaData", tableMetaData);
Template template = velocityEngine.getTemplate(MAPPER_TEMPLATE, "UTF-8");
String content = mergeTemplate(model, template);
writeToFile(targetPath, content);
}
private static void generateService(VelocityEngine velocityEngine, TableMetaData tableMetaData, String className) {
String targetPath = JAVA_PATH + PACKAGE_PATH + "service/" + className + "Service.java";
if (!FILE_OVERRIDE && new File(targetPath).exists()) {
System.out.println(targetPath + " already exists! Skip generating service.");
return;
}
Map model = new HashMap<>();
model.put("package", PACKAGE_NAME);
model.put("className", className);
model.put("tableMetaData", tableMetaData);
Template template = velocityEngine.getTemplate(SERVICE_TEMPLATE, "UTF-8");
String content = mergeTemplate(model, template);
writeToFile(targetPath, content);
}
private static void generateController(VelocityEngine velocityEngine, TableMetaData tableMetaData, String className) {
String targetPath = JAVA_PATH + PACKAGE_PATH + "controller/" + className + "Controller.java";
if (!FILE_OVERRIDE && new File(targetPath).exists()) {
System.out.println(targetPath + " already exists! Skip generating controller.");
return;
}
Map model = new HashMap<>();
model.put("package", PACKAGE_NAME);
model.put("className", className);
model.put("tableMetaData", tableMetaData);
Template template = velocityEngine.getTemplate(CONTROLLER_TEMPLATE, "UTF-8");
String content = mergeTemplate(model, template);
writeToFile(targetPath, content);
}
private static TableMetaData getTableMetaData(JdbcTemplate jdbcTemplate, String tableName) {
String sql = "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?";
List