Skip to content

Commit 87ed848

Browse files
committed
add> optimize code
1 parent 6d83bfa commit 87ed848

File tree

1 file changed

+53
-50
lines changed

1 file changed

+53
-50
lines changed

src/test/java/CodeGenerator.java

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/**
1414
* 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。
1515
*/
16-
public abstract class CodeGenerator {
16+
public class CodeGenerator {
1717
//JDBC配置,请修改为你项目的实际配置
1818
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/test";
1919
private static final String JDBC_USERNAME = "root";
@@ -50,62 +50,64 @@ public static void genCode(String... tableNames) {
5050

5151

5252
public static void genModelAndMapper(String tableName) {
53+
Context context = new Context(ModelType.FLAT);
54+
context.setId("Potato");
55+
context.setTargetRuntime("MyBatis3Simple");
56+
context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
57+
context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
58+
59+
JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
60+
jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
61+
jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
62+
jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
63+
jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
64+
context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
65+
66+
PluginConfiguration pluginConfiguration = new PluginConfiguration();
67+
pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
68+
pluginConfiguration.addProperty("mappers", ProjectConstant.MAPPER_INTERFACE_REFERENCE);
69+
context.addPluginConfiguration(pluginConfiguration);
70+
71+
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
72+
javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
73+
javaModelGeneratorConfiguration.setTargetPackage(ProjectConstant.MODEL_PACKAGE);
74+
context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
75+
76+
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
77+
sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + RESOURCES_PATH);
78+
sqlMapGeneratorConfiguration.setTargetPackage("mapper");
79+
context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
80+
81+
JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
82+
javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
83+
javaClientGeneratorConfiguration.setTargetPackage(ProjectConstant.MAPPER_PACKAGE);
84+
javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
85+
context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
86+
87+
TableConfiguration tableConfiguration = new TableConfiguration(context);
88+
tableConfiguration.setTableName(tableName);
89+
tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
90+
context.addTableConfiguration(tableConfiguration);
91+
92+
List<String> warnings;
93+
MyBatisGenerator generator;
5394
try {
54-
List<String> warnings = new ArrayList<String>();
55-
boolean overwrite = true;
56-
Context context = new Context(ModelType.FLAT);
57-
context.setId("Potato");
58-
context.setTargetRuntime("MyBatis3Simple");
59-
context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
60-
context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
61-
62-
JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
63-
jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
64-
jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
65-
jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
66-
jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
67-
context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
68-
69-
PluginConfiguration pluginConfiguration = new PluginConfiguration();
70-
pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
71-
pluginConfiguration.addProperty("mappers", ProjectConstant.MAPPER_INTERFACE_REFERENCE);
72-
context.addPluginConfiguration(pluginConfiguration);
73-
74-
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
75-
javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
76-
javaModelGeneratorConfiguration.setTargetPackage(ProjectConstant.MODEL_PACKAGE);
77-
context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
78-
79-
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
80-
sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + RESOURCES_PATH);
81-
sqlMapGeneratorConfiguration.setTargetPackage("mapper");
82-
context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
83-
84-
JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
85-
javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
86-
javaClientGeneratorConfiguration.setTargetPackage(ProjectConstant.MAPPER_PACKAGE);
87-
javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
88-
context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
89-
90-
TableConfiguration tableConfiguration = new TableConfiguration(context);
91-
tableConfiguration.setTableName(tableName);
92-
tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
93-
context.addTableConfiguration(tableConfiguration);
94-
95-
96-
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
9795
Configuration config = new Configuration();
9896
config.addContext(context);
9997
config.validate();
100-
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
101-
myBatisGenerator.generate(null);
102-
String modelName = tableNameConvertUpperCamel(tableName);
103-
System.out.println(modelName + ".java 生成成功");
104-
System.out.println(modelName + "Mapper.java 生成成功");
105-
System.out.println(modelName + "Mapper.xml 生成成功");
98+
99+
boolean overwrite = true;
100+
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
101+
warnings = new ArrayList<String>();
102+
generator = new MyBatisGenerator(config, callback, warnings);
103+
generator.generate(null);
106104
} catch (Exception e) {
107105
throw new RuntimeException("生成Model和Mapper失败", e);
108106
}
107+
108+
if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) {
109+
throw new RuntimeException("生成Model和Mapper失败:" + warnings);
110+
}
109111
}
110112

111113
public static void genService(String tableName) {
@@ -158,6 +160,7 @@ public static void genController(String tableName) {
158160
if (!file.getParentFile().exists()) {
159161
file.getParentFile().mkdirs();
160162
}
163+
//cfg.getTemplate("controller-restful.ftl").process(data, new FileWriter(file));
161164
cfg.getTemplate("controller.ftl").process(data, new FileWriter(file));
162165

163166
System.out.println(modelNameUpperCamel + "Controller.java 生成成功");

0 commit comments

Comments
 (0)