|
|
@ -13,7 +13,7 @@ import java.util.*; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。 |
|
|
|
* 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public abstract class CodeGenerator { |
|
|
|
public class CodeGenerator { |
|
|
|
//JDBC配置,请修改为你项目的实际配置
|
|
|
|
//JDBC配置,请修改为你项目的实际配置
|
|
|
|
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/test"; |
|
|
|
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/test"; |
|
|
|
private static final String JDBC_USERNAME = "root"; |
|
|
|
private static final String JDBC_USERNAME = "root"; |
|
|
@ -50,9 +50,6 @@ public abstract class CodeGenerator { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void genModelAndMapper(String tableName) { |
|
|
|
public static void genModelAndMapper(String tableName) { |
|
|
|
try { |
|
|
|
|
|
|
|
List<String> warnings = new ArrayList<String>(); |
|
|
|
|
|
|
|
boolean overwrite = true; |
|
|
|
|
|
|
|
Context context = new Context(ModelType.FLAT); |
|
|
|
Context context = new Context(ModelType.FLAT); |
|
|
|
context.setId("Potato"); |
|
|
|
context.setId("Potato"); |
|
|
|
context.setTargetRuntime("MyBatis3Simple"); |
|
|
|
context.setTargetRuntime("MyBatis3Simple"); |
|
|
@ -92,20 +89,25 @@ public abstract class CodeGenerator { |
|
|
|
tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null)); |
|
|
|
tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null)); |
|
|
|
context.addTableConfiguration(tableConfiguration); |
|
|
|
context.addTableConfiguration(tableConfiguration); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<String> warnings; |
|
|
|
DefaultShellCallback callback = new DefaultShellCallback(overwrite); |
|
|
|
MyBatisGenerator generator; |
|
|
|
|
|
|
|
try { |
|
|
|
Configuration config = new Configuration(); |
|
|
|
Configuration config = new Configuration(); |
|
|
|
config.addContext(context); |
|
|
|
config.addContext(context); |
|
|
|
config.validate(); |
|
|
|
config.validate(); |
|
|
|
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); |
|
|
|
|
|
|
|
myBatisGenerator.generate(null); |
|
|
|
boolean overwrite = true; |
|
|
|
String modelName = tableNameConvertUpperCamel(tableName); |
|
|
|
DefaultShellCallback callback = new DefaultShellCallback(overwrite); |
|
|
|
System.out.println(modelName + ".java 生成成功"); |
|
|
|
warnings = new ArrayList<String>(); |
|
|
|
System.out.println(modelName + "Mapper.java 生成成功"); |
|
|
|
generator = new MyBatisGenerator(config, callback, warnings); |
|
|
|
System.out.println(modelName + "Mapper.xml 生成成功"); |
|
|
|
generator.generate(null); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException("生成Model和Mapper失败", e); |
|
|
|
throw new RuntimeException("生成Model和Mapper失败", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) { |
|
|
|
|
|
|
|
throw new RuntimeException("生成Model和Mapper失败:" + warnings); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void genService(String tableName) { |
|
|
|
public static void genService(String tableName) { |
|
|
@ -158,6 +160,7 @@ public abstract class CodeGenerator { |
|
|
|
if (!file.getParentFile().exists()) { |
|
|
|
if (!file.getParentFile().exists()) { |
|
|
|
file.getParentFile().mkdirs(); |
|
|
|
file.getParentFile().mkdirs(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//cfg.getTemplate("controller-restful.ftl").process(data, new FileWriter(file));
|
|
|
|
cfg.getTemplate("controller.ftl").process(data, new FileWriter(file)); |
|
|
|
cfg.getTemplate("controller.ftl").process(data, new FileWriter(file)); |
|
|
|
|
|
|
|
|
|
|
|
System.out.println(modelNameUpperCamel + "Controller.java 生成成功"); |
|
|
|
System.out.println(modelNameUpperCamel + "Controller.java 生成成功"); |
|
|
|