From f8d31b6d91e254c22d82b6bb3ae49d51af154afa Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 22:02:16 +0800 Subject: [PATCH 01/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20package-inf?= =?UTF-8?q?o.java=20=E6=96=B9=E4=BE=BF=E5=BC=80=E5=8F=91=E8=80=85=E7=90=86?= =?UTF-8?q?=E8=A7=A3=E5=8C=85=E5=86=85=E5=AE=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/package-info.java | 20 +++++++++++++++++++ .../codegen/constant/package-info.java | 20 +++++++++++++++++++ .../codegen/dialect/package-info.java | 20 +++++++++++++++++++ .../codegen/entity/package-info.java | 20 +++++++++++++++++++ .../codegen/generator/impl/package-info.java | 20 +++++++++++++++++++ .../codegen/generator/package-info.java | 20 +++++++++++++++++++ .../codegen/template/impl/package-info.java | 20 +++++++++++++++++++ .../codegen/template/package-info.java | 20 +++++++++++++++++++ 8 files changed, 160 insertions(+) create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/package-info.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/package-info.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/package-info.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/package-info.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/package-info.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/package-info.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/package-info.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/package-info.java diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/package-info.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/package-info.java new file mode 100644 index 00000000..94681442 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * 配置选项。 + */ +package com.mybatisflex.codegen.config; \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/package-info.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/package-info.java new file mode 100644 index 00000000..ba6aeec5 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * 常量定义。 + */ +package com.mybatisflex.codegen.constant; \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/package-info.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/package-info.java new file mode 100644 index 00000000..30014ccc --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * SQL 方言。 + */ +package com.mybatisflex.codegen.dialect; \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/package-info.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/package-info.java new file mode 100644 index 00000000..1a670c74 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * 数据库表与列信息。 + */ +package com.mybatisflex.codegen.entity; \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/package-info.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/package-info.java new file mode 100644 index 00000000..379ba6c4 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * 代码生成器实现类。 + */ +package com.mybatisflex.codegen.generator.impl; \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/package-info.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/package-info.java new file mode 100644 index 00000000..a05e47c9 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * 代码生成器。 + */ +package com.mybatisflex.codegen.generator; \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/package-info.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/package-info.java new file mode 100644 index 00000000..8e33ee0e --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * 模板引擎实现类。 + */ +package com.mybatisflex.codegen.template.impl; \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/package-info.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/package-info.java new file mode 100644 index 00000000..6f8d09fc --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * 模板引擎。 + */ +package com.mybatisflex.codegen.template; \ No newline at end of file From 61b6773700bed4861c0de5670ebd25f84814750a Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 22:09:56 +0800 Subject: [PATCH 02/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20Column=20?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E6=B3=A8=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/codegen/entity/Column.java | 63 +++++++++++++------ 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java index 97c1cfc8..4a3feef3 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java @@ -1,17 +1,17 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.entity; @@ -24,22 +24,51 @@ import com.mybatisflex.core.util.StringUtil; import java.util.ArrayList; import java.util.List; +/** + * 数据库表里面的列信息。 + */ public class Column { + /** + * 字段名称。 + */ private String name; + + /** + * 属性名称。 + */ private String property; + + /** + * 属性类型。 + */ private String propertyType; + /** + * 字段注释。 + */ private String comment; + /** + * 是否为主键。 + */ private boolean isPrimaryKey = false; + + /** + * 是否自增。 + */ private Boolean isAutoIncrement; + /** + * 是否需要生成 @Column 注解。 + */ private boolean needGenColumnAnnotation = false; + /** + * 字段配置。 + */ private ColumnConfig columnConfig; - public String getName() { return name; } @@ -106,7 +135,6 @@ public class Column { return "set" + StringUtil.firstCharToUpperCase(property); } - public String buildComment() { if (StringUtil.isBlank(comment)) { return ""; @@ -118,7 +146,6 @@ public class Column { } } - public String buildPropertyName() { String entityJavaFileName = name; return StringUtil.firstCharToLowerCase(StringUtil.underlineToCamel(entityJavaFileName)); @@ -285,7 +312,6 @@ public class Column { return importClasses; } - @Override public String toString() { return "Column{" + @@ -295,4 +321,5 @@ public class Column { ", isAutoIncrement=" + isAutoIncrement + '}'; } + } From 04f28261764382e265eb62eb7e32d898f50da06d Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 22:11:03 +0800 Subject: [PATCH 03/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20Table=20?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E6=B3=A8=E9=87=8A=EF=BC=8C=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=B1=BB=E7=BB=93=E6=9E=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/codegen/entity/Table.java | 279 ++++++++++-------- 1 file changed, 162 insertions(+), 117 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java index 2d7e5119..c0e96cc0 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java @@ -22,17 +22,46 @@ import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors; +/** + * 数据库表信息。 + */ public class Table { - private String schema; + /** + * 表名。 + */ private String name; + + /** + * schema(模式)。 + */ + private String schema; + + /** + * 表注释。 + */ private String comment; + + /** + * 主键。 + */ private Set primaryKeys; + + /** + * 所包含的列。 + */ private List columns = new ArrayList<>(); - private GlobalConfig globalConfig; + /** + * 表配置。 + */ private TableConfig tableConfig; + /** + * 全局配置。 + */ + private GlobalConfig globalConfig; + public String getSchema() { return schema; } @@ -69,7 +98,6 @@ public class Table { .orElse(null); } - public Set getPrimaryKeys() { return primaryKeys; } @@ -98,10 +126,8 @@ public class Table { //主键 if (primaryKeys != null && primaryKeys.contains(column.getName())) { column.setPrimaryKey(true); - if (column.getAutoIncrement() == null) { - if (column.getPropertyType().equals(Integer.class.getName()) || column.getPropertyType().equals(BigInteger.class.getName())) { - column.setAutoIncrement(true); - } + if (column.getAutoIncrement() == null && (column.getPropertyType().equals(Integer.class.getName()) || column.getPropertyType().equals(BigInteger.class.getName()))) { + column.setAutoIncrement(true); } } @@ -130,9 +156,13 @@ public class Table { this.tableConfig = tableConfig; } + // ===== 构建实体类文件 ===== + + /** + * 构建 import 导包。 + */ public List buildImports() { Set imports = new HashSet<>(); -// imports.add(com.mybatisflex.annotation.Table.class.getName()); imports.add("com.mybatisflex.annotation.Table"); for (Column column : columns) { imports.addAll(column.getImportClasses()); @@ -165,115 +195,8 @@ public class Table { return imports.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList()); } - public String getEntityJavaFileName() { - String entityJavaFileName = name; - String tablePrefix = globalConfig.getStrategyConfig().getTablePrefix(); - if (tablePrefix != null) { - String[] tablePrefixes = tablePrefix.split(","); - for (String prefix : tablePrefixes) { - String trimPrefix = prefix.trim(); - if (trimPrefix.length() > 0 && name.startsWith(trimPrefix)) { - entityJavaFileName = name.substring(trimPrefix.length()); - break; - } - } - } - return StringUtil.firstCharToUpperCase(StringUtil.underlineToCamel(entityJavaFileName)); - } - /** - * 构建 entity 的 Class 名称 - * - * @return className - */ - public String buildEntityClassName() { - String entityJavaFileName = getEntityJavaFileName(); - EntityConfig entityConfig = globalConfig.getEntityConfig(); - return entityConfig.getClassPrefix() - + entityJavaFileName - + entityConfig.getClassSuffix(); - } - - /** - * 构建 tableDef 的 Class 名称 - * - * @return className - */ - public String buildTableDefClassName() { - String tableDefJavaFileName = getEntityJavaFileName(); - TableDefConfig tableDefConfig = globalConfig.getTableDefConfig(); - return tableDefConfig.getClassPrefix() - + tableDefJavaFileName - + tableDefConfig.getClassSuffix(); - } - - /** - * 构建 MapperXml 的文件名称 - * - * @return fileName - */ - public String buildMapperXmlFileName() { - String tableDefJavaFileName = getEntityJavaFileName(); - MapperXmlConfig mapperXmlConfig = globalConfig.getMapperXmlConfig(); - return mapperXmlConfig.getFilePrefix() - + tableDefJavaFileName - + mapperXmlConfig.getFileSuffix(); - } - - public String buildExtends() { - EntityConfig entityConfig = globalConfig.getEntityConfig(); - if (entityConfig.getSupperClass() != null) { - return " extends " + entityConfig.getSupperClass().getSimpleName(); - } else { - return ""; - } - } - - public String buildImplements() { - Class[] entityInterfaces = globalConfig.getEntityConfig().getImplInterfaces(); - if (entityInterfaces != null && entityInterfaces.length > 0) { - return " implements " + StringUtil.join(", ", Arrays.stream(entityInterfaces) - .map(Class::getSimpleName).collect(Collectors.toList())); - } else { - return ""; - } - } - - - public String buildMapperClassName() { - String entityJavaFileName = getEntityJavaFileName(); - MapperConfig mapperConfig = globalConfig.getMapperConfig(); - return mapperConfig.getClassPrefix() - + entityJavaFileName - + mapperConfig.getClassSuffix(); - } - - public String buildServiceClassName() { - String entityJavaFileName = getEntityJavaFileName(); - ServiceConfig serviceConfig = globalConfig.getServiceConfig(); - return serviceConfig.getClassPrefix() - + entityJavaFileName - + serviceConfig.getClassSuffix(); - } - - public String buildServiceImplClassName() { - String entityJavaFileName = getEntityJavaFileName(); - ServiceImplConfig serviceImplConfig = globalConfig.getServiceImplConfig(); - return serviceImplConfig.getClassPrefix() - + entityJavaFileName - + serviceImplConfig.getClassSuffix(); - } - - public String buildControllerClassName() { - String entityJavaFileName = getEntityJavaFileName(); - ControllerConfig controllerConfig = globalConfig.getControllerConfig(); - return controllerConfig.getClassPrefix() - + entityJavaFileName - + controllerConfig.getClassSuffix(); - } - - /** - * 构建 @Table(...) 注解 + * 构建 @Table(...) 注解。 */ public String buildTableAnnotation() { StringBuilder tableAnnotation = new StringBuilder(); @@ -307,6 +230,129 @@ public class Table { return tableAnnotation.append(")").toString(); } + /** + * 构建 extends 继承。 + */ + public String buildExtends() { + EntityConfig entityConfig = globalConfig.getEntityConfig(); + if (entityConfig.getSupperClass() != null) { + return " extends " + entityConfig.getSupperClass().getSimpleName(); + } else { + return ""; + } + } + + /** + * 构建 implements 实现。 + */ + public String buildImplements() { + Class[] entityInterfaces = globalConfig.getEntityConfig().getImplInterfaces(); + if (entityInterfaces != null && entityInterfaces.length > 0) { + return " implements " + StringUtil.join(", ", Arrays.stream(entityInterfaces) + .map(Class::getSimpleName).collect(Collectors.toList())); + } else { + return ""; + } + } + + // ===== 构建相关类名 ===== + + /** + * 获取生成 Java 文件名。 + */ + public String getEntityJavaFileName() { + String entityJavaFileName = name; + String tablePrefix = globalConfig.getStrategyConfig().getTablePrefix(); + if (tablePrefix != null) { + String[] tablePrefixes = tablePrefix.split(","); + for (String prefix : tablePrefixes) { + String trimPrefix = prefix.trim(); + if (trimPrefix.length() > 0 && name.startsWith(trimPrefix)) { + entityJavaFileName = name.substring(trimPrefix.length()); + break; + } + } + } + return StringUtil.firstCharToUpperCase(StringUtil.underlineToCamel(entityJavaFileName)); + } + + /** + * 构建 entity 的 Class 名称。 + */ + public String buildEntityClassName() { + String entityJavaFileName = getEntityJavaFileName(); + EntityConfig entityConfig = globalConfig.getEntityConfig(); + return entityConfig.getClassPrefix() + + entityJavaFileName + + entityConfig.getClassSuffix(); + } + + /** + * 构建 tableDef 的 Class 名称。 + */ + public String buildTableDefClassName() { + String tableDefJavaFileName = getEntityJavaFileName(); + TableDefConfig tableDefConfig = globalConfig.getTableDefConfig(); + return tableDefConfig.getClassPrefix() + + tableDefJavaFileName + + tableDefConfig.getClassSuffix(); + } + + /** + * 构建 mapper 的 Class 名称。 + */ + public String buildMapperClassName() { + String entityJavaFileName = getEntityJavaFileName(); + MapperConfig mapperConfig = globalConfig.getMapperConfig(); + return mapperConfig.getClassPrefix() + + entityJavaFileName + + mapperConfig.getClassSuffix(); + } + + /** + * 构建 service 的 Class 名称。 + */ + public String buildServiceClassName() { + String entityJavaFileName = getEntityJavaFileName(); + ServiceConfig serviceConfig = globalConfig.getServiceConfig(); + return serviceConfig.getClassPrefix() + + entityJavaFileName + + serviceConfig.getClassSuffix(); + } + + /** + * 构建 serviceImpl 的 Class 名称。 + */ + public String buildServiceImplClassName() { + String entityJavaFileName = getEntityJavaFileName(); + ServiceImplConfig serviceImplConfig = globalConfig.getServiceImplConfig(); + return serviceImplConfig.getClassPrefix() + + entityJavaFileName + + serviceImplConfig.getClassSuffix(); + } + + /** + * 构建 controller 的 Class 名称。 + */ + public String buildControllerClassName() { + String entityJavaFileName = getEntityJavaFileName(); + ControllerConfig controllerConfig = globalConfig.getControllerConfig(); + return controllerConfig.getClassPrefix() + + entityJavaFileName + + controllerConfig.getClassSuffix(); + } + + /** + * 构建 MapperXml 的文件名称。 + */ + public String buildMapperXmlFileName() { + String tableDefJavaFileName = getEntityJavaFileName(); + MapperXmlConfig mapperXmlConfig = globalConfig.getMapperXmlConfig(); + return mapperXmlConfig.getFilePrefix() + + tableDefJavaFileName + + mapperXmlConfig.getFileSuffix(); + } + @Override public String toString() { return "Table{" + @@ -318,5 +364,4 @@ public class Table { '}'; } - } From 1b1d46989103e6f73e8a944c2cf742334bf9a8db Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 22:11:56 +0800 Subject: [PATCH 04/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20ColumnConfi?= =?UTF-8?q?g=20=E7=B1=BB=E7=9A=84=E5=B1=9E=E6=80=A7=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/ColumnConfig.java | 82 +++++++++++++++---- 1 file changed, 67 insertions(+), 15 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ColumnConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ColumnConfig.java index 0be32654..04579aae 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ColumnConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ColumnConfig.java @@ -1,17 +1,17 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.config; @@ -21,30 +21,81 @@ import org.apache.ibatis.type.TypeHandler; import java.io.Serializable; +/** + * 表字段的单独设置。 + */ public class ColumnConfig implements Serializable { + /** + * 字段名称。 + */ private String columnName; + /** + * insert 的时候默认值,这个值会直接被拼接到 sql 而不通过参数设置。 + */ private String onInsertValue; + + /** + * update 的时候自动赋值,这个值会直接被拼接到 sql 而不通过参数设置。 + */ private String onUpdateValue; + /** + * 是否是大字段,大字段 APT 不会生成到 DEFAULT_COLUMNS 里。 + */ private Boolean isLarge; + + /** + * 是否是逻辑删除字段,一张表中只能存在 1 一个逻辑删除字段。 + */ private Boolean isLogicDelete; + + /** + * 是否为乐观锁字段。 + */ private Boolean version; + /** + * 配置的 jdbcType。 + */ private JdbcType jdbcType; + + /** + * 自定义 TypeHandler。 + */ private Class typeHandler; + /** + * 脱敏方式。 + */ private String mask; + /** + * 字段是否为主键。 + */ private boolean isPrimaryKey = false; + + /** + * ID 生成策略。 + */ private KeyType keyType; + + /** + * ID 生成器值。 + */ private String keyValue; + + /** + * sequence 序列执行顺序。 + */ private Boolean keyBefore; + /** + * 是否是租户 ID。 + */ private Boolean tenantId; - public String getColumnName() { return columnName; } @@ -156,4 +207,5 @@ public class ColumnConfig implements Serializable { public void setTenantId(Boolean tenantId) { this.tenantId = tenantId; } + } From 86284b932a3ab9710aaa9db8b68e31bc8ebfd4ee Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 22:12:14 +0800 Subject: [PATCH 05/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20TableConfig?= =?UTF-8?q?=20=E7=B1=BB=E7=9A=84=E5=B1=9E=E6=80=A7=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/TableConfig.java | 59 ++++++++++++------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableConfig.java index f403c11c..707d5ad5 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableConfig.java @@ -1,17 +1,17 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.config; @@ -22,35 +22,51 @@ import com.mybatisflex.annotation.UpdateListener; import java.util.HashMap; import java.util.Map; +/** + * 表的单独设置。 + */ public class TableConfig { + /** + * 表名。 + */ private String tableName; /** - * 数据库的 schema(模式) + * 数据库的 schema(模式)。 */ private String schema; /** - * 默认为 驼峰属性 转换为 下划线字段 + * 默认为 驼峰属性 转换为 下划线字段。 */ private Boolean camelToUnderline; - + /** + * 监听 entity 的 insert 行为。 + */ private Class insertListenerClass; - + /** + * 监听 entity 的 update 行为。 + */ private Class updateListenerClass; - + /** + * 监听 entity 的查询数据的 set 行为。 + */ private Class setListenerClass; - + /** + * 对应列的配置。 + */ private Map columnConfigMap; + /** + * 是否开启 Mapper 生成。 + */ private Boolean mapperGenerateEnable = Boolean.TRUE; - public String getTableName() { return tableName; } @@ -126,5 +142,4 @@ public class TableConfig { return columnConfigMap == null ? null : columnConfigMap.get(columnName); } - } From 95a474847d880a6b12b0d9f39a6eead71a327b61 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 22:13:00 +0800 Subject: [PATCH 06/15] =?UTF-8?q?style:=20=E8=B0=83=E6=95=B4=E7=A7=81?= =?UTF-8?q?=E6=9C=89=E6=9E=84=E9=80=A0=E5=99=A8=E4=BD=8D=E7=BD=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/codegen/constant/GenTypeConst.java | 7 +++---- .../com/mybatisflex/codegen/constant/TemplateConst.java | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/GenTypeConst.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/GenTypeConst.java index e91969cf..e06ae201 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/GenTypeConst.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/GenTypeConst.java @@ -23,6 +23,9 @@ package com.mybatisflex.codegen.constant; */ public class GenTypeConst { + private GenTypeConst() { + } + public static final String ENTITY = "entity"; public static final String MAPPER = "mapper"; public static final String SERVICE = "service"; @@ -32,8 +35,4 @@ public class GenTypeConst { public static final String MAPPER_XML = "mapperXml"; public static final String PACKAGE_INFO = "package-info"; - - private GenTypeConst() { - } - } \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java index 21a14621..f2a929d2 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java @@ -24,6 +24,9 @@ package com.mybatisflex.codegen.constant; */ public final class TemplateConst { + private TemplateConst() { + } + public static final String ENTITY = "/templates/enjoy/entity.tpl"; public static final String MAPPER = "/templates/enjoy/mapper.tpl"; public static final String SERVICE = "/templates/enjoy/service.tpl"; @@ -33,7 +36,4 @@ public final class TemplateConst { public static final String MAPPER_XML = "/templates/enjoy/mapperXml.tpl"; public static final String PACKAGE_INFO = "/templates/enjoy/package-info.tpl"; - private TemplateConst() { - } - } \ No newline at end of file From 90d2fe48ce0a52da87eba8bd105d521a646e3e4e Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 22:13:23 +0800 Subject: [PATCH 07/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=E5=86=85?= =?UTF-8?q?=E9=83=A8=E7=B1=BB=E6=B3=A8=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/generator/impl/PackageInfoGenerator.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/PackageInfoGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/PackageInfoGenerator.java index d6c1fab1..9f7d0f95 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/PackageInfoGenerator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/PackageInfoGenerator.java @@ -98,6 +98,9 @@ public class PackageInfoGenerator implements IGenerator { this.templatePath = templatePath; } + /** + * 内置类,用于存放数据。 + */ private static class Data { String packageName; From 16f607ef28bcfce181d375b96be0c2c766966cb5 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 22:16:06 +0800 Subject: [PATCH 08/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20ITemplate?= =?UTF-8?q?=20=E6=B3=A8=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/template/ITemplate.java | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/ITemplate.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/ITemplate.java index 5b6b7a40..9e562c7d 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/ITemplate.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/ITemplate.java @@ -1,25 +1,36 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.template; import java.io.File; import java.util.Map; +/** + * 模板引擎。 + */ +@FunctionalInterface public interface ITemplate { + /** + * 使用模板引擎生成代码。 + * + * @param params 生成参数 + * @param templateFilePath 模板文件位置 + * @param generateFile 生成文件位置 + */ void generate(Map params, String templateFilePath, File generateFile); } From 12e563fc7a2eb391fec3bb989f189f256de7919d Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 22:16:54 +0800 Subject: [PATCH 09/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20Generator?= =?UTF-8?q?=20=E6=B3=A8=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/codegen/Generator.java | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java index 34e79d39..1b2c2d41 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java @@ -1,17 +1,17 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen; @@ -31,6 +31,9 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +/** + * 代码生成器。 + */ public class Generator { protected DataSource dataSource; @@ -40,20 +43,17 @@ public class Generator { protected Connection conn = null; protected DatabaseMetaData dbMeta = null; - public Generator(DataSource dataSource, GlobalConfig globalConfig) { this.dataSource = dataSource; this.globalConfig = globalConfig; } - public Generator(DataSource dataSource, GlobalConfig globalConfig, IDialect dialect) { this.dataSource = dataSource; this.globalConfig = globalConfig; this.dialect = dialect; } - public void generate() { try { conn = dataSource.getConnection(); @@ -74,7 +74,6 @@ public class Generator { } } - protected void buildPrimaryKey(Table table) throws SQLException { try (ResultSet rs = dbMeta.getPrimaryKeys(conn.getCatalog(), null, table.getName())) { while (rs.next()) { @@ -84,7 +83,6 @@ public class Generator { } } - private List buildTables() throws SQLException { StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); String schemaName = strategyConfig.getGenerateSchema(); @@ -117,7 +115,6 @@ public class Generator { return tables; } - protected ResultSet getTablesResultSet(String schema) throws SQLException { if (globalConfig.getStrategyConfig().isGenerateForView()) { return dialect.getTablesResultSet(dbMeta, conn, schema, new String[]{"TABLE", "VIEW"}); @@ -125,4 +122,5 @@ public class Generator { return dialect.getTablesResultSet(dbMeta, conn, schema, new String[]{"TABLE"}); } } + } From ab2c5b706ab6c12ef1423248d27f4991244aeca6 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 22:17:07 +0800 Subject: [PATCH 10/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20GeneratorFa?= =?UTF-8?q?ctory=20=E6=B3=A8=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/generator/GeneratorFactory.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/GeneratorFactory.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/GeneratorFactory.java index 9bfb5b68..3cd21890 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/GeneratorFactory.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/GeneratorFactory.java @@ -22,6 +22,11 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; +/** + * 代码生成器工厂,用于创建各种类型文件的生成。 + * + * @see GenTypeConst + */ public class GeneratorFactory { private static final Map generators = new HashMap<>(); @@ -37,17 +42,36 @@ public class GeneratorFactory { registerGenerator(GenTypeConst.PACKAGE_INFO, new PackageInfoGenerator()); } + private GeneratorFactory() { + } + + /** + * 获取指定类型文件的生成器。 + * + * @param genType 生成类型 + * @return 该类型的文件生成器 + */ public static IGenerator getGenerator(String genType) { return generators.get(genType); } - + /** + * 获取所有的文件生成器。 + * + * @return 所有的文件生成器 + */ public static Collection getGenerators() { return generators.values(); } - + /** + * 注册文件生成器。 + * + * @param name 生成器名称 + * @param generator 生成器 + */ public static void registerGenerator(String name, IGenerator generator) { generators.put(name, generator); } + } From 933e4dfb48371e89b2af2c70157f62341adcb185 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 21 Jun 2023 22:17:26 +0800 Subject: [PATCH 11/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20EnjoyTempla?= =?UTF-8?q?te=20=E6=B3=A8=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/template/impl/EnjoyTemplate.java | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/EnjoyTemplate.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/EnjoyTemplate.java index cd1b43a8..f752ecc6 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/EnjoyTemplate.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/EnjoyTemplate.java @@ -1,17 +1,17 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.template.impl; @@ -28,6 +28,9 @@ import java.io.File; import java.io.FileOutputStream; import java.util.Map; +/** + * JFinal Enjoy 模板引擎实现。 + */ public class EnjoyTemplate implements ITemplate { private final Engine engine; @@ -43,17 +46,22 @@ public class EnjoyTemplate implements ITemplate { @Override public void generate(Map params, String templateFilePath, File generateFile) { - if (!generateFile.getParentFile().exists() && !generateFile.getParentFile().mkdirs()){ + if (!generateFile.getParentFile().exists() && !generateFile.getParentFile().mkdirs()) { throw new IllegalStateException("Can not mkdirs by dir: " + generateFile.getParentFile()); } - - try(FileOutputStream fileOutputStream = new FileOutputStream(generateFile)) { + // 开始生成文件 + try (FileOutputStream fileOutputStream = new FileOutputStream(generateFile)) { engine.getTemplate(templateFilePath).render(params, fileOutputStream); } catch (Exception e) { e.printStackTrace(); } } + /** + * 从文件或者类路径读取模板。 + * + * @author 王帅 + */ public static class FileAndClassPathSourceFactory implements ISourceFactory { @Override From 607a8f1cbe1bc0512b86787aadf46ecb17618f6c Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 22 Jun 2023 09:17:45 +0800 Subject: [PATCH 12/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20IDialect=20?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/codegen/dialect/IDialect.java | 66 ++++++++++++++----- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/IDialect.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/IDialect.java index 97cad28b..231d0155 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/IDialect.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/IDialect.java @@ -1,17 +1,17 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.dialect; @@ -24,8 +24,14 @@ import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; +/** + * 方言接口。 + */ public interface IDialect { + /** + * 默认方言。 + */ IDialect DEFAULT = new JdbcDialect() { @Override String forBuildColumnsSql(String schema, String tableName) { @@ -33,7 +39,9 @@ public interface IDialect { } }; - + /** + * MySQL 方言。 + */ IDialect MYSQL = new JdbcDialect() { @Override String forBuildColumnsSql(String schema, String tableName) { @@ -41,7 +49,9 @@ public interface IDialect { } }; - + /** + * Oracle 方言。 + */ IDialect ORACLE = new JdbcDialect() { @Override public String forBuildColumnsSql(String schema, String tableName) { @@ -54,10 +64,32 @@ public interface IDialect { } }; + /** + * Sqlite 方言。 + */ IDialect SQLITE = new SqliteDialect(); - + /** + * 构建表和列的信息。 + * + * @param table 存入的表对象 + * @param globalConfig 全局配置 + * @param dbMeta 数据库元数据 + * @param conn 连接 + * @throws SQLException 发生 SQL 异常时抛出 + */ void buildTableColumns(Table table, GlobalConfig globalConfig, DatabaseMetaData dbMeta, Connection conn) throws SQLException; + /** + * 获取表的描述信息。 + * + * @param dbMeta 数据库元数据 + * @param conn 连接 + * @param schema 模式 + * @param types 结果集类型 + * @return 结果集 + * @throws SQLException 发生 SQL 异常时抛出 + */ ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException; + } From e98f2bd1f36950aca0a850c3694d2ff94a876455 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 22 Jun 2023 09:18:00 +0800 Subject: [PATCH 13/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20JdbcDialect?= =?UTF-8?q?=20=E6=B3=A8=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/dialect/JdbcDialect.java | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java index 676e7ba1..2f4258f9 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java @@ -1,17 +1,17 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.dialect; @@ -23,6 +23,9 @@ import java.sql.*; import java.util.HashMap; import java.util.Map; +/** + * 默认方言抽象类。 + */ public abstract class JdbcDialect implements IDialect { @Override @@ -52,7 +55,6 @@ public abstract class JdbcDialect implements IDialect { } } - private Map buildColumnRemarks(Table table, DatabaseMetaData dbMeta, Connection conn) { Map columnRemarks = new HashMap<>(); try (ResultSet colRs = dbMeta.getColumns(conn.getCatalog(), null, table.getName(), null)) { @@ -65,12 +67,18 @@ public abstract class JdbcDialect implements IDialect { return columnRemarks; } - @Override public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException { return dbMeta.getTables(conn.getCatalog(), schema, null, types); } - + /** + * 构建查询所有数据的 SQL 语句。 + * + * @param schema 模式 + * @param tableName 表名 + * @return 全量查询 SQL 语句 + */ abstract String forBuildColumnsSql(String schema, String tableName); + } From 65dcab9b26e54dec87d2268585677b3c2d97ee6c Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 22 Jun 2023 09:18:18 +0800 Subject: [PATCH 14/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20SqliteDiale?= =?UTF-8?q?ct=20=E6=B3=A8=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/dialect/SqliteDialect.java | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/SqliteDialect.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/SqliteDialect.java index 7e57e149..96257739 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/SqliteDialect.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/SqliteDialect.java @@ -1,17 +1,17 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.dialect; @@ -23,6 +23,9 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.sql.*; +/** + * Sqlite 方言实现。 + */ public class SqliteDialect implements IDialect { @Override @@ -48,13 +51,11 @@ public class SqliteDialect implements IDialect { } } - @Override public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException { return dbMeta.getTables(conn.getCatalog(), schema, null, types); } - private String type2ClassName(String type) { int indexOf = type.indexOf("("); if (indexOf > 0) { @@ -94,4 +95,5 @@ public class SqliteDialect implements IDialect { return String.class.getName(); } } + } From 27c4ced2d82c2c8b2e6cdce324585626263db8ea Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 22 Jun 2023 09:18:33 +0800 Subject: [PATCH 15/15] =?UTF-8?q?style:=20=E6=B7=BB=E5=8A=A0=20JdbcTypeMap?= =?UTF-8?q?ping=20=E6=B3=A8=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/dialect/JdbcTypeMapping.java | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcTypeMapping.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcTypeMapping.java index 14eb7d00..3f8f3e50 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcTypeMapping.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcTypeMapping.java @@ -1,17 +1,17 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.dialect; @@ -20,8 +20,14 @@ import com.mybatisflex.core.util.StringUtil; import java.util.HashMap; import java.util.Map; +/** + * 字段类型映射。 + */ public class JdbcTypeMapping { + private JdbcTypeMapping() { + } + private static final Map mapping = new HashMap<>(); static { @@ -50,9 +56,9 @@ public class JdbcTypeMapping { registerMapping("java.time.LocalDate", "java.util.Date"); } - static String getType(String jdbcType) { String registered = mapping.get(jdbcType); return StringUtil.isNotBlank(registered) ? registered : jdbcType; } + }