|
13 | 13 | /** |
14 | 14 | * 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。 |
15 | 15 | */ |
16 | | -public abstract class CodeGenerator { |
| 16 | +public class CodeGenerator { |
17 | 17 | //JDBC配置,请修改为你项目的实际配置 |
18 | 18 | private static final String JDBC_URL = "jdbc:mysql://localhost:3306/test"; |
19 | 19 | private static final String JDBC_USERNAME = "root"; |
@@ -50,62 +50,64 @@ public static void genCode(String... tableNames) { |
50 | 50 |
|
51 | 51 |
|
52 | 52 | 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; |
53 | 94 | 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); |
97 | 95 | Configuration config = new Configuration(); |
98 | 96 | config.addContext(context); |
99 | 97 | 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); |
106 | 104 | } catch (Exception e) { |
107 | 105 | throw new RuntimeException("生成Model和Mapper失败", e); |
108 | 106 | } |
| 107 | + |
| 108 | + if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) { |
| 109 | + throw new RuntimeException("生成Model和Mapper失败:" + warnings); |
| 110 | + } |
109 | 111 | } |
110 | 112 |
|
111 | 113 | public static void genService(String tableName) { |
@@ -158,6 +160,7 @@ public static void genController(String tableName) { |
158 | 160 | if (!file.getParentFile().exists()) { |
159 | 161 | file.getParentFile().mkdirs(); |
160 | 162 | } |
| 163 | + //cfg.getTemplate("controller-restful.ftl").process(data, new FileWriter(file)); |
161 | 164 | cfg.getTemplate("controller.ftl").process(data, new FileWriter(file)); |
162 | 165 |
|
163 | 166 | System.out.println(modelNameUpperCamel + "Controller.java 生成成功"); |
|
0 commit comments