Skip to content

Commit

Permalink
add > new feature for issue #32
Browse files Browse the repository at this point in the history
  • Loading branch information
lihengming committed Jul 24, 2017
1 parent 069c318 commit e800911
Showing 1 changed file with 39 additions and 34 deletions.
73 changes: 39 additions & 34 deletions src/test/java/CodeGenerator.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import com.google.common.base.CaseFormat;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.internal.DefaultShellCallback;
Expand Down Expand Up @@ -36,18 +38,34 @@ public class CodeGenerator {

public static void main(String[] args) {
genCode("输入表名");
//genCode("输入表名","输入自定义Model名称");
}

/**
* 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。
* 如输入表名称 "t_user_detail" 将生成 TUserDetail、TUserDetailMapper、TUserDetailService ...
* @param tableNames 数据表名称...
*/
public static void genCode(String... tableNames) {
for (String tableName : tableNames) {
//根据需求生成,不需要的注掉,模板有问题的话可以自己修改。
genModelAndMapper(tableName);
genService(tableName);
genController(tableName);
genCode(tableName, null);
}
}

public static void genModelAndMapper(String tableName) {
/**
* 通过数据表名称,和自定义的 Model 名称生成代码
* 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User" 将生成 User、UserMapper、UserService ...
* @param tableName 数据表名称
* @param modelName 自定义的 Model 名称
*/
public static void genCode(String tableName, String modelName) {
genModelAndMapper(tableName, modelName);
genService(tableName, modelName);
genController(tableName, modelName);
}


public static void genModelAndMapper(String tableName, String modelName) {
Context context = new Context(ModelType.FLAT);
context.setId("Potato");
context.setTargetRuntime("MyBatis3Simple");
Expand Down Expand Up @@ -84,6 +102,7 @@ public static void genModelAndMapper(String tableName) {

TableConfiguration tableConfiguration = new TableConfiguration(context);
tableConfiguration.setTableName(tableName);
tableConfiguration.setDomainObjectName(modelName);
tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
context.addTableConfiguration(tableConfiguration);

Expand All @@ -106,21 +125,20 @@ public static void genModelAndMapper(String tableName) {
if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) {
throw new RuntimeException("生成Model和Mapper失败:" + warnings);
}

String modelName = tableNameConvertUpperCamel(tableName);
if (StringUtils.isEmpty(modelName)) modelName = tableNameConvertUpperCamel(tableName);
System.out.println(modelName + ".java 生成成功");
System.out.println(modelName + "Mapper.java 生成成功");
System.out.println(modelName + "Mapper.xml 生成成功");
}

public static void genService(String tableName) {
public static void genService(String tableName, String modelName) {
try {
freemarker.template.Configuration cfg = getConfiguration();

Map<String, Object> data = new HashMap<>();
data.put("date", DATE);
data.put("author", AUTHOR);
String modelNameUpperCamel = tableNameConvertUpperCamel(tableName);
String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
data.put("modelNameUpperCamel", modelNameUpperCamel);
data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName));
data.put("basePackage", BASE_PACKAGE);
Expand All @@ -145,17 +163,17 @@ public static void genService(String tableName) {
}
}

public static void genController(String tableName) {
public static void genController(String tableName, String modelName) {
try {
freemarker.template.Configuration cfg = getConfiguration();

Map<String, Object> data = new HashMap<>();
data.put("date", DATE);
data.put("author", AUTHOR);
data.put("baseRequestMapping", tableNameConvertMappingPath(tableName));
String modelNameUpperCamel = tableNameConvertUpperCamel(tableName);
String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
data.put("baseRequestMapping", modelNameConvertMappingPath(modelNameUpperCamel));
data.put("modelNameUpperCamel", modelNameUpperCamel);
data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName));
data.put("modelNameLowerCamel", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel));
data.put("basePackage", BASE_PACKAGE);

File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_CONTROLLER + modelNameUpperCamel + "Controller.java");
Expand All @@ -181,30 +199,11 @@ private static freemarker.template.Configuration getConfiguration() throws IOExc
}

private static String tableNameConvertLowerCamel(String tableName) {
StringBuilder result = new StringBuilder();
if (tableName != null && tableName.length() > 0) {
tableName = tableName.toLowerCase();//兼容使用大写的表名
boolean flag = false;
for (int i = 0; i < tableName.length(); i++) {
char ch = tableName.charAt(i);
if ("_".charAt(0) == ch) {
flag = true;
} else {
if (flag) {
result.append(Character.toUpperCase(ch));
flag = false;
} else {
result.append(ch);
}
}
}
}
return result.toString();
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableName.toLowerCase());
}

private static String tableNameConvertUpperCamel(String tableName) {
String camel = tableNameConvertLowerCamel(tableName);
return camel.substring(0, 1).toUpperCase() + camel.substring(1);
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName.toLowerCase());

}

Expand All @@ -213,7 +212,13 @@ private static String tableNameConvertMappingPath(String tableName) {
return "/" + (tableName.contains("_") ? tableName.replaceAll("_", "/") : tableName);
}

private static String modelNameConvertMappingPath(String modelName) {
String tableName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, modelName);
return tableNameConvertMappingPath(tableName);
}

private static String packageConvertPath(String packageName) {
return String.format("/%s/", packageName.contains(".") ? packageName.replaceAll("\\.", "/") : packageName);
}

}

0 comments on commit e800911

Please sign in to comment.