templates;
39 | /**
40 | * package包名前缀
41 | */
42 | private String packagePrefix;
43 |
44 | /**
45 | * 获取不同系统的package转换目录分割后的路径
46 | *
47 | * @return 获取不同系统格式化后的包名路径
48 | */
49 | public String getDiffSysPackagePrefix() {
50 | if (!StringUtil.isNotEmpty(packagePrefix)) {
51 | return packagePrefix;
52 | }
53 | return packagePrefix.replace(".", File.separator);
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/configuration/TemplateConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.configuration;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import lombok.Data;
19 |
20 | /**
21 | * 模板配置基本信息
22 | *
23 | * @author:于起宇
24 | * ===============================
25 | * Created with IDEA.
26 | * Date:2018/7/12
27 | * Time:4:45 PM
28 | * 简书:http://www.jianshu.com/u/092df3f77bca
29 | * ================================
30 | */
31 | @Data
32 | public class TemplateConfiguration {
33 | /**
34 | * 模板名称
35 | * 如:model.ftl
36 | */
37 | private String name;
38 | /**
39 | * 包名
40 | */
41 | private String packageName;
42 | /**
43 | * 生成文件的前缀名
44 | * 如:DXxx,D则是前缀
45 | */
46 | private String filePrefix;
47 | /**
48 | * 生成文件的后缀名
49 | */
50 | private String fileSuffix;
51 | }
52 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/AbstractDataBase.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.database;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
19 | import com.gitee.hengboy.builder.common.enums.ErrorEnum;
20 | import com.gitee.hengboy.builder.common.enums.TableMetaDataEnum;
21 | import com.gitee.hengboy.builder.common.exception.CodeBuilderException;
22 | import com.gitee.hengboy.builder.common.util.StringUtil;
23 | import com.gitee.hengboy.builder.core.database.model.Column;
24 | import com.gitee.hengboy.builder.core.database.model.Table;
25 | import com.gitee.hengboy.builder.core.database.model.util.JavaTypeResolver;
26 | import com.gitee.hengboy.builder.core.database.model.util.JdbcTypeResolver;
27 |
28 | import java.sql.*;
29 | import java.util.ArrayList;
30 | import java.util.List;
31 |
32 | /**
33 | * 抽象数据库实例类
34 | *
35 | * @author:于起宇 ===============================
36 | * Created with IDEA.
37 | * Date:2018/7/8
38 | * Time:5:24 PM
39 | * 简书:http://www.jianshu.com/u/092df3f77bca
40 | * ================================
41 | */
42 | public abstract class AbstractDataBase implements DataBase {
43 | /**
44 | * 数据库连接对象
45 | */
46 | protected Connection connection;
47 | /**
48 | * 代码生成器参数配置实例
49 | */
50 | protected CodeBuilderProperties codeBuilderProperties;
51 |
52 | /**
53 | * 构造函数传递参数配置实例
54 | *
55 | * @param codeBuilderProperties 生成所需参数对象
56 | */
57 | public AbstractDataBase(CodeBuilderProperties codeBuilderProperties) {
58 | this.codeBuilderProperties = codeBuilderProperties;
59 | // 获取数据库连接
60 | getConnection();
61 | }
62 |
63 | /**
64 | * 获取数据库连接对象
65 | *
66 | * @return 数据库连接对象
67 | */
68 | public Connection getConnection() {
69 | try {
70 | /*
71 | * 连接不存在 || 连接失效
72 | * 重新获取连接
73 | */
74 | if (connection == null || connection.isClosed()) {
75 | // spring-boot-starter方式获取数据库连接
76 | if (codeBuilderProperties.getDataSource() != null) {
77 | connection = codeBuilderProperties.getDataSource().getConnection();
78 | }
79 | // maven-plugin插件形式获取数据库连接
80 | else {
81 | // 默认使用数据库驱动类型内的限定类名
82 | String driverClassName = codeBuilderProperties.getDbType().getValue();
83 | // 存在自定义的驱动限定类名时使用自定义来实例化驱动对象
84 | if (StringUtil.isNotEmpty(codeBuilderProperties.getDbDriverClassName())) {
85 | driverClassName = codeBuilderProperties.getDbDriverClassName();
86 | }
87 | //加载驱动程序
88 | Class.forName(driverClassName);
89 | // 获取数据库连接
90 | connection = DriverManager.getConnection(codeBuilderProperties.getDbUrl(), codeBuilderProperties.getDbUserName(), codeBuilderProperties.getDbPassword());
91 | }
92 | }
93 | return connection;
94 | } catch (Exception e) {
95 | e.printStackTrace();
96 | }
97 | throw new CodeBuilderException(ErrorEnum.NOT_GET_CONNECTION);
98 | }
99 |
100 | /**
101 | * 关闭数据库连接对象
102 | */
103 | public void closeConnection() {
104 | try {
105 | if (!connection.isClosed()) {
106 | connection.close();
107 | }
108 | } catch (SQLException e) {
109 | e.printStackTrace();
110 | }
111 | }
112 |
113 | /**
114 | * 获取表名列表
115 | *
116 | * @param tableNamePattern 表名表达式
117 | * @return 表名列表
118 | */
119 | public List getTableNames(String tableNamePattern) {
120 | try {
121 | // 获取该数据库内的所有表
122 | ResultSet resultSet = connection.getMetaData().getTables(connection.getCatalog(), codeBuilderProperties.getDbUserName(), tableNamePattern, new String[]{"TABLE"});
123 | List tables = new ArrayList();
124 | while (resultSet.next()) {
125 | // 获取表名
126 | String tableName = resultSet.getString(TableMetaDataEnum.TABLE_NAME.getValue());
127 | // 获取表格基本信息
128 | tables.add(tableName);
129 | }
130 | return tables;
131 | } catch (Exception e) {
132 | e.printStackTrace();
133 | }
134 | throw new CodeBuilderException(ErrorEnum.NOT_GET_TABLE);
135 | }
136 |
137 | /**
138 | * 获取数据库内的所有表
139 | *
140 | * @param tableNamePattern 表名表达式
141 | * @return 数据表列表结果集
142 | */
143 | public List getTables(String tableNamePattern) {
144 | try {
145 | // 获取该数据库内的所有表
146 | ResultSet resultSet = connection.getMetaData().getTables(connection.getCatalog(), codeBuilderProperties.getDbUserName(), tableNamePattern, new String[]{"TABLE"});
147 | List tables = new ArrayList();
148 | while (resultSet.next()) {
149 | // 获取表名
150 | String tableName = resultSet.getString(TableMetaDataEnum.TABLE_NAME.getValue());
151 | // 获取表格基本信息
152 | tables.add(getTable(tableName));
153 | }
154 | return tables;
155 | } catch (Exception e) {
156 | e.printStackTrace();
157 | }
158 | throw new CodeBuilderException(ErrorEnum.NOT_GET_TABLE);
159 | }
160 |
161 | /**
162 | * 获取数据表基本信息
163 | *
164 | * @param tableName 表名
165 | * @return 数据表对象实例
166 | */
167 | public Table getTable(String tableName) {
168 | // 构建数据表对象
169 | Table.TableBuilder tableBuilder = Table.builder();
170 | try {
171 | ResultSet resultSet = getConnection().getMetaData().getTables(null, null, tableName, new String[]{"TABLE"});
172 | if (resultSet.next()) {
173 | tableBuilder
174 | // 表名
175 | .tableName(tableName)
176 | // 实体类名称
177 | .entityName(StringUtil.getCamelCaseString(tableName, true))
178 | // 表类别
179 | .catalog(resultSet.getString(TableMetaDataEnum.TABLE_CAT.getValue()))
180 | // 表模式
181 | .schema(resultSet.getString(TableMetaDataEnum.TABLE_SCHEMA.getValue()))
182 | // 表类型
183 | .tableType(resultSet.getString(TableMetaDataEnum.TABLE_TYPE.getValue()))
184 | // 获取备注信息
185 | .remark(getTableComment(tableName))
186 | // 所有列,排除主键
187 | .columns(getColumns(tableName))
188 | // 主键列表
189 | .primaryKeys(getPrimaryKeys(tableName));
190 | }
191 | } catch (SQLException e) {
192 | e.printStackTrace();
193 | }
194 | return tableBuilder.build().buildAfterSetting(codeBuilderProperties);
195 | }
196 |
197 | /**
198 | * 根据结果集获取列的详细信息
199 | * 根据不同的columnPattern获取到的列元数据
200 | *
201 | * @param resultSet 结果集
202 | * @param isPrimaryColumn true:主键列,false:普通列
203 | * @return 列基本信息对象
204 | */
205 | private Column getColumn(ResultSet resultSet, boolean isPrimaryColumn) {
206 | try {
207 | // 数据库字段类型
208 | int jdbcType = resultSet.getInt(TableMetaDataEnum.DATA_TYPE.getValue());
209 | // 列名
210 | String columnName = resultSet.getString(TableMetaDataEnum.COLUMN_NAME.getValue());
211 | // 表名
212 | String tableName = resultSet.getString(TableMetaDataEnum.TABLE_NAME.getValue());
213 |
214 | return Column.builder()
215 | // 列名
216 | .columnName(columnName)
217 | // 列长度
218 | .size(resultSet.getInt(TableMetaDataEnum.COLUMN_SIZE.getValue()))
219 | // 是否为空
220 | .nullable(resultSet.getBoolean(TableMetaDataEnum.NULLABLE.getValue()))
221 | // 默认值
222 | .defaultValue(resultSet.getString(TableMetaDataEnum.COLUMN_DEF.getValue()))
223 | // 数据库列类型
224 | .jdbcType(jdbcType)
225 | // 是否自增
226 | .autoincrement(hasColumn(resultSet, TableMetaDataEnum.IS_AUTOINCREMENT.getValue()) ? resultSet.getBoolean(TableMetaDataEnum.IS_AUTOINCREMENT.getValue()) : false)
227 | // 列备注信息
228 | .remark(resultSet.getString(TableMetaDataEnum.REMARKS.getValue()))
229 | // 精度
230 | .decimalDigits(resultSet.getInt(TableMetaDataEnum.DECIMAL_DIGITS.getValue()))
231 | // jdbc类型名称
232 | .jdbcTypeName(JdbcTypeResolver.getJdbcTypeName(jdbcType))
233 | // 格式化后的java field
234 | .javaProperty(StringUtil.getCamelCaseString(columnName, false))
235 | // 对应的java数据类型
236 | .javaType(JavaTypeResolver.getJavaType(jdbcType, false))
237 | // java数据类型
238 | .fullJavaType(JavaTypeResolver.getJavaType(jdbcType, true))
239 | // 是否为主键列
240 | // 如果是主键获取列信息,直接返回true
241 | .primaryKey(!isPrimaryColumn ? isPrimaryKey(tableName, columnName) : isPrimaryColumn)
242 | // 是否为外键列
243 | // 如果是主键获取列信息,直接返回false
244 | .foreignKey(!isPrimaryColumn ? isForeignKey(tableName, columnName) : false)
245 | .build();
246 | } catch (SQLException e) {
247 | e.printStackTrace();
248 | }
249 | return null;
250 | }
251 |
252 | /**
253 | * 获取表内的所有列
254 | *
255 | * @param tableName 表名
256 | * @return 数据列列表
257 | */
258 | public List getColumns(String tableName) {
259 | List columns = new ArrayList();
260 | try {
261 | // 获取列信息
262 | ResultSet resultSet = getConnection().getMetaData().getColumns(null, null, tableName, "%");
263 | while (resultSet.next()) {
264 | columns.add(getColumn(resultSet, false));
265 | }
266 | return columns;
267 | } catch (SQLException e) {
268 | e.printStackTrace();
269 | }
270 | throw new CodeBuilderException(ErrorEnum.NOT_GET_COLUMN, tableName);
271 | }
272 |
273 | /**
274 | * 验证指定的字段是否为主键
275 | *
276 | * @param tableName 表名
277 | * @param columnName 列名
278 | * @return 是否为主键,true:主键,false:非主键
279 | */
280 | public boolean isPrimaryKey(String tableName, String columnName) {
281 | try {
282 | // 获取表内的主键列表
283 | ResultSet resultSet = connection.getMetaData().getPrimaryKeys(null, null, tableName);
284 | while (resultSet.next()) {
285 | // 获取主键的列名
286 | String pkColumnName = resultSet.getString(TableMetaDataEnum.COLUMN_NAME.getValue());
287 | if (columnName.equals(pkColumnName)) {
288 | return true;
289 | }
290 | }
291 | } catch (SQLException e) {
292 | e.printStackTrace();
293 | }
294 | return false;
295 | }
296 |
297 | /**
298 | * 验证指定列是否为外键
299 | *
300 | * @param tableName 表名
301 | * @param columnName 列名
302 | * @return 是否为外键,true:外键,false:非外键
303 | */
304 | public boolean isForeignKey(String tableName, String columnName) {
305 | try {
306 | // 获取表内的外键列表
307 | ResultSet resultSet = connection.getMetaData().getImportedKeys(null, null, tableName);
308 | while (resultSet.next()) {
309 | // 获取外键的列名
310 | String fkColumnName = resultSet.getString(TableMetaDataEnum.FK_COLUMN_NAME.getValue());
311 | if (columnName.equals(fkColumnName)) {
312 | return true;
313 | }
314 | }
315 | } catch (SQLException e) {
316 | e.printStackTrace();
317 | }
318 | return false;
319 | }
320 |
321 | /**
322 | * 获取表内的主键列表
323 | *
324 | * @param tableName 表名
325 | * @return 主键列表
326 | */
327 | public List getPrimaryKeys(String tableName) {
328 | try {
329 | // 获取表内的主键列表
330 | ResultSet resultSet = connection.getMetaData().getPrimaryKeys(null, null, tableName);
331 | List primaryKeys = new ArrayList();
332 | while (resultSet.next()) {
333 | // 获取主键的列名
334 | String columnName = resultSet.getString(TableMetaDataEnum.COLUMN_NAME.getValue());
335 | // 获取主键列的详细信息
336 | ResultSet columnResultSet = connection.getMetaData().getColumns(null, null, tableName, columnName);
337 | if (columnResultSet.next()) {
338 | // 添加主键信息
339 | primaryKeys.add(getColumn(columnResultSet, true));
340 | }
341 | }
342 | return primaryKeys;
343 | } catch (SQLException e) {
344 | e.printStackTrace();
345 | }
346 | throw new CodeBuilderException(ErrorEnum.NOT_GET_PRIMARY_KEYS, tableName);
347 | }
348 |
349 | /**
350 | * 结果集内是否存在自增的列
351 | *
352 | * @param rs 结果集
353 | * @param columnName 自增列名
354 | * @return true:存在列表,false:不存在列
355 | * @throws SQLException 数据库异常
356 | */
357 | private boolean hasColumn(ResultSet rs, String columnName) throws SQLException {
358 | ResultSetMetaData resultSetMetaData = rs.getMetaData();
359 | int columns = resultSetMetaData.getColumnCount();
360 | for (int x = 1; x <= columns; x++) {
361 | if (columnName.equals(resultSetMetaData.getColumnName(x))) {
362 | return true;
363 | }
364 | }
365 | return false;
366 | }
367 | }
368 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/DataBase.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.database;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | import com.gitee.hengboy.builder.core.database.model.Column;
18 | import com.gitee.hengboy.builder.core.database.model.Table;
19 |
20 | import java.sql.Connection;
21 | import java.util.List;
22 |
23 | /**
24 | * 数据库实例接口
25 | *
26 | * @author:于起宇 ===============================
27 | * Created with IDEA.
28 | * Date:2018/7/8
29 | * Time:5:23 PM
30 | * 简书:http://www.jianshu.com/u/092df3f77bca
31 | * ================================
32 | */
33 | public interface DataBase {
34 | /**
35 | * 获取数据库的连接
36 | *
37 | * @return 数据库连接对象
38 | */
39 | Connection getConnection();
40 |
41 | /**
42 | * 关闭数据库连接
43 | */
44 | void closeConnection();
45 |
46 | /**
47 | * 获取数据库内的所有数据表
48 | *
49 | * @param tableNamePattern 表名称表达式过滤,如:sys_%,则仅仅查询出【sys_】开头的所有表
50 | * @return 数据表列表
51 | */
52 | List getTables(String tableNamePattern);
53 |
54 | /**
55 | * 获取表名称列表
56 | *
57 | * @param tableNamePattern 获取表名时使用的表达式
58 | * @return 表名列表
59 | */
60 | List getTableNames(String tableNamePattern);
61 |
62 | /**
63 | * 根据表名获取数据表对象
64 | *
65 | * @param tableName 表名
66 | * @return 表对象实例
67 | */
68 | Table getTable(String tableName);
69 |
70 | /**
71 | * 查询表内的全部列表
72 | *
73 | * @param tableName 表名
74 | * @return 数据列列表
75 | */
76 | List getColumns(String tableName);
77 |
78 | /**
79 | * 查询表内
80 | *
81 | * @param tableName 表名
82 | * @return 主键列表
83 | */
84 | List getPrimaryKeys(String tableName);
85 |
86 | /**
87 | * 是否为主键列
88 | *
89 | * @param tableName 表名
90 | * @param columnName 列名
91 | * @return 是否为主键,true:主键,false:非主键
92 | */
93 | boolean isPrimaryKey(String tableName, String columnName);
94 |
95 | /**
96 | * 是否为外键
97 | *
98 | * @param tableName 表名
99 | * @param columnName 列名
100 | * @return 是否外键,true:外键,false:非外键
101 | */
102 | boolean isForeignKey(String tableName, String columnName);
103 |
104 | /**
105 | * 获取表备注信息
106 | *
107 | * @param tableName 表名
108 | * @return 表备注信息
109 | */
110 | String getTableComment(String tableName);
111 | }
112 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/DataBaseFactory.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.database;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
19 | import com.gitee.hengboy.builder.common.enums.DbTypeEnum;
20 | import com.gitee.hengboy.builder.common.enums.ErrorEnum;
21 | import com.gitee.hengboy.builder.common.exception.CodeBuilderException;
22 |
23 | import java.lang.reflect.Constructor;
24 |
25 | /**
26 | * 数据库工厂
27 | *
28 | * @author:于起宇 ===============================
29 | * Created with IDEA.
30 | * Date:2018/7/8
31 | * Time:5:22 PM
32 | * 简书:http://www.jianshu.com/u/092df3f77bca
33 | * ================================
34 | */
35 | public class DataBaseFactory {
36 | /**
37 | * 构造函数私有化
38 | * 禁止通过new方式实例化对象
39 | */
40 | private DataBaseFactory() {
41 | }
42 |
43 | /**
44 | * 获取配置的数据库类型实例
45 | *
46 | * @param codeBuilderProperties 配置构建参数实体
47 | * @return 数据库实例
48 | */
49 | public static DataBase newInstance(CodeBuilderProperties codeBuilderProperties) {
50 | // 数据库类型枚举实例
51 | DbTypeEnum dbTypeEnum = codeBuilderProperties.getDbType();
52 | try {
53 | // 获取数据库实现类的构造函数
54 | Constructor constructor = dbTypeEnum.getDataBaseImplClass().getConstructor(CodeBuilderProperties.class);
55 | // 反射获取数据库实现类实例
56 | return (DataBase) constructor.newInstance(codeBuilderProperties);
57 | } catch (Exception e) {
58 | e.printStackTrace();
59 | throw new CodeBuilderException(ErrorEnum.NOT_ALLOW_DB_TYPE);
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/impl/Db2DataBase.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.database.impl;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
18 | import com.gitee.hengboy.builder.core.database.AbstractDataBase;
19 |
20 | /**
21 | * DB2数据库实现
22 | * @author:于起宇
23 | * ===============================
24 | * Created with IDEA.
25 | * Date:2018/7/9
26 | * Time:11:38 AM
27 | * 简书:http://www.jianshu.com/u/092df3f77bca
28 | * ================================
29 | */
30 | public class Db2DataBase extends AbstractDataBase {
31 |
32 | public Db2DataBase(CodeBuilderProperties codeBuilderProperties) {
33 | super(codeBuilderProperties);
34 | }
35 |
36 | public String getTableComment(String tableName){
37 | // TODO 暂未支持
38 | return null;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/impl/MySqlDataBase.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.database.impl;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
18 | import com.gitee.hengboy.builder.common.enums.ErrorEnum;
19 | import com.gitee.hengboy.builder.common.exception.CodeBuilderException;
20 | import com.gitee.hengboy.builder.core.database.AbstractDataBase;
21 |
22 | import java.sql.PreparedStatement;
23 | import java.sql.ResultSet;
24 | import java.sql.SQLException;
25 |
26 | /**
27 | * MySQL数据库类型实体
28 | *
29 | * @author:于起宇
30 | * ===============================
31 | * Created with IDEA.
32 | * Date:2018/7/8
33 | * Time:5:24 PM
34 | * 简书:http://www.jianshu.com/u/092df3f77bca
35 | * ================================
36 | */
37 | public class MySqlDataBase extends AbstractDataBase {
38 | /**
39 | * MySQL查询表状态的执行SQL
40 | */
41 | private static final String TABLE_COMMENT_SQL = "show table status where NAME=?";
42 | /**
43 | * 表备注字段名称
44 | */
45 | private static final String TABLE_COMMENT_COLUMN = "COMMENT";
46 |
47 | public MySqlDataBase(CodeBuilderProperties codeBuilderProperties) {
48 | super(codeBuilderProperties);
49 | }
50 |
51 | /**
52 | * 获取表备注信息
53 | *
54 | * @param tableName 表名
55 | * @return 表备注信息
56 | */
57 | public String getTableComment(String tableName) {
58 | try {
59 | PreparedStatement statement = connection.prepareStatement(TABLE_COMMENT_SQL);
60 | statement.setString(1, tableName);
61 | ResultSet resultSet = statement.executeQuery();
62 | if (resultSet.next()) {
63 | return resultSet.getString(TABLE_COMMENT_COLUMN);
64 | }
65 | resultSet.close();
66 | statement.close();
67 | } catch (SQLException e) {
68 | e.printStackTrace();
69 | }
70 | throw new CodeBuilderException(ErrorEnum.NOT_GET_COMMENT, tableName);
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/impl/OracleDataBase.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.database.impl;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
18 | import com.gitee.hengboy.builder.core.database.AbstractDataBase;
19 |
20 | /**
21 | * Oracle数据库实现
22 | *
23 | * @author:于起宇
24 | * ===============================
25 | * Created with IDEA.
26 | * Date:2018/7/9
27 | * Time:11:41 AM
28 | * 简书:http://www.jianshu.com/u/092df3f77bca
29 | * ================================
30 | */
31 | public class OracleDataBase extends AbstractDataBase {
32 |
33 | public OracleDataBase(CodeBuilderProperties codeBuilderProperties) {
34 | super(codeBuilderProperties);
35 | }
36 |
37 | /**
38 | * 获取表的备注信息
39 | *
40 | * @param tableName 表名
41 | * @return 表备注信息
42 | */
43 | public String getTableComment(String tableName) {
44 | // TODO 暂未支持
45 | return null;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/impl/PostgreSqlDataBase.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.database.impl;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
18 | import com.gitee.hengboy.builder.core.database.AbstractDataBase;
19 |
20 | /**
21 | * Postgresql 数据库
22 | *
23 | * @author:于起宇
24 | * ===============================
25 | * Created with IDEA.
26 | * Date:2018/7/9
27 | * Time:11:40 AM
28 | * 简书:http://www.jianshu.com/u/092df3f77bca
29 | * ================================
30 | */
31 | public class PostgreSqlDataBase extends AbstractDataBase {
32 |
33 | public PostgreSqlDataBase(CodeBuilderProperties codeBuilderProperties) {
34 | super(codeBuilderProperties);
35 | }
36 |
37 |
38 | public String getTableComment(String tableName) {
39 | // TODO 暂未支持
40 | return null;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/impl/SqlServerDataBase.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.database.impl;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
18 | import com.gitee.hengboy.builder.core.database.AbstractDataBase;
19 |
20 | /**
21 | * SqlServer数据库实现
22 | * @author:于起宇
23 | * ===============================
24 | * Created with IDEA.
25 | * Date:2018/7/9
26 | * Time:11:41 AM
27 | * 简书:http://www.jianshu.com/u/092df3f77bca
28 | * ================================
29 | */
30 | public class SqlServerDataBase extends AbstractDataBase {
31 | public SqlServerDataBase(CodeBuilderProperties codeBuilderProperties) {
32 | super(codeBuilderProperties);
33 | }
34 |
35 |
36 | public String getTableComment(String tableName){
37 | // TODO 暂未支持
38 | return null;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/model/Column.java:
--------------------------------------------------------------------------------
1 |
2 | package com.gitee.hengboy.builder.core.database.model;
3 |
4 | /**
5 | * Copyright 2018 恒宇少年
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * you may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | */
19 | import lombok.Builder;
20 | import lombok.Getter;
21 | import lombok.Setter;
22 |
23 | /**
24 | * 数据表内的列对象
25 | *
26 | * @author:于起宇
27 | * ===============================
28 | * Created with IDEA.
29 | * Date:2018/7/8
30 | * Time:5:44 PM
31 | * 简书:http://www.jianshu.com/u/092df3f77bca
32 | * ================================
33 | */
34 | @Getter
35 | @Setter
36 | @Builder
37 | public class Column {
38 | /**
39 | * 列名
40 | */
41 | private String columnName;
42 | /**
43 | * 是否为主键
44 | */
45 | private boolean primaryKey;
46 | /**
47 | * 是否为外键
48 | */
49 | private boolean foreignKey;
50 | /**
51 | * 列长度
52 | */
53 | private int size;
54 | /**
55 | * 小数点位数
56 | */
57 | private int decimalDigits;
58 | /**
59 | * 是否为空
60 | */
61 | private boolean nullable;
62 | /**
63 | * 是否自增
64 | */
65 | private boolean autoincrement;
66 | /**
67 | * 默认值
68 | */
69 | private String defaultValue;
70 | /**
71 | * 备注信息
72 | */
73 | private String remark;
74 | /**
75 | * 数据库类型
76 | */
77 | private int jdbcType;
78 | /**
79 | * java.sql.Types对应的类型名称
80 | */
81 | private String jdbcTypeName;
82 | /**
83 | * 列名格式化后对应实体类内的属性
84 | */
85 | private String javaProperty;
86 | /**
87 | * java.lang.xxx数据类型
88 | */
89 | private String javaType;
90 | /**
91 | * java.lang.xxx数据类型全名称
92 | */
93 | private String fullJavaType;
94 | }
95 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/model/Key.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.database.model;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | import lombok.Builder;
18 | import lombok.Data;
19 |
20 | /**
21 | * 数据表内的主键、外键对象
22 | *
23 | * @author:于起宇
24 | * ===============================
25 | * Created with IDEA.
26 | * Date:2018/7/8
27 | * Time:5:32 PM
28 | * 简书:http://www.jianshu.com/u/092df3f77bca
29 | * ================================
30 | */
31 | @Data
32 | @Builder
33 | public class Key {
34 | /**
35 | * 主键表名
36 | */
37 | private String pkTableName;
38 | /**
39 | * 主键列名
40 | */
41 | private String pkColumnName;
42 | /**
43 | * 外键表名
44 | */
45 | private String fkTableName;
46 | /**
47 | * 外键列名
48 | */
49 | private String fkColumnName;
50 | /**
51 | * 外键中的序列号(值1表示外键的第一列,值2表示外键中的第二列)。
52 | */
53 | private Integer seq;
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/model/Table.java:
--------------------------------------------------------------------------------
1 |
2 | package com.gitee.hengboy.builder.core.database.model;
3 |
4 | /**
5 | * Copyright 2018 恒宇少年
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * you may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | */
19 |
20 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
21 | import com.gitee.hengboy.builder.common.enums.JavaTypeEnum;
22 | import com.gitee.hengboy.builder.common.util.StringUtil;
23 | import lombok.Builder;
24 | import lombok.Data;
25 |
26 | import java.util.List;
27 |
28 | /**
29 | * 数据表对象
30 | *
31 | * @author:于起宇 ===============================
32 | * Created with IDEA.
33 | * Date:2018/7/8
34 | * Time:5:28 PM
35 | * 简书:http://www.jianshu.com/u/092df3f77bca
36 | * ================================
37 | */
38 | @Builder
39 | @Data
40 | public class Table {
41 | /**
42 | * 表名
43 | */
44 | private String tableName;
45 | /**
46 | * 表驼峰名称
47 | */
48 | private String tableCamelName;
49 | /**
50 | * 表类型
51 | */
52 | private String tableType;
53 | /**
54 | * 表别名
55 | */
56 | private String tableAlias;
57 | /**
58 | * 表备注信息
59 | */
60 | private String remark;
61 | /**
62 | * 实体名称
63 | */
64 | private String entityName;
65 | /**
66 | * catalog
67 | */
68 | private String catalog;
69 | /**
70 | * schema
71 | */
72 | private String schema;
73 | /**
74 | * 数据列列表
75 | */
76 | private List columns;
77 | /**
78 | * 主键列表
79 | */
80 | private List primaryKeys;
81 | /**
82 | * 是否存在java.sql.Date类型的列
83 | */
84 | private boolean hasSqlDate;
85 | /**
86 | * 是否存在TimeStamp的列
87 | */
88 | private boolean hasTimeStamp;
89 | /**
90 | * 是否存在BigDecimal的列
91 | */
92 | private boolean hasBigDecimal;
93 |
94 | /**
95 | * 构建对象后设置是否存在特殊类型的字段
96 | * 如:java.math.BigDecimal、java.sql.TimeStamp等
97 | *
98 | * @param codeBuilderProperties 参数对象
99 | * @return Table实例
100 | */
101 | public Table buildAfterSetting(CodeBuilderProperties codeBuilderProperties) {
102 | for (Column column : columns) {
103 | // 是否存在bigDecimal的列
104 | if (JavaTypeEnum.TYPE_BIG_DECIMAL.getFullName().equals(column.getFullJavaType())) {
105 | this.hasBigDecimal = true;
106 | }
107 | // 是否存在timeStamp的列
108 | if (JavaTypeEnum.TYPE_TIMESTAMP.getFullName().equals(column.getFullJavaType())) {
109 | this.hasTimeStamp = true;
110 | }
111 | // 是否存在java.sql.Date的列
112 | if (JavaTypeEnum.TYPE_DATE.getFullName().equals(column.getFullJavaType())) {
113 | this.hasSqlDate = true;
114 | }
115 | }
116 | // 自动忽略前缀
117 | if (StringUtil.isNotEmpty(codeBuilderProperties.getIgnoreClassPrefix())) {
118 | entityName = entityName.replaceFirst(codeBuilderProperties.getIgnoreClassPrefix(), "");
119 | }
120 | return this;
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/model/util/JavaTypeResolver.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.database.model.util;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | import com.gitee.hengboy.builder.common.enums.JavaTypeEnum;
18 |
19 | import java.sql.Types;
20 | import java.util.HashMap;
21 | import java.util.Map;
22 |
23 | /**
24 | * Java数据类型装载器
25 | *
26 | * @author:于起宇 ===============================
27 | * Created with IDEA.
28 | * Date:2018/7/8
29 | * Time:6:13 PM
30 | * 简书:http://www.jianshu.com/u/092df3f77bca
31 | * ================================
32 | */
33 | public class JavaTypeResolver {
34 |
35 | /**
36 | * java 字段类型映射集合
37 | */
38 | private static Map typeMap;
39 |
40 |
41 | static {
42 | typeMap = new HashMap();
43 | typeMap.put(Types.ARRAY, JavaTypeEnum.TYPE_OBJECT);
44 | typeMap.put(Types.BIGINT, JavaTypeEnum.TYPE_LONG);
45 | typeMap.put(Types.BINARY, JavaTypeEnum.TYPE_BYTE_ARRAY);
46 | typeMap.put(Types.BIT, JavaTypeEnum.TYPE_BOOLEAN);
47 | typeMap.put(Types.BLOB, JavaTypeEnum.TYPE_BYTE_ARRAY);
48 | typeMap.put(Types.BOOLEAN, JavaTypeEnum.TYPE_BOOLEAN);
49 | typeMap.put(Types.CHAR, JavaTypeEnum.TYPE_STRING);
50 | typeMap.put(Types.CLOB, JavaTypeEnum.TYPE_STRING);
51 | typeMap.put(Types.DATALINK, JavaTypeEnum.TYPE_STRING);
52 | typeMap.put(Types.DATE, JavaTypeEnum.TYPE_DATE);
53 | typeMap.put(Types.DISTINCT, JavaTypeEnum.TYPE_OBJECT);
54 | typeMap.put(Types.DOUBLE, JavaTypeEnum.TYPE_DOUBLE);
55 | typeMap.put(Types.FLOAT, JavaTypeEnum.TYPE_DOUBLE);
56 | typeMap.put(Types.INTEGER, JavaTypeEnum.TYPE_INTEGER);
57 | typeMap.put(Types.JAVA_OBJECT, JavaTypeEnum.TYPE_OBJECT);
58 | typeMap.put(Jdbc4Types.LONGNVARCHAR, JavaTypeEnum.TYPE_STRING);
59 | typeMap.put(Types.LONGVARBINARY, JavaTypeEnum.TYPE_BYTE_ARRAY);
60 | typeMap.put(Types.LONGVARCHAR, JavaTypeEnum.TYPE_STRING);
61 | typeMap.put(Jdbc4Types.NCHAR, JavaTypeEnum.TYPE_STRING);
62 | typeMap.put(Jdbc4Types.NCLOB, JavaTypeEnum.TYPE_STRING);
63 | typeMap.put(Jdbc4Types.NVARCHAR, JavaTypeEnum.TYPE_STRING);
64 | typeMap.put(Types.NULL, JavaTypeEnum.TYPE_OBJECT);
65 | typeMap.put(Types.OTHER, JavaTypeEnum.TYPE_OBJECT);
66 | typeMap.put(Types.REAL, JavaTypeEnum.TYPE_FLOAT);
67 | typeMap.put(Types.REF, JavaTypeEnum.TYPE_OBJECT);
68 | typeMap.put(Types.SMALLINT, JavaTypeEnum.TYPE_SHORT);
69 | typeMap.put(Types.STRUCT, JavaTypeEnum.TYPE_OBJECT);
70 | typeMap.put(Types.TIME, JavaTypeEnum.TYPE_DATE);
71 | typeMap.put(Types.TIMESTAMP, JavaTypeEnum.TYPE_TIMESTAMP);
72 | typeMap.put(Types.TINYINT, JavaTypeEnum.TYPE_BYTE);
73 | typeMap.put(Types.VARBINARY, JavaTypeEnum.TYPE_BYTE_ARRAY);
74 | typeMap.put(Types.VARCHAR, JavaTypeEnum.TYPE_STRING);
75 | typeMap.put(Types.DECIMAL, JavaTypeEnum.TYPE_BIG_DECIMAL);
76 | typeMap.put(Types.NUMERIC, JavaTypeEnum.TYPE_BIG_DECIMAL);
77 | }
78 |
79 | /**
80 | * 获取java数据类型
81 | *
82 | * @param jdbcType 数据库类型
83 | * @param isFullJavaType 是否获取java类型的全路径
84 | * @return java数据类型全路径
85 | */
86 | public static String getJavaType(int jdbcType, boolean isFullJavaType) {
87 | return isFullJavaType ? typeMap.get(jdbcType).getFullName() : typeMap.get(jdbcType).getShortName();
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/model/util/Jdbc4Types.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2018 恒宇少年
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.gitee.hengboy.builder.core.database.model.util;
17 |
18 | /**
19 | * JDBC 4 类型
20 | *
21 | * @author:于起宇
22 | * ===============================
23 | * Created with IDEA.
24 | * Date:2018/7/8
25 | * Time:5:28 PM
26 | * 简书:http://www.jianshu.com/u/092df3f77bca
27 | * ================================
28 | */
29 | public class Jdbc4Types {
30 | public static final int LONGNVARCHAR = -16;
31 | public static final int NVARCHAR = -9;
32 | public static final int NCHAR = -15;
33 | public static final int NCLOB = 2011;
34 |
35 | private Jdbc4Types() {
36 |
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/database/model/util/JdbcTypeResolver.java:
--------------------------------------------------------------------------------
1 |
2 | package com.gitee.hengboy.builder.core.database.model.util;
3 | /**
4 | * Copyright 2018 恒宇少年
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 | import java.sql.Types;
19 | import java.util.HashMap;
20 | import java.util.Map;
21 |
22 | /**
23 | * JDBC字段类型装载器
24 | *
25 | * @author:于起宇
26 | * ===============================
27 | * Created with IDEA.
28 | * Date:2018/7/8
29 | * Time:6:23 PM
30 | * 简书:http://www.jianshu.com/u/092df3f77bca
31 | * ================================
32 | */
33 | public class JdbcTypeResolver {
34 |
35 | private static Map typeToName;
36 | private static Map nameToType;
37 | private static final String TYPE_ARRAY = "ARRAY";
38 | private static final String TYPE_BIGINT = "BIGINT";
39 | private static final String TYPE_BINARY = "BINARY";
40 | private static final String TYPE_BIT = "BIT";
41 | private static final String TYPE_BLOB = "BLOB";
42 | private static final String TYPE_BOOLEAN = "BOOLEAN";
43 | private static final String TYPE_CHAR = "CHAR";
44 | private static final String TYPE_CLOB = "CLOB";
45 | private static final String TYPE_DATALINK = "DATALINK";
46 | private static final String TYPE_DATE = "DATE";
47 | private static final String TYPE_DECIMAL = "DECIMAL";
48 | private static final String TYPE_DISTINCT = "DISTINCT";
49 | private static final String TYPE_DOUBLE = "DOUBLE";
50 | private static final String TYPE_FLOAT = "FLOAT";
51 | private static final String TYPE_INTEGER = "INTEGER";
52 | private static final String TYPE_JAVA_OBJECT = "JAVA_OBJECT";
53 | private static final String TYPE_LONGVARBINARY = "LONGVARBINARY";
54 | private static final String TYPE_LONGVARCHAR = "LONGVARCHAR";
55 | private static final String TYPE_NCHAR = "NCHAR";
56 | private static final String TYPE_NCLOB = "NCLOB";
57 | private static final String TYPE_NVARCHAR = "NVARCHAR";
58 | private static final String TYPE_LONGNVARCHAR = "LONGNVARCHAR";
59 | private static final String TYPE_NULL = "NULL";
60 | private static final String TYPE_NUMERIC = "NUMERIC";
61 | private static final String TYPE_OTHER = "OTHER";
62 | private static final String TYPE_REAL = "REAL";
63 | private static final String TYPE_REF = "REF";
64 | private static final String TYPE_SMALLINT = "SMALLINT";
65 | private static final String TYPE_STRUCT = "STRUCT";
66 | private static final String TYPE_TIME = "TIME";
67 | private static final String TYPE_TIMESTAMP = "TIMESTAMP";
68 | private static final String TYPE_TINYINT = "TINYINT";
69 | private static final String TYPE_VARBINARY = "VARBINARY";
70 | private static final String TYPE_VARCHAR = "VARCHAR";
71 |
72 | static {
73 | typeToName = new HashMap();
74 | typeToName.put(Types.ARRAY, TYPE_ARRAY);
75 | typeToName.put(Types.BIGINT, TYPE_BIGINT);
76 | typeToName.put(Types.BINARY, TYPE_BINARY);
77 | typeToName.put(Types.BIT, TYPE_BIT);
78 | typeToName.put(Types.BLOB, TYPE_BLOB);
79 | typeToName.put(Types.BOOLEAN, TYPE_BOOLEAN);
80 | typeToName.put(Types.CHAR, TYPE_CHAR);
81 | typeToName.put(Types.CLOB, TYPE_CLOB);
82 | typeToName.put(Types.DATALINK, TYPE_DATALINK);
83 | typeToName.put(Types.DATE, TYPE_DATE);
84 | typeToName.put(Types.DECIMAL, TYPE_DECIMAL);
85 | typeToName.put(Types.DISTINCT, TYPE_DISTINCT);
86 | typeToName.put(Types.DOUBLE, TYPE_DOUBLE);
87 | typeToName.put(Types.FLOAT, TYPE_FLOAT);
88 | typeToName.put(Types.INTEGER, TYPE_INTEGER);
89 | typeToName.put(Types.JAVA_OBJECT, TYPE_JAVA_OBJECT);
90 | typeToName.put(Types.LONGVARBINARY, TYPE_LONGVARBINARY);
91 | typeToName.put(Types.LONGVARCHAR, TYPE_LONGVARCHAR);
92 | typeToName.put(Jdbc4Types.NCHAR, TYPE_NCHAR);
93 | typeToName.put(Jdbc4Types.NCLOB, TYPE_NCLOB);
94 | typeToName.put(Jdbc4Types.NVARCHAR, TYPE_NVARCHAR);
95 | typeToName.put(Jdbc4Types.LONGNVARCHAR, TYPE_LONGNVARCHAR);
96 | typeToName.put(Types.NULL, TYPE_NULL);
97 | typeToName.put(Types.NUMERIC, TYPE_NUMERIC);
98 | typeToName.put(Types.OTHER, TYPE_OTHER);
99 | typeToName.put(Types.REAL, TYPE_REAL);
100 | typeToName.put(Types.REF, TYPE_REF);
101 | typeToName.put(Types.SMALLINT, TYPE_SMALLINT);
102 | typeToName.put(Types.STRUCT, TYPE_STRUCT);
103 | typeToName.put(Types.TIME, TYPE_TIME);
104 | typeToName.put(Types.TIMESTAMP, TYPE_TIMESTAMP);
105 | typeToName.put(Types.TINYINT, TYPE_TINYINT);
106 | typeToName.put(Types.VARBINARY, TYPE_VARBINARY);
107 | typeToName.put(Types.VARCHAR, TYPE_VARCHAR);
108 |
109 | nameToType = new HashMap();
110 | nameToType.put(TYPE_ARRAY, Types.ARRAY);
111 | nameToType.put(TYPE_BIGINT, Types.BIGINT);
112 | nameToType.put(TYPE_BINARY, Types.BINARY);
113 | nameToType.put(TYPE_BIT, Types.BIT);
114 | nameToType.put(TYPE_BLOB, Types.BLOB);
115 | nameToType.put(TYPE_BOOLEAN, Types.BOOLEAN);
116 | nameToType.put(TYPE_CHAR, Types.CHAR);
117 | nameToType.put(TYPE_CLOB, Types.CLOB);
118 | nameToType.put(TYPE_DATALINK, Types.DATALINK);
119 | nameToType.put(TYPE_DATE, Types.DATE);
120 | nameToType.put(TYPE_DECIMAL, Types.DECIMAL);
121 | nameToType.put(TYPE_DISTINCT, Types.DISTINCT);
122 | nameToType.put(TYPE_DOUBLE, Types.DOUBLE);
123 | nameToType.put(TYPE_FLOAT, Types.FLOAT);
124 | nameToType.put(TYPE_INTEGER, Types.INTEGER);
125 | nameToType.put(TYPE_JAVA_OBJECT, Types.JAVA_OBJECT);
126 | nameToType.put(TYPE_LONGVARBINARY, Types.LONGVARBINARY);
127 | nameToType.put(TYPE_LONGVARCHAR, Types.LONGVARCHAR);
128 | nameToType.put(TYPE_NCHAR, Jdbc4Types.NCHAR);
129 | nameToType.put(TYPE_NCLOB, Jdbc4Types.NCLOB);
130 | nameToType.put(TYPE_NVARCHAR, Jdbc4Types.NVARCHAR);
131 | nameToType.put(TYPE_LONGNVARCHAR, Jdbc4Types.LONGNVARCHAR);
132 | nameToType.put(TYPE_NULL, Types.NULL);
133 | nameToType.put(TYPE_NUMERIC, Types.NUMERIC);
134 | nameToType.put(TYPE_OTHER, Types.OTHER);
135 | nameToType.put(TYPE_REAL, Types.REAL);
136 | nameToType.put(TYPE_REF, Types.REF);
137 | nameToType.put(TYPE_SMALLINT, Types.SMALLINT);
138 | nameToType.put(TYPE_STRUCT, Types.STRUCT);
139 | nameToType.put(TYPE_TIME, Types.TIME);
140 | nameToType.put(TYPE_TIMESTAMP, Types.TIMESTAMP);
141 | nameToType.put(TYPE_TINYINT, Types.TINYINT);
142 | nameToType.put(TYPE_VARBINARY, Types.VARBINARY);
143 | nameToType.put(TYPE_VARCHAR, Types.VARCHAR);
144 | }
145 |
146 | private JdbcTypeResolver() {
147 | super();
148 | }
149 |
150 | public static String getJdbcTypeName(int jdbcType) {
151 | String answer = typeToName.get(jdbcType);
152 | if (answer == null) {
153 | answer = TYPE_OTHER;
154 | }
155 |
156 | return answer;
157 | }
158 |
159 | public static int getJdbcType(String jdbcTypeName) {
160 | Integer answer = nameToType.get(jdbcTypeName);
161 | if (answer == null) {
162 | answer = Types.OTHER;
163 | }
164 |
165 | return answer;
166 | }
167 | }
168 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/engine/AbstractEngineTemplate.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.engine;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
19 | import com.gitee.hengboy.builder.common.util.StringUtil;
20 | import com.gitee.hengboy.builder.core.configuration.BuilderConfiguration;
21 | import com.gitee.hengboy.builder.core.configuration.TemplateConfiguration;
22 | import com.gitee.hengboy.builder.core.database.DataBase;
23 | import org.slf4j.Logger;
24 | import org.slf4j.LoggerFactory;
25 |
26 | import java.io.File;
27 | import java.util.List;
28 |
29 | /**
30 | * 驱动抽象类
31 | *
32 | * @author:于起宇 ===============================
33 | * Created with IDEA.
34 | * Date:2018/7/12
35 | * Time:2:22 PM
36 | * 简书:http://www.jianshu.com/u/092df3f77bca
37 | * ================================
38 | */
39 | public abstract class AbstractEngineTemplate implements EngineTemplate {
40 | /**
41 | * logger instance
42 | */
43 | static Logger logger = LoggerFactory.getLogger(AbstractEngineTemplate.class);
44 |
45 | /**
46 | * 数据库对象实例
47 | */
48 | protected DataBase dataBase;
49 | /**
50 | * 自动生成参数实体实例
51 | */
52 | protected CodeBuilderProperties codeBuilderProperties;
53 | /**
54 | * 系统分隔符
55 | */
56 | protected static final String SEPARATOR = File.separator;
57 | /**
58 | * 自动生成文件的后缀名
59 | */
60 | protected static final String FILE_SUFFIX = ".java";
61 | /**
62 | * package 分隔符
63 | */
64 | protected static final String PACKAGE_SPLIT = ".";
65 |
66 | /**
67 | * 构造函数初始化数据库对象实例、代码生成配置实体
68 | *
69 | * @param dataBase 数据库对象实例
70 | * @param codeBuilderProperties 代码生成配置实体
71 | */
72 | public AbstractEngineTemplate(DataBase dataBase, CodeBuilderProperties codeBuilderProperties) {
73 | this.dataBase = dataBase;
74 | this.codeBuilderProperties = codeBuilderProperties;
75 | }
76 |
77 | /**
78 | * 执行输出日志
79 | * 打印本次执行的基本信息
80 | *
81 | * @param tablesSize 本次生成表格的数量
82 | */
83 | public void invokeConsoleLog(int tablesSize) {
84 | logger.info("Code-Builder >>> 本次有{}个表参与生成.", tablesSize);
85 | logger.info("Code-Builder >>> 执行项目目录:{}", getProjectDir());
86 | logger.info("Code-Builder >>> 生成目录:{}", getBasePackageTargetDir());
87 | logger.info("Code-Builder >>> Builder根目录:{}", getBaseBuilderDir());
88 | }
89 |
90 | /**
91 | * 循环生成文件
92 | *
93 | * @param tableNames 数据表列表
94 | */
95 | public void loopProcess(List tableNames) {
96 | for (String tableName : tableNames) {
97 | logger.info("Auto Builder Table > 【{}】", tableName);
98 | process(dataBase.getTable(tableName));
99 | }
100 | // 执行生成日志输出
101 | invokeConsoleLog(tableNames.size());
102 | }
103 |
104 | /**
105 | * 获取自动化配置对象实例
106 | * 从builder.yml配置文件内自动映射为配置类实例
107 | *
108 | * @return 配置对象实例
109 | */
110 | protected BuilderConfiguration getConfiguration() {
111 | return codeBuilderProperties.getBuilder();
112 | }
113 |
114 | /**
115 | * 获取项目目录
116 | *
117 | * @return 项目根路径
118 | */
119 | protected String getProjectDir() {
120 | return codeBuilderProperties.getProjectBaseDir() + SEPARATOR;
121 | }
122 |
123 | /**
124 | * 获取生成目标目录地址
125 | *
126 | * @return 目标根路径
127 | */
128 | protected String getBaseTargetDir() {
129 | StringBuffer baseTargetDir = new StringBuffer();
130 | // 项目根地址
131 | baseTargetDir.append(getProjectDir());
132 | // 生成目标目录
133 | baseTargetDir.append(codeBuilderProperties.getTargetDir());
134 | baseTargetDir.append(SEPARATOR);
135 | return baseTargetDir.toString();
136 | }
137 |
138 | /**
139 | * 获取builder基础目录地址
140 | *
141 | * @return builder根路径
142 | */
143 | protected String getBaseBuilderDir() {
144 | StringBuffer builderDir = new StringBuffer();
145 | // 项目根地址
146 | builderDir.append(codeBuilderProperties.getProjectBaseDir());
147 | // 系统分隔符
148 | builderDir.append(SEPARATOR);
149 |
150 | // builder根地址
151 | builderDir.append(codeBuilderProperties.getBuilderDir());
152 | // 系统分隔符
153 | builderDir.append(SEPARATOR);
154 | return builderDir.toString();
155 | }
156 |
157 | /**
158 | * 获取模板创建文件后的包名
159 | *
160 | * @param templateConfiguration 模板配置对象
161 | * @return 包名
162 | */
163 | protected String getTemplatePackageName(TemplateConfiguration templateConfiguration) {
164 | // 包名前缀
165 | StringBuffer packageName = new StringBuffer(codeBuilderProperties.getBuilder().getPackagePrefix());
166 | // 默认包名
167 | if (StringUtil.isNotEmpty(templateConfiguration.getPackageName())) {
168 | packageName.append(PACKAGE_SPLIT);
169 | // 转换为小写
170 | packageName.append(templateConfiguration.getPackageName().toLowerCase());
171 | }
172 | return packageName.toString();
173 | }
174 |
175 | /**
176 | * 获取生成目标目录 + package目录格式化后的根地址
177 | *
178 | * @return 存在包名的目录根路径
179 | */
180 | protected String getBasePackageTargetDir() {
181 | // 获取builder配置信息
182 | BuilderConfiguration builderConfiguration = codeBuilderProperties.getBuilder();
183 | StringBuffer basePackageDir = new StringBuffer();
184 | // 生成文件的目标根路径
185 | basePackageDir.append(getBaseTargetDir());
186 |
187 | // 是否存在自定义的package前缀
188 | // 存在前缀添加到路径内
189 | if (StringUtil.isNotEmpty(builderConfiguration.getDiffSysPackagePrefix())) {
190 | basePackageDir.append(builderConfiguration.getDiffSysPackagePrefix());
191 | }
192 | // 系统分隔符
193 | basePackageDir.append(SEPARATOR);
194 | return basePackageDir.toString();
195 | }
196 |
197 | /**
198 | * 获取模板生成文件后的类名
199 | *
200 | * @param templateConfiguration 默认配置信息
201 | * @param entityName 数据表对应的实体名称
202 | * @return Class 名称
203 | */
204 | protected String getTemplateClassName(TemplateConfiguration templateConfiguration, String entityName) {
205 | StringBuffer className = new StringBuffer();
206 | // 追加文件前缀名
207 | if (StringUtil.isNotEmpty(templateConfiguration.getFilePrefix())) {
208 | className.append(StringUtil.getCamelCaseString(templateConfiguration.getFilePrefix(), true));
209 | }
210 | // 实体类名称
211 | className.append(entityName);
212 | // 追加文件后缀名
213 | if (StringUtil.isNotEmpty(templateConfiguration.getFileSuffix())) {
214 | className.append(StringUtil.getCamelCaseString(templateConfiguration.getFileSuffix(), true));
215 | }
216 | return className.toString();
217 | }
218 |
219 | /**
220 | * 获取新文件的全名称
221 | * 如:XxxEntity.java
222 | *
223 | * @param templateConfiguration 模板配置对象
224 | * @param entityName 数据表对应的实体名称
225 | * @return 新文件名称
226 | */
227 | protected String getTemplateNewFileName(TemplateConfiguration templateConfiguration, String entityName) {
228 | StringBuffer fileName = new StringBuffer();
229 | // 目标package的根目录
230 | fileName.append(getBasePackageTargetDir());
231 |
232 | // 是否配置了模板创建文件后所属的package目录
233 | if (StringUtil.isNotEmpty(templateConfiguration.getPackageName())) {
234 | fileName.append(templateConfiguration.getPackageName());
235 | fileName.append(SEPARATOR);
236 | }
237 | // 类名
238 | fileName.append(getTemplateClassName(templateConfiguration, entityName));
239 | // 文件扩展名
240 | fileName.append(FILE_SUFFIX);
241 | return fileName.toString();
242 | }
243 |
244 | /**
245 | * 循环创建package
246 | * 根据builder.yml配置文件内的参数packagePrefix进行自动创建package包名的文件夹
247 | * 如:com.code.builder
248 | * 则自动创建com/code/builder文件夹
249 | *
250 | * @param templateConfiguration 模板配置
251 | */
252 | protected void loopCreatePackage(TemplateConfiguration templateConfiguration) {
253 | // 目录地址
254 | StringBuffer basePackagePath = new StringBuffer();
255 | // 目标根地址
256 | basePackagePath.append(getBasePackageTargetDir());
257 | // 模板生成文件目标独有的package
258 | if (StringUtil.isNotEmpty(templateConfiguration.getPackageName())) {
259 | basePackagePath.append(templateConfiguration.getPackageName());
260 | }
261 | // 执行创建目录
262 | File basePackage = new File(basePackagePath.toString());
263 | basePackage.mkdirs();
264 | }
265 | }
266 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/engine/EngineTemplate.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.engine;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | import com.gitee.hengboy.builder.core.database.model.Table;
18 |
19 | import java.util.List;
20 |
21 | /**
22 | * 驱动模板接口
23 | * 目前仅实现freemarker模板
24 | *
25 | * @author:于起宇
26 | * ===============================
27 | * Created with IDEA.
28 | * Date:2018/7/8
29 | * Time:5:15 PM
30 | * 简书:http://www.jianshu.com/u/092df3f77bca
31 | * ================================
32 | */
33 | public interface EngineTemplate {
34 | /**
35 | * 单个数据表生成文件
36 | *
37 | * @param table 数据表对象
38 | */
39 | void process(Table table);
40 |
41 | /**
42 | * 循环执行生成文件
43 | *
44 | * @param tableNames 表名列表
45 | */
46 | void loopProcess(List tableNames);
47 | }
48 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/engine/EngineTemplateFactory.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.engine;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
19 | import com.gitee.hengboy.builder.common.enums.EngineTypeEnum;
20 | import com.gitee.hengboy.builder.common.enums.ErrorEnum;
21 | import com.gitee.hengboy.builder.common.exception.CodeBuilderException;
22 | import com.gitee.hengboy.builder.core.database.DataBase;
23 |
24 | import java.lang.reflect.Constructor;
25 |
26 | /**
27 | * 驱动模板动态工厂
28 | *
29 | * @author:于起宇 ===============================
30 | * Created with IDEA.
31 | * Date:2018/7/11
32 | * Time:5:55 PM
33 | * 简书:http://www.jianshu.com/u/092df3f77bca
34 | * ================================
35 | */
36 | public class EngineTemplateFactory {
37 | private EngineTemplateFactory() {
38 | }
39 |
40 | /**
41 | * 根据驱动枚举动态初始化获取驱动实现类实例
42 | *
43 | * @param engineTypeEnum 驱动枚举
44 | * @param codeBuilderProperties 配置参数实体
45 | * @param dataBase 数据库连接实例
46 | * @return 驱动模板实例
47 | */
48 | public static EngineTemplate newInstance(EngineTypeEnum engineTypeEnum, DataBase dataBase, CodeBuilderProperties codeBuilderProperties) {
49 | try {
50 | // 获取数据库实现类的构造函数
51 | Constructor constructor = engineTypeEnum.getImplClass().getConstructor(DataBase.class, CodeBuilderProperties.class);
52 | // 反射获取数据库实现类实例
53 | return (EngineTemplate) constructor.newInstance(dataBase, codeBuilderProperties);
54 | } catch (Exception e) {
55 | e.printStackTrace();
56 | }
57 | throw new CodeBuilderException(ErrorEnum.NOT_ALLOW_ENGINE);
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/engine/impl/FreemarkerEngineImpl.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.engine.impl;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
19 | import com.gitee.hengboy.builder.common.enums.EngineTypeEnum;
20 | import com.gitee.hengboy.builder.core.configuration.BuilderConfiguration;
21 | import com.gitee.hengboy.builder.core.configuration.TemplateConfiguration;
22 | import com.gitee.hengboy.builder.core.database.DataBase;
23 | import com.gitee.hengboy.builder.core.database.model.Table;
24 | import com.gitee.hengboy.builder.core.engine.AbstractEngineTemplate;
25 | import com.gitee.hengboy.builder.core.engine.model.DataModelEntity;
26 | import freemarker.template.Configuration;
27 | import freemarker.template.Template;
28 |
29 | import java.io.*;
30 | import java.util.Locale;
31 |
32 | /**
33 | * 使用freemarker模板驱动实现类
34 | *
35 | * @author:于起宇 ===============================
36 | * Created with IDEA.
37 | * Date:2018/7/8
38 | * Time:5:20 PM
39 | * 简书:http://www.jianshu.com/u/092df3f77bca
40 | * ================================
41 | */
42 | public class FreemarkerEngineImpl extends AbstractEngineTemplate {
43 | /**
44 | * freemarker配置对象实例化
45 | * 采用2.3.28版本
46 | */
47 | private Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
48 |
49 | /**
50 | * 默认字符集
51 | */
52 | static final String DEFAULT_ENCODING = "UTF-8";
53 | /**
54 | * 默认国际化
55 | */
56 | static final String DEFAULT_LOCALE = "zh_CN";
57 |
58 | public FreemarkerEngineImpl(DataBase dataBase, CodeBuilderProperties codeBuilderProperties) {
59 | super(dataBase, codeBuilderProperties);
60 | }
61 |
62 | /**
63 | * 设置freemarker全局配置参数
64 | */
65 | {
66 | try {
67 | configuration.setDirectoryForTemplateLoading(new File(getBaseBuilderDir() + EngineTypeEnum.FREEMARKER.getTemplateDirName()));
68 | configuration.setDefaultEncoding(DEFAULT_ENCODING);
69 | configuration.setLocale(new Locale(DEFAULT_LOCALE));
70 | } catch (Exception e) {
71 | e.printStackTrace();
72 | }
73 | }
74 |
75 | /**
76 | * 构建指定表的实体
77 | *
78 | * @param table 数据表对象
79 | */
80 | public void process(Table table) {
81 | try {
82 | // 获取配置
83 | BuilderConfiguration builderConfiguration = getConfiguration();
84 | // 遍历生成文件
85 | for (TemplateConfiguration templateConfiguration : builderConfiguration.getTemplates()) {
86 | // 创建package
87 | loopCreatePackage(templateConfiguration);
88 | // freemarker模板
89 | Template template = configuration.getTemplate(templateConfiguration.getName());
90 | // 创建文件
91 | File file = new File(getTemplateNewFileName(templateConfiguration, table.getEntityName()));
92 | // 写入freemarker模板内容
93 | Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), DEFAULT_ENCODING));
94 |
95 | /*
96 | * 构建数据模型实体
97 | * 1. 设置当前模板创建类的包名
98 | * 2. 设置当前模板创建类的类名
99 | * 3. 设置数据表格对象
100 | */
101 | DataModelEntity dataModelEntity = DataModelEntity.builder()
102 | .packageName(getTemplatePackageName(templateConfiguration))
103 | .className(getTemplateClassName(templateConfiguration, table.getEntityName()))
104 | .table(table)
105 | .build();
106 |
107 | // 执行生成
108 | template.process(dataModelEntity, out);
109 | }
110 |
111 | } catch (Exception e) {
112 | e.printStackTrace();
113 | }
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/engine/model/DataModelEntity.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.engine.model;
2 |
3 | import com.gitee.hengboy.builder.core.database.model.Table;
4 | import lombok.Builder;
5 | import lombok.Data;
6 |
7 | /**
8 | * 模板结果实体
9 | * 没一个模板都会有该实体的对象实例传递
10 | * 比如:freemarker在process时传递该实体的实例到freemarker模板内
11 | *
12 | * @author:于起宇 ===============================
13 | * Created with IDEA.
14 | * Date:2018/7/17
15 | * Time:10:29 AM
16 | * 简书:http://www.jianshu.com/u/092df3f77bca
17 | * ================================
18 | */
19 | @Data
20 | @Builder
21 | public class DataModelEntity {
22 | /**
23 | * 表格实例
24 | */
25 | private Table table;
26 | /**
27 | * 类名
28 | * 如:UserInfoEntity
29 | */
30 | private String className;
31 | /**
32 | * 包名
33 | * 如:com.xxx.xxx.user
34 | */
35 | private String packageName;
36 | }
37 |
--------------------------------------------------------------------------------
/code-builder-core/src/main/java/com/gitee/hengboy/builder/core/invoke/CodeBuilderInvoke.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.core.invoke;
2 |
3 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
4 | import com.gitee.hengboy.builder.common.enums.ErrorEnum;
5 | import com.gitee.hengboy.builder.common.exception.CodeBuilderException;
6 | import com.gitee.hengboy.builder.common.util.StringUtil;
7 | import com.gitee.hengboy.builder.core.database.DataBase;
8 | import com.gitee.hengboy.builder.core.database.DataBaseFactory;
9 | import com.gitee.hengboy.builder.core.engine.EngineTemplate;
10 | import com.gitee.hengboy.builder.core.engine.EngineTemplateFactory;
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 |
14 | import java.util.List;
15 |
16 | /**
17 | * 自动生成执行类型
18 | * @author:于起宇
19 | * ===============================
20 | * Created with IDEA.
21 | * Date:2018/7/19
22 | * Time:1:59 PM
23 | * 简书:http://www.jianshu.com/u/092df3f77bca
24 | * ================================
25 | */
26 | public class CodeBuilderInvoke {
27 |
28 | private CodeBuilderInvoke() { }
29 |
30 | /**
31 | * logger instance
32 | */
33 | static Logger logger = LoggerFactory.getLogger(CodeBuilderInvoke.class);
34 |
35 | /**
36 | * 执行构造入口方法
37 | * 该方法用于maven-plugin、starter两个地方
38 | *
39 | * @param codeBuilderProperties 自动生成参数实体
40 | */
41 | public static void invoke(CodeBuilderProperties codeBuilderProperties) {
42 | if (!codeBuilderProperties.isExecute()) {
43 | logger.info("未开启自动代码生成,如需生成实体类请配置【execute=true】");
44 | return;
45 | }
46 |
47 | // 获取数据库对象
48 | DataBase dataBase = DataBaseFactory.newInstance(codeBuilderProperties);
49 | // 获取驱动模板
50 | EngineTemplate engineTemplate = EngineTemplateFactory.newInstance(codeBuilderProperties.getEngineTypeEnum(), dataBase, codeBuilderProperties);
51 | // 获取表名列表
52 | List tableNames = getTables(dataBase, codeBuilderProperties);
53 | // 执行循环自动生成文件
54 | engineTemplate.loopProcess(tableNames);
55 | // 关闭数据库连接
56 | dataBase.closeConnection();
57 | }
58 |
59 | /**
60 | * 获取需要自动生成的表列表
61 | *
62 | * @param dataBase 数据库对象实例
63 | * @return
64 | */
65 | private static List getTables(DataBase dataBase, CodeBuilderProperties codeBuilderProperties) {
66 | List tables = codeBuilderProperties.getTables();
67 | String generatorByPattern = codeBuilderProperties.getGeneratorByPattern();
68 | /*
69 | * 根据配置tables参数表名进行构建生成
70 | * 优先级高于generatorByPattern
71 | */
72 | if (null != tables && tables.size() > 0) {
73 | logger.info("Using table name to generate code automatically, please wait...");
74 | return tables;
75 | }
76 | /*
77 | * 如果配置generatorByPattern参数,优先级高于tables
78 | */
79 | else if (StringUtil.isNotEmpty(generatorByPattern)) {
80 | logger.info("Using expression method to generate code automatically, please wait...");
81 | return dataBase.getTableNames(generatorByPattern);
82 | }
83 | throw new CodeBuilderException(ErrorEnum.NO_BUILDER_TABLE);
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/code-builder-maven-plugin-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | code-builder
7 | com.gitee.hengboy
8 | 1.0.5.RELEASE
9 |
10 | 4.0.0
11 | Code-Builder Maven插件示例
12 | code-builder-maven-plugin-sample
13 |
14 | 集成code-builder-maven-plugin插件形式
15 |
16 |
17 |
18 |
19 |
20 |
21 | com.gitee.hengboy
22 | mybatis-enhance-spring-boot-starter
23 | 1.0.3.RELEASE
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 | com.gitee.hengboy
32 | code-builder-maven-plugin
33 | 1.0.5.RELEASE
34 |
35 |
36 |
37 | generator
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 | mysql
46 | mysql-connector-java
47 | 5.1.46
48 |
49 |
50 |
51 |
52 |
53 | true
54 |
55 |
56 |
57 |
58 |
59 |
60 | xxxx
61 | xxxxx
62 | xxxx
63 | jdbc:mysql://xxx.xxx.xx.xxx:3306
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | Sys
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 | com.code.builder.sample
88 |
89 |
90 |
91 |
92 |
93 |
94 | entity.ftl
95 |
96 | model
97 |
98 | entity
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 | org.apache.maven.plugins
109 | maven-deploy-plugin
110 | 2.8.2
111 |
112 | true
113 |
114 |
115 |
116 |
117 |
--------------------------------------------------------------------------------
/code-builder-maven-plugin-sample/src/main/resources/templates/builder/freemarker/entity.ftl:
--------------------------------------------------------------------------------
1 | <#if (packageName)??>
2 | package ${packageName};
3 | #if>
4 | <#if (table.hasSqlDate)>
5 | import java.sql.Date;
6 | #if>
7 | <#if (table.hasTimeStamp)>
8 | import java.sql.Timestamp;
9 | #if>
10 | <#if (table.hasBigDecimal)>
11 | import java.math.BigDecimal;
12 | #if>
13 | import com.gitee.hengboy.mybatis.enhance.common.annotation.Column;
14 | import com.gitee.hengboy.mybatis.enhance.common.annotation.Id;
15 | import com.gitee.hengboy.mybatis.enhance.common.annotation.Table;
16 | import com.gitee.hengboy.mybatis.enhance.common.enums.KeyGeneratorTypeEnum;
17 | import lombok.Data;
18 |
19 | /**
20 | * 本类由Code-Builder自动生成
21 | * 表名: ${table.tableName} - ${table.remark} - 数据实体
22 | *
23 | * @author Code-Builder
24 | * @since 恒宇少年
25 | * ===============================
26 | * Created with Code-Builder.
27 | * User:
28 | * Date:${.now}
29 | * 简书:http://www.jianshu.com/u/092df3f77bca
30 | * 码云:https://gitee.com/hengboy
31 | * GitHub:https://github.com/hengyuboy
32 | * ================================
33 | */
34 | @Data
35 | @Table(name = "${table.tableName}")
36 | public class ${className} {
37 | <#list table.primaryKeys as key>
38 | /**
39 | * ${key.columnName} - ${key.remark}
40 | */
41 | <#if (key.autoincrement)>
42 | @Id(generatorType = KeyGeneratorTypeEnum.AUTO)
43 | <#else>
44 | @Id(generatorType = KeyGeneratorTypeEnum.UUID)
45 | #if>
46 | @Column(name="${key.columnName}")
47 | private ${key.javaType} ${key.javaProperty};
48 | #list>
49 | <#list table.columns as column>
50 | <#if (!column.primaryKey)>
51 | /**
52 | * ${column.columnName} - ${column.remark}
53 | */
54 | @Column(name="${column.columnName}"<#if (column.defaultValue)??>,insertable = false#if>)
55 | private ${column.javaType} ${column.javaProperty};
56 | #if>
57 | #list>
58 | }
--------------------------------------------------------------------------------
/code-builder-maven-plugin/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | code-builder
7 | com.gitee.hengboy
8 | 1.0.5.RELEASE
9 |
10 | 4.0.0
11 | code-builder-maven-plugin
12 | maven-plugin
13 |
14 |
15 |
16 | com.gitee.hengboy
17 | code-builder-core
18 | ${code.builder.core.version}
19 |
20 |
21 |
22 | org.apache.maven
23 | maven-plugin-api
24 | ${maven.plugin.api.version}
25 |
26 |
27 |
28 | org.apache.maven.plugin-tools
29 | maven-plugin-annotations
30 | ${maven.plugin.annotation.version}
31 | provided
32 |
33 |
34 |
--------------------------------------------------------------------------------
/code-builder-maven-plugin/src/main/java/com/gitee/hengboy/builder/CodeBuilderMojo.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder;
2 | /**
3 | * Copyright 2018 恒宇少年
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
19 | import com.gitee.hengboy.builder.common.enums.DbTypeEnum;
20 | import com.gitee.hengboy.builder.common.enums.EngineTypeEnum;
21 | import com.gitee.hengboy.builder.core.configuration.BuilderConfiguration;
22 | import com.gitee.hengboy.builder.core.invoke.CodeBuilderInvoke;
23 | import org.apache.maven.plugin.AbstractMojo;
24 | import org.apache.maven.plugin.MojoExecutionException;
25 | import org.apache.maven.plugin.MojoFailureException;
26 | import org.apache.maven.plugins.annotations.Execute;
27 | import org.apache.maven.plugins.annotations.LifecyclePhase;
28 | import org.apache.maven.plugins.annotations.Mojo;
29 | import org.apache.maven.plugins.annotations.Parameter;
30 |
31 | import java.util.List;
32 |
33 | /**
34 | * 代码生成器Maven插件入口类
35 | *
简书:http://www.jianshu.com/u/092df3f77bca
36 | * 码云:https://gitee.com/hengboy
37 | *
38 | * @author 恒宇少年
39 | */
40 | @Mojo(name = "generator", defaultPhase = LifecyclePhase.COMPILE)
41 | @Execute(phase = LifecyclePhase.COMPILE)
42 | public class CodeBuilderMojo
43 | extends AbstractMojo {
44 | /**
45 | * 是否执行自动生成
46 | * 默认值:不执行
47 | */
48 | @Parameter(defaultValue = "false", required = true)
49 | private boolean execute;
50 | /**
51 | * 数据库类型
52 | * 自动根据类型加载对应的驱动
53 | * 如:MySQL => com.mysql.jdbc.Driver
54 | */
55 | @Parameter(defaultValue = "MySQL")
56 | private DbTypeEnum dbType;
57 | /**
58 | * 数据库名称
59 | */
60 | @Parameter(required = true)
61 | private String dbName;
62 | /**
63 | * 数据库连接地址
64 | * 排除数据库名称
65 | * 如:jdbc:mysql://xxx.xx.xx.xxx:3306
66 | */
67 | @Parameter(required = true)
68 | private String dbUrl;
69 | /**
70 | * 数据库连接用户名
71 | */
72 | @Parameter(required = true)
73 | private String dbUserName;
74 | /**
75 | * 数据库连接密码
76 | */
77 | @Parameter
78 | private String dbPassword;
79 | /**
80 | * 数据库驱动类全局限定名
81 | */
82 | @Parameter
83 | private String dbDriverClassName;
84 | /**
85 | * 忽略表名的前缀
86 | * 如建表时表名为:sys_menu_info
87 | * 配置忽略前缀为:sys_
88 | * 生成对应的Class后为:MenuInfo
89 | */
90 | @Parameter
91 | private String ignoreClassPrefix;
92 |
93 | /**
94 | * 指定生成的表列表
95 | * 根据指定的表名进行生成
96 | * 如:
97 | *
98 | *
99 | *
100 | *
101 | * 会自动生成sys_menu_info、sys_role_info两张表对应的模板文件
102 | */
103 | @Parameter
104 | private List tables;
105 | /**
106 | * 根据指定前缀生成
107 | * 如:sys_
108 | * 会匹配:sys_menu_info、sys_role_info、sys_button_info等表
109 | */
110 | @Parameter
111 | private String generatorByPattern;
112 | /**
113 | * 驱动模板类型
114 | * 默认值:freemarker
115 | */
116 | @Parameter(defaultValue = "FREEMARKER")
117 | private EngineTypeEnum engineType;
118 |
119 | /**
120 | * 项目根地址
121 | */
122 | @Parameter(defaultValue = "${basedir}")
123 | private String projectBaseDir;
124 |
125 | /**
126 | * builder配置文件目录地址
127 | * 这里配置分隔符是"."
128 | * 因为linux、osx、windows下的分隔符不一样
129 | * 需要根据java获取系统的分隔符后格式化
130 | */
131 | @Parameter(defaultValue = "target.classes.templates.builder")
132 | private String builderDir;
133 |
134 | /**
135 | * 文件生成后目标根地址
136 | */
137 | @Parameter(defaultValue = "target.generated-sources.java")
138 | private String targetDir;
139 | /**
140 | * 自动生成配置信息实体
141 | */
142 | @Parameter
143 | private BuilderConfiguration builder;
144 |
145 | /**
146 | * 执行插件入口方法
147 | * - 构造自动构建参数对象
148 | * - 获取数据库对象
149 | * - 获取驱动模型
150 | * - 执行驱动生成文件
151 | *
152 | * @throws MojoExecutionException mojo执行异常
153 | * @throws MojoFailureException mojo错误异常
154 | */
155 | public void execute() throws MojoExecutionException, MojoFailureException {
156 | try {
157 | /*
158 | * 组装代码生成器所需的实体参数
159 | * 传递到所需配置类
160 | */
161 | CodeBuilderProperties codeBuilderProperties = CodeBuilderProperties.builder()
162 | .execute(execute)
163 | .dbType(dbType)
164 | .dbName(dbName)
165 | .dbUserName(dbUserName)
166 | .dbPassword(dbPassword)
167 | .dbUrl(dbUrl)
168 | .dbDriverClassName(dbDriverClassName)
169 | .tables(tables)
170 | .generatorByPattern(generatorByPattern)
171 | .ignoreClassPrefix(ignoreClassPrefix)
172 | .projectBaseDir(projectBaseDir)
173 | .builderDir(builderDir)
174 | .targetDir(targetDir)
175 | .builder(builder)
176 | .engineTypeEnum(engineType)
177 | .build();
178 |
179 | // 执行代码生成
180 | CodeBuilderInvoke.invoke(codeBuilderProperties);
181 |
182 | } catch (Exception e) {
183 | getLog().error("Invoke have errors :" + e.getMessage());
184 | }
185 |
186 | }
187 | }
188 |
--------------------------------------------------------------------------------
/code-builder-spring-boot-autoconfigure/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | code-builder
7 | com.gitee.hengboy
8 | 1.0.5.RELEASE
9 |
10 | 4.0.0
11 | jar
12 | code-builder-spring-boot-autoconfigure
13 |
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-autoconfigure
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-configuration-processor
23 | true
24 |
25 |
26 |
27 | com.gitee.hengboy
28 | code-builder-core
29 | ${code.builder.core.version}
30 |
31 |
32 | com.alibaba
33 | fastjson
34 | 1.2.47
35 |
36 |
37 |
--------------------------------------------------------------------------------
/code-builder-spring-boot-autoconfigure/src/main/java/com/gitee/hengboy/builder/spring/boot/autoconfigure/BuilderAutoConfigureProperties.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.spring.boot.autoconfigure;
2 |
3 | import com.gitee.hengboy.builder.common.enums.DbTypeEnum;
4 | import com.gitee.hengboy.builder.common.enums.EngineTypeEnum;
5 | import com.gitee.hengboy.builder.core.configuration.BuilderConfiguration;
6 | import lombok.Data;
7 | import org.springframework.boot.context.properties.ConfigurationProperties;
8 |
9 | import java.util.List;
10 |
11 | import static com.gitee.hengboy.builder.spring.boot.autoconfigure.BuilderAutoConfigureProperties.CODE_BUILDER_PREFIX;
12 |
13 | /**
14 | * 代码生成自动配置文件
15 | *
16 | * @author:于起宇 ===============================
17 | * Created with IDEA.
18 | * Date:2018/7/19
19 | * Time:11:40 AM
20 | * 简书:http://www.jianshu.com/u/092df3f77bca
21 | * ================================
22 | */
23 | @Data
24 | @ConfigurationProperties(prefix = CODE_BUILDER_PREFIX)
25 | public class BuilderAutoConfigureProperties {
26 |
27 | /**
28 | * Properties Prefix
29 | */
30 | public static final String CODE_BUILDER_PREFIX = "hengboy.code.builder";
31 |
32 | private boolean execute;
33 | private String ignoreClassPrefix;
34 | private List tables;
35 | private String generatorByPattern;
36 | private DbTypeEnum dbType = DbTypeEnum.MySQL;
37 | private EngineTypeEnum engineTypeEnum = EngineTypeEnum.FREEMARKER;
38 | private String projectBaseDir;
39 | private String builderDir = "target.classes.templates.builder";
40 | private String targetDir = "target.generated-sources.java";
41 | private BuilderConfiguration configuration;
42 | }
43 |
--------------------------------------------------------------------------------
/code-builder-spring-boot-autoconfigure/src/main/java/com/gitee/hengboy/builder/spring/boot/autoconfigure/CodeBuilderAutoConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.gitee.hengboy.builder.spring.boot.autoconfigure;
2 |
3 | import com.gitee.hengboy.builder.common.CodeBuilderProperties;
4 | import com.gitee.hengboy.builder.core.configuration.BuilderConfiguration;
5 | import com.gitee.hengboy.builder.core.configuration.TemplateConfiguration;
6 | import com.gitee.hengboy.builder.core.invoke.CodeBuilderInvoke;
7 | import org.slf4j.Logger;
8 | import org.slf4j.LoggerFactory;
9 | import org.springframework.beans.factory.ObjectProvider;
10 | import org.springframework.boot.autoconfigure.AutoConfigureAfter;
11 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
12 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
13 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
14 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
15 | import org.springframework.context.annotation.Configuration;
16 | import org.springframework.util.ResourceUtils;
17 |
18 | import javax.sql.DataSource;
19 |
20 | import static org.springframework.util.ResourceUtils.CLASSPATH_URL_PREFIX;
21 |
22 | /**
23 | * 代码生成器自动化配置
24 | *
25 | * @author:于起宇 ===============================
26 | * Created with IDEA.
27 | * Date:2018/7/19
28 | * Time:11:37 AM
29 | * 简书:http://www.jianshu.com/u/092df3f77bca
30 | * ================================
31 | */
32 | @Configuration
33 | @ConditionalOnClass({BuilderConfiguration.class, TemplateConfiguration.class})
34 | @ConditionalOnBean(DataSource.class)
35 | @EnableConfigurationProperties(BuilderAutoConfigureProperties.class)
36 | @AutoConfigureAfter(DataSourceAutoConfiguration.class)
37 | public class CodeBuilderAutoConfiguration {
38 | /**
39 | * logger instance
40 | */
41 | static Logger logger = LoggerFactory.getLogger(CodeBuilderAutoConfiguration.class);
42 |
43 | private BuilderAutoConfigureProperties builderAutoConfigureProperties;
44 | private DataSource dataSource;
45 |
46 | /**
47 | * 初始化BuilderAutoConfigureProperties配置类
48 | *
49 | * @param builderAutoConfigureProperties 代码生成器配置类实例
50 | * @param dataSource 数据源
51 | */
52 | public CodeBuilderAutoConfiguration(BuilderAutoConfigureProperties builderAutoConfigureProperties,
53 | ObjectProvider dataSource) {
54 | this.builderAutoConfigureProperties = builderAutoConfigureProperties;
55 | this.dataSource = dataSource.getIfAvailable();
56 | invokeBuilder();
57 | }
58 |
59 | /**
60 | * Invoke Code Builder
61 | */
62 | void invokeBuilder() {
63 | try {
64 | CodeBuilderInvoke.invoke(
65 | CodeBuilderProperties.builder()
66 | .execute(builderAutoConfigureProperties.isExecute())
67 | .dataSource(dataSource)
68 | .dbType(builderAutoConfigureProperties.getDbType())
69 | .tables(builderAutoConfigureProperties.getTables())
70 | .generatorByPattern(builderAutoConfigureProperties.getGeneratorByPattern())
71 | .ignoreClassPrefix(builderAutoConfigureProperties.getIgnoreClassPrefix())
72 | // 项目根路径
73 | .projectBaseDir(ResourceUtils.getURL(CLASSPATH_URL_PREFIX).getPath().replace("/target/classes/", ""))
74 | .builderDir(builderAutoConfigureProperties.getBuilderDir())
75 | .targetDir(builderAutoConfigureProperties.getTargetDir())
76 | .builder(builderAutoConfigureProperties.getConfiguration())
77 | .engineTypeEnum(builderAutoConfigureProperties.getEngineTypeEnum())
78 | .build());
79 | } catch (Exception e) {
80 | logger.error("InvokeBuilder have error :{}", e.getMessage());
81 | }
82 |
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/code-builder-spring-boot-autoconfigure/src/main/resources/META-INF/spring-configuration-metadata.json:
--------------------------------------------------------------------------------
1 | {
2 | "hints": [],
3 | "groups": [
4 | {
5 | "sourceType": "com.gitee.hengboy.builder.spring.boot.autoconfigure.BuilderAutoConfigureProperties",
6 | "name": "hengboy.code.builder",
7 | "type": "com.gitee.hengboy.builder.spring.boot.autoconfigure.BuilderAutoConfigureProperties"
8 | },
9 | {
10 | "sourceType": "com.gitee.hengboy.builder.spring.boot.autoconfigure.BuilderAutoConfigureProperties",
11 | "name": "hengboy.code.builder.configuration",
12 | "sourceMethod": "getConfiguration()",
13 | "type": "com.gitee.hengboy.builder.core.configuration.BuilderConfiguration"
14 | },
15 | {
16 | "sourceType": "com.gitee.hengboy.builder.core.configuration.BuilderConfiguration",
17 | "name": "hengboy.code.builder.configuration.templates",
18 | "sourceMethod": "getTemplates()",
19 | "type": "com.gitee.hengboy.builder.core.configuration.TemplateConfiguration"
20 | }
21 | ],
22 | "properties": [
23 | {
24 | "sourceType": "com.gitee.hengboy.builder.spring.boot.autoconfigure.BuilderAutoConfigureProperties",
25 | "defaultValue": false,
26 | "name": "hengboy.code.builder.execute",
27 | "description": "是否执行自动生成,默认值:false(不执行)",
28 | "type": "java.lang.Boolean"
29 | },
30 | {
31 | "sourceType": "com.gitee.hengboy.builder.spring.boot.autoconfigure.BuilderAutoConfigureProperties",
32 | "name": "hengboy.code.builder.ignore-class-prefix",
33 | "description": "排除生成后实体类的前缀",
34 | "type": "java.lang.String"
35 | },
36 | {
37 | "sourceType": "com.gitee.hengboy.builder.spring.boot.autoconfigure.BuilderAutoConfigureProperties",
38 | "name": "hengboy.code.builder.generator-by-pattern",
39 | "description": "根据表达式匹配数据表进行生成",
40 | "type": "java.lang.String"
41 | },
42 | {
43 | "sourceType": "com.gitee.hengboy.builder.spring.boot.autoconfigure.BuilderAutoConfigureProperties",
44 | "name": "hengboy.code.builder.tables",
45 | "description": "根据指定表名称集合进行生成",
46 | "type": "java.util.List"
47 | },
48 | {
49 | "sourceType": "com.gitee.hengboy.builder.spring.boot.autoconfigure.BuilderAutoConfigureProperties",
50 | "name": "hengboy.code.builder.db-type",
51 | "defaultValue": "MySQL",
52 | "description": "数据库类型,默认MySQL",
53 | "type": "com.gitee.hengboy.builder.common.enums.DbTypeEnum"
54 | },
55 | {
56 | "sourceType": "com.gitee.hengboy.builder.spring.boot.autoconfigure.BuilderAutoConfigureProperties",
57 | "name": "hengboy.code.builder.engine-type-enum",
58 | "defaultValue": "FREEMARKER",
59 | "description": "驱动模板类型,默认Freemarker",
60 | "type": "com.gitee.hengboy.builder.common.enums.EngineTypeEnum"
61 | },
62 | {
63 | "sourceType": "com.gitee.hengboy.builder.spring.boot.autoconfigure.BuilderAutoConfigureProperties",
64 | "name": "hengboy.code.builder.builder-dir",
65 | "defaultValue":"target.classes.templates.builder",
66 | "description": "CodeBuilder根目录",
67 | "type": "java.lang.String"
68 | },
69 | {
70 | "sourceType": "com.gitee.hengboy.builder.spring.boot.autoconfigure.BuilderAutoConfigureProperties",
71 | "name": "hengboy.code.builder.target-dir",
72 | "defaultValue":"target.generated-sources.java",
73 | "description": "CodeBuilder生成后实体类的根路径",
74 | "type": "java.lang.String"
75 | },
76 | {
77 | "sourceType": "com.gitee.hengboy.builder.core.configuration.BuilderConfiguration",
78 | "name": "hengboy.code.builder.configuration.package-prefix",
79 | "description": "生成实体类的包名(package)前缀",
80 | "type": "java.lang.String"
81 | },
82 | {
83 | "name": "hengboy.code.builder.configuration.templates",
84 | "sourceType": "com.gitee.hengboy.builder.core.configuration.BuilderConfiguration",
85 | "description": "模板列表",
86 | "type": "java.util.List"
87 | }
88 | ]
89 | }
--------------------------------------------------------------------------------
/code-builder-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories:
--------------------------------------------------------------------------------
1 | #配置code-builder自动化配置
2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
3 | com.gitee.hengboy.builder.spring.boot.autoconfigure.CodeBuilderAutoConfiguration
--------------------------------------------------------------------------------
/code-builder-spring-boot-starter-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | code-builder
7 | com.gitee.hengboy
8 | 1.0.5.RELEASE
9 |
10 | 4.0.0
11 | Code-Builder SpringBoot Starter示例
12 | code-builder-spring-boot-starter-sample
13 |
14 | 集成code-builder-spring-boot-starter形式
15 |
16 |
17 |
18 |
19 |
20 |
21 | com.gitee.hengboy
22 | mybatis-enhance-spring-boot-starter
23 | 1.0.3.RELEASE
24 |
25 |
26 |
27 |
28 | com.gitee.hengboy
29 | code-builder-spring-boot-starter
30 | 1.0.5.RELEASE
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 | org.apache.maven.plugins
39 | maven-deploy-plugin
40 | 2.8.2
41 |
42 | true
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/code-builder-spring-boot-starter-sample/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | # spring boot starter 方式code-builder会使用项目的数据源作为读取数据库元数据的连接
3 | datasource:
4 | druid:
5 | url: jdbc:mysql://xxx.xxx.xxx.xx:3306/xxxx?characterEncoding=utf8
6 | driver-class-name: com.mysql.jdbc.Driver
7 | username: xxxxx
8 | password: xxxx
9 |
10 | # code-builder插件配置
11 | hengboy:
12 | code:
13 | builder:
14 | # true:执行生成,false:不执行生成
15 | execute: true
16 | configuration:
17 | # 生成实体类后的包名前缀
18 | package-prefix: com.code.builder.sample
19 | # 模板列表,可配置多个
20 | templates:
21 | -
22 | # entity.ftl对应resources/templates/builder/freemarker/entity.ftl
23 | name: entity.ftl
24 | # 生成后实体类的子包名
25 | packageName: model
26 | # 实体类后缀
27 | fileSuffix: Entity
28 | # 生成表达式,与like查询语法一致,优先级低于tables配置
29 | # generator-by-pattern: '%app_user_info%'
30 |
31 | # 数据库类型,默认mysql
32 | # db-type: mysql
33 |
34 | # 模板驱动类型,默认freemarker
35 | # engine-type-enum: freemarker
36 |
37 | # builder根目录,默认:classes.templates.builder
38 | # builder-dir: classes.templates.builder
39 |
40 | # 生成实体类目标位置,classes/generated-sources/java
41 | # target-dir: generated-sources.java
42 |
43 | # 指定生成的表名列表
44 | tables:
45 | - sys_menu_info
46 | - sys_role_info
47 |
48 | # 替换生成实体类后的前缀为空
49 | ignore-class-prefix: Sys
50 |
51 |
--------------------------------------------------------------------------------
/code-builder-spring-boot-starter-sample/src/main/resources/templates/builder/freemarker/entity.ftl:
--------------------------------------------------------------------------------
1 | <#if (packageName)??>
2 | package ${packageName};
3 | #if>
4 | <#if (table.hasSqlDate)>
5 | import java.sql.Date;
6 | #if>
7 | <#if (table.hasTimeStamp)>
8 | import java.sql.Timestamp;
9 | #if>
10 | <#if (table.hasBigDecimal)>
11 | import java.math.BigDecimal;
12 | #if>
13 | import com.gitee.hengboy.mybatis.enhance.common.annotation.Column;
14 | import com.gitee.hengboy.mybatis.enhance.common.annotation.Id;
15 | import com.gitee.hengboy.mybatis.enhance.common.annotation.Table;
16 | import com.gitee.hengboy.mybatis.enhance.common.enums.KeyGeneratorTypeEnum;
17 | import lombok.Data;
18 |
19 | /**
20 | * 本类由Code-Builder自动生成
21 | * 表名: ${table.tableName} - ${table.remark} - 数据实体
22 | *
23 | * @author Code-Builder
24 | * @since 恒宇少年
25 | * ===============================
26 | * Created with Code-Builder.
27 | * User:
28 | * Date:${.now}
29 | * 简书:http://www.jianshu.com/u/092df3f77bca
30 | * 码云:https://gitee.com/hengboy
31 | * GitHub:https://github.com/hengyuboy
32 | * ================================
33 | */
34 | @Data
35 | @Table(name = "${table.tableName}")
36 | public class ${className} {
37 | <#list table.primaryKeys as key>
38 | /**
39 | * ${key.columnName} - ${key.remark}
40 | */
41 | <#if (key.autoincrement)>
42 | @Id(generatorType = KeyGeneratorTypeEnum.AUTO)
43 | <#else>
44 | @Id(generatorType = KeyGeneratorTypeEnum.UUID)
45 | #if>
46 | @Column(name="${key.columnName}")
47 | private ${key.javaType} ${key.javaProperty};
48 | #list>
49 | <#list table.columns as column>
50 | <#if (!column.primaryKey)>
51 | /**
52 | * ${column.columnName} - ${column.remark}
53 | */
54 | @Column(name="${column.columnName}"<#if (column.defaultValue)??>,insertable = false#if>)
55 | private ${column.javaType} ${column.javaProperty};
56 | #if>
57 | #list>
58 | }
--------------------------------------------------------------------------------
/code-builder-spring-boot-starter/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | code-builder
7 | com.gitee.hengboy
8 | 1.0.5.RELEASE
9 |
10 | 4.0.0
11 | jar
12 | code-builder-spring-boot-starter
13 |
14 |
15 |
16 |
17 | com.gitee.hengboy
18 | code-builder-spring-boot-autoconfigure
19 | ${code.builder.autoconfigure.version}
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.gitee.hengboy
5 | code-builder
6 | pom
7 | 1.0.5.RELEASE
8 |
9 | code-builder-core
10 | code-builder-spring-boot-starter
11 | code-builder-spring-boot-autoconfigure
12 | code-builder-maven-plugin
13 | code-builder-maven-plugin-sample
14 | code-builder-spring-boot-starter-sample
15 |
16 | code-builder
17 | http://maven.apache.org
18 |
19 |
20 | 2.0
21 | 3.5.2
22 | 1.16.20
23 | 2.3.28
24 | 1.7.25
25 |
26 | 1.0.5.RELEASE
27 |
28 | 1.0.5.RELEASE
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-starter-parent
33 | 2.0.3.RELEASE
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 | org.projectlombok
42 | lombok
43 | ${lombok.version}
44 | provided
45 |
46 |
47 |
48 |
49 | org.freemarker
50 | freemarker
51 | ${freemarker.version}
52 |
53 |
54 |
55 |
56 | org.slf4j
57 | slf4j-api
58 | ${slf4j.version}
59 | provided
60 |
61 |
62 |
63 |
64 |
65 | Apache License, Version 2.0
66 | http://www.apache.org/licenses/LICENSE-2.0
67 |
68 |
69 |
70 |
71 | Yu Qi Yu
72 | yuqiyu@vip.qq.com
73 | https://www.jianshu.com/u/092df3f77bca
74 | gitee
75 | https://gitee.com/hengboy
76 |
77 |
78 |
79 | scm:git:https://gitee.com/hengboy/code-builder
80 | scm:git:https://gitee.com/hengboy/code-builder
81 | https://gitee.com/hengboy/code-builder
82 | 1.0.5.RELEASE
83 |
84 |
85 |
86 | hengyu
87 | https://oss.sonatype.org/content/repositories/snapshots
88 |
89 |
90 | hengyu
91 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
92 |
93 |
94 |
141 |
142 |
--------------------------------------------------------------------------------