!85 optimize mybatis-flex-codegen javadoc

Merge pull request !85 from 王帅/main
This commit is contained in:
Michael Yang 2023-06-22 01:38:51 +00:00 committed by Gitee
commit a040d76257
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
23 changed files with 687 additions and 297 deletions

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<Table> 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"});
}
}
}

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<? extends TypeHandler> 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;
}
}

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<? extends InsertListener> insertListenerClass;
/**
* 监听 entity update 行为
*/
private Class<? extends UpdateListener> updateListenerClass;
/**
* 监听 entity 的查询数据的 set 行为
*/
private Class<? extends SetListener> setListenerClass;
/**
* 对应列的配置
*/
private Map<String, ColumnConfig> 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);
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;

View File

@ -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() {
}
}

View File

@ -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() {
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
}

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<String, String> buildColumnRemarks(Table table, DatabaseMetaData dbMeta, Connection conn) {
Map<String, String> 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);
}

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<String, String> 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;
}
}

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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();
}
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 +
'}';
}
}

View File

@ -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<String> primaryKeys;
/**
* 所包含的列
*/
private List<Column> 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<String> 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<String> buildImports() {
Set<String> 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 {
'}';
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;

View File

@ -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<String, IGenerator> 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<IGenerator> getGenerators() {
return generators.values();
}
/**
* 注册文件生成器
*
* @param name 生成器名称
* @param generator 生成器
*/
public static void registerGenerator(String name, IGenerator generator) {
generators.put(name, generator);
}
}

View File

@ -98,6 +98,9 @@ public class PackageInfoGenerator implements IGenerator {
this.templatePath = templatePath;
}
/**
* 内置类用于存放数据
*/
private static class Data {
String packageName;

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;

View File

@ -1,25 +1,36 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<String, Object> params, String templateFilePath, File generateFile);
}

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
/**
* <a href=https://jfinal.com/doc/6-1>JFinal Enjoy</a> 模板引擎实现
*/
public class EnjoyTemplate implements ITemplate {
private final Engine engine;
@ -43,17 +46,22 @@ public class EnjoyTemplate implements ITemplate {
@Override
public void generate(Map<String, Object> 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

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;