This commit is contained in:
Michael Yang 2025-07-13 09:26:27 +08:00
commit 531152e1d4
6 changed files with 202 additions and 97 deletions

View File

@ -42,7 +42,7 @@ public class GlobalConfig implements Serializable {
private final FileType fileType;
// === 必须配置 ===
//region === 必须配置 ===
public GlobalConfig() {
this(FileType.JAVA);
@ -52,8 +52,9 @@ public class GlobalConfig implements Serializable {
private final PackageConfig packageConfig;
private final StrategyConfig strategyConfig;
private final TemplateConfig templateConfig;
//endregion === 必须配置 ===
// === 可选配置 ===
//region === 可选配置 ===
private EntityConfig entityConfig;
private MapperConfig mapperConfig;
@ -62,11 +63,13 @@ public class GlobalConfig implements Serializable {
private ControllerConfig controllerConfig;
private TableDefConfig tableDefConfig;
private MapperXmlConfig mapperXmlConfig;
//endregion === 可选配置 ===
// === 其他自定义配置 ===
//region === 其他自定义配置 ===
private Map<String, Object> customConfig = new HashMap<>();
//endregion === 其他自定义配置 ===
// === 是否启用生成 ===
//region === 是否启用生成 ===
private boolean entityGenerateEnable;
private boolean mapperGenerateEnable;
@ -122,8 +125,9 @@ public class GlobalConfig implements Serializable {
}
}
//endregion === 是否启用生成 ===
// === 分类配置 ===
//region === 分类配置 ===
public JavadocConfig getJavadocConfig() {
return javadocConfig;
@ -189,8 +193,9 @@ public class GlobalConfig implements Serializable {
}
return mapperXmlConfig;
}
//endregion === 分类配置 ===
// === 启用配置 ===
//region === 启用配置 ===
public EntityConfig enableEntity() {
entityGenerateEnable = true;
@ -230,8 +235,9 @@ public class GlobalConfig implements Serializable {
public void enablePackageInfo() {
packageInfoGenerateEnable = true;
}
//endregion === 启用配置 ===
// === 禁用配置 ===
//region === 禁用配置 ===
public void disableEntity() {
entityGenerateEnable = false;
@ -264,9 +270,9 @@ public class GlobalConfig implements Serializable {
public void disablePackageInfo() {
packageInfoGenerateEnable = false;
}
//endregion === 禁用配置 ===
// === 自定义配置 ===
//region === 自定义配置 ===
public Object getCustomConfig(String key) {
return customConfig.get(key);
@ -283,7 +289,9 @@ public class GlobalConfig implements Serializable {
public void setCustomConfig(Map<String, Object> customConfig) {
this.customConfig = customConfig;
}
// === 分项配置 ===
//endregion === 自定义配置 ===
//region === 分项配置 ===
/**
* @see JavadocConfig#getAuthor()
@ -1566,5 +1574,5 @@ public class GlobalConfig implements Serializable {
public void setPackageInfoGenerateEnable(boolean packageInfoGenerateEnable) {
this.packageInfoGenerateEnable = packageInfoGenerateEnable;
}
//endregion === 分项配置 ===
}

View File

@ -93,6 +93,10 @@ public class Column {
this.property = buildPropertyName();
}
public void setProperty(String property) {
this.property = property;
}
public String getProperty() {
return property;
}
@ -110,12 +114,58 @@ public class Column {
if (!columnConfig.getPropertyType().contains(".")) {
return columnConfig.getPropertyType();
}
return StringUtil.substringAfterLast(columnConfig.getPropertyType(), ".");
return convertToSimpleGenericType(columnConfig.getPropertyType());
} else {
return StringUtil.substringAfterLast(propertyType, ".");
return convertToSimpleGenericType(propertyType);
}
}
private String convertToSimpleGenericType(String fullType) {
if (fullType == null || fullType.isEmpty()) {
return fullType;
}
// 如果不包含泛型直接处理
if (!fullType.contains("<") && !fullType.endsWith(">")) {
return StringUtil.substringAfterLast(fullType, ".");
}
StringBuilder result = new StringBuilder();
int i = 0;
while (i < fullType.length()) {
char c = fullType.charAt(i);
if (c == '<' || c == ',' || c == '>') {
result.append(c);
if (c == ',' || c == '<') {
// 跳过空格
while (i + 1 < fullType.length() && fullType.charAt(i + 1) == ' ') {
i++;
result.append(' ');
}
}
i++;
} else {
// 提取类型名称
int start = i;
while (i < fullType.length() && fullType.charAt(i) != '<' &&
fullType.charAt(i) != ',' && fullType.charAt(i) != '>') {
i++;
}
String typeName = fullType.substring(start, i).trim();
if (!typeName.isEmpty()) {
// 转换为简单类名
String simpleType = StringUtil.substringAfterLast(typeName, ".");
result.append(simpleType);
}
}
}
return result.toString();
}
public void setPropertyType(String propertyType) {
this.propertyType = propertyType;
}
@ -197,8 +247,8 @@ public class Column {
return "";
} else {
return "/**\n" +
" * " + comment + "\n" +
" */";
" * " + comment + "\n" +
" */";
}
}
@ -212,16 +262,41 @@ public class Column {
*/
private static void addImportClass(Set<String> importClasses, String importClass) {
importClass = importClass.trim();
extractAllTypes(importClasses, importClass);
}
// java.util.List<String> >>>>> java.util.List
if (importClass.contains("<") && importClass.endsWith(">")) {
importClass = importClass.substring(0, importClass.indexOf("<"));
private static void extractAllTypes(Set<String> importClasses, String typeString) {
if (typeString == null || typeString.isEmpty()) {
return;
}
// 不包含.则认为是原始类型不需要import
// lang 包不需要显式导入
if (importClass.contains(".") && !importClass.startsWith("java.lang.")) {
importClasses.add(importClass);
int i = 0;
while (i < typeString.length()) {
// 跳过非字母字符
while (i < typeString.length() && !Character.isLetter(typeString.charAt(i))
&& typeString.charAt(i) != '_') {
i++;
}
if (i >= typeString.length()) {
break;
}
// 提取类名
int start = i;
while (i < typeString.length() &&
(Character.isLetterOrDigit(typeString.charAt(i)) ||
typeString.charAt(i) == '_' ||
typeString.charAt(i) == '.')) {
i++;
}
String className = typeString.substring(start, i);
// 检查是否包含包路径且不是java.lang包
if (className.contains(".") && !className.startsWith("java.lang.")) {
importClasses.add(className);
}
}
}
@ -234,7 +309,7 @@ public class Column {
public String buildAnnotations() {
StringBuilder annotations = new StringBuilder();
//@Id 的注解
// @Id 的注解
if (isPrimaryKey || columnConfig.isPrimaryKey()) {
annotations.append("@Id(");
@ -259,14 +334,13 @@ public class Column {
needComma = true;
}
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(comment)) {
addComma(annotations, needComma);
annotations.append("comment = \"")
.append(this.comment.replace("\n", "")
.replace("\"", "\\\"")
.trim())
.append("\"");
.append(this.comment.replace("\n", "")
.replace("\"", "\\\"")
.trim())
.append("\"");
}
if (annotations.length() == 4) {
@ -281,25 +355,25 @@ public class Column {
}
boolean needGenColumnAnnotation = (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation())
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(this.comment) && annotations.length() == 0);
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(this.comment)
&& annotations.length() == 0);
StringBuilder columnAnnotation = new StringBuilder("@Column(");
//@Column 注解
// @Column 注解
if (columnConfig.getOnInsertValue() != null
|| columnConfig.getOnUpdateValue() != null
|| columnConfig.getLarge() != null
|| columnConfig.getLogicDelete() != null
|| columnConfig.getVersion() != null
|| columnConfig.getJdbcType() != null
|| columnConfig.getTypeHandler() != null
|| columnConfig.getTenantId() != null
|| needGenColumnAnnotation
) {
|| columnConfig.getOnUpdateValue() != null
|| columnConfig.getLarge() != null
|| columnConfig.getLogicDelete() != null
|| columnConfig.getVersion() != null
|| columnConfig.getJdbcType() != null
|| columnConfig.getTypeHandler() != null
|| columnConfig.getTenantId() != null
|| needGenColumnAnnotation) {
boolean needComma = false;
if (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation()
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) {
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) {
columnAnnotation.append("value = \"").append(name).append("\"");
needComma = true;
}
@ -336,7 +410,8 @@ public class Column {
}
if (columnConfig.getTypeHandler() != null) {
addComma(columnAnnotation, needComma);
columnAnnotation.append("typeHandler = ").append(columnConfig.getTypeHandler().getSimpleName()).append(".class");
columnAnnotation.append("typeHandler = ").append(columnConfig.getTypeHandler().getSimpleName())
.append(".class");
needComma = true;
}
if (Boolean.TRUE.equals(columnConfig.getTenantId())) {
@ -347,10 +422,10 @@ public class Column {
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(comment)) {
addComma(columnAnnotation, needComma);
columnAnnotation.append("comment = \"")
.append(this.comment.replace("\n", "")
.replace("\"", "\\\"")
.trim())
.append("\"");
.append(this.comment.replace("\n", "")
.replace("\"", "\\\"")
.trim())
.append("\"");
}
columnAnnotation.append(")");
@ -418,19 +493,19 @@ public class Column {
}
boolean needGenColumnAnnotation = (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation())
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(this.comment));
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|| (entityConfig != null && entityConfig.isColumnCommentEnable()
&& StringUtil.hasText(this.comment));
if (columnConfig.getOnInsertValue() != null
|| columnConfig.getOnUpdateValue() != null
|| columnConfig.getLarge() != null
|| columnConfig.getLogicDelete() != null
|| columnConfig.getVersion() != null
|| columnConfig.getJdbcType() != null
|| columnConfig.getTypeHandler() != null
|| Boolean.TRUE.equals(columnConfig.getTenantId())
|| needGenColumnAnnotation
) {
|| columnConfig.getOnUpdateValue() != null
|| columnConfig.getLarge() != null
|| columnConfig.getLogicDelete() != null
|| columnConfig.getVersion() != null
|| columnConfig.getJdbcType() != null
|| columnConfig.getTypeHandler() != null
|| Boolean.TRUE.equals(columnConfig.getTenantId())
|| needGenColumnAnnotation) {
addImportClass(importClasses, com.mybatisflex.annotation.Column.class.getName());
}
}
@ -450,11 +525,11 @@ public class Column {
@Override
public String toString() {
return "Column{" +
"name='" + name + '\'' +
", className='" + propertyType + '\'' +
", remarks='" + comment + '\'' +
", isAutoIncrement=" + isAutoIncrement +
'}';
"name='" + name + '\'' +
", className='" + propertyType + '\'' +
", remarks='" + comment + '\'' +
", isAutoIncrement=" + isAutoIncrement +
'}';
}
}

View File

@ -74,7 +74,7 @@ public interface BaseMapper<T> {
*/
int DEFAULT_BATCH_SIZE = 1000;
// === insert ===
//region === insert ===
/**
* 插入实体类数据不忽略 {@code null}
@ -253,8 +253,9 @@ public interface BaseMapper<T> {
return update(entity, ignoreNulls);
}
}
//endregion === insert ===
// === delete ===
//region === delete ===
/**
* 根据实体主键来删除数据
@ -343,8 +344,9 @@ public interface BaseMapper<T> {
*/
@DeleteProvider(type = EntitySqlProvider.class, method = "deleteByQuery")
int deleteByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper);
//endregion === delete ===
// === update ===
//region === update ===
/**
* 根据主键来更新数据若实体类属性数据为 {@code null}该属性不会更新到数据库
@ -439,9 +441,9 @@ public interface BaseMapper<T> {
*/
@UpdateProvider(type = EntitySqlProvider.class, method = "updateByQuery")
int updateByQuery(@Param(FlexConsts.ENTITY) T entity, @Param(FlexConsts.IGNORE_NULLS) boolean ignoreNulls, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper);
//endregion === update ===
// === select ===
//region === update ===
/**
* 根据实体主键查询数据
@ -1264,5 +1266,5 @@ public interface BaseMapper<T> {
}
return page;
}
//endregion === update ===
}

View File

@ -43,7 +43,7 @@ public class Mappers {
private Mappers() {
}
private static final Map<Class<?>, Object> MAPPER_OBJECTS = new ConcurrentHashMap<>();
private static final Map<String, Map<Class<?>, Object>> MAPPER_OBJECTS_OF_ENV = new ConcurrentHashMap<>();
private static final Map<Class<?>, Class<?>> ENTITY_MAPPER_MAP = new ConcurrentHashMap<>();
@ -79,14 +79,16 @@ public class Mappers {
* @return {@link BaseMapper} 对象
*/
public static <M> M ofMapperClass(Class<M> mapperClass) {
Object mapperObject = MapUtil.computeIfAbsent(MAPPER_OBJECTS, mapperClass, clazz ->
Map<Class<?>, Object> mapperObjects = MapUtil.computeIfAbsent(MAPPER_OBJECTS_OF_ENV, "default", envId -> new ConcurrentHashMap<>());
Object mapperObject = MapUtil.computeIfAbsent(mapperObjects, mapperClass, clazz ->
Proxy.newProxyInstance(mapperClass.getClassLoader()
, new Class[]{mapperClass}
, new MapperHandler(mapperClass)));
return (M) mapperObject;
}
public static <M> M ofMapperClass(String environmentId, Class<M> mapperClass) {
Object mapperObject = MapUtil.computeIfAbsent(MAPPER_OBJECTS, mapperClass, clazz ->
Map<Class<?>, Object> mapperObjects = MapUtil.computeIfAbsent(MAPPER_OBJECTS_OF_ENV, environmentId, envId -> new ConcurrentHashMap<>());
Object mapperObject = MapUtil.computeIfAbsent(mapperObjects, mapperClass, clazz ->
Proxy.newProxyInstance(mapperClass.getClassLoader()
, new Class[]{mapperClass}
, new MapperHandler(environmentId, mapperClass)));

View File

@ -35,7 +35,7 @@ public class QueryMethods {
private QueryMethods() {
}
// === 数学函数 ===
//region === 数学函数 ===
/**
* 返回 x 的绝对值
@ -680,8 +680,9 @@ public class QueryMethods {
public static <T> QueryColumn cot(LambdaGetter<T> columnX) {
return new FunctionQueryColumn(COT, LambdaUtil.getQueryColumn(columnX));
}
//endregion === 数学函数 ===
// === 字符串函数 ===
//region === 字符串函数 ===
/**
* 返回字符串 s 的字符数
@ -1181,8 +1182,9 @@ public class QueryMethods {
public static <S1, S2> QueryColumn findInSet(LambdaGetter<S1> columnS1, LambdaGetter<S2> columnS2) {
return new FunctionQueryColumn(FIND_IN_SET, LambdaUtil.getQueryColumn(columnS1), LambdaUtil.getQueryColumn(columnS2));
}
//endregion === 字符串函数 ===
// === 日期时间函数 ===
//region === 日期时间函数 ===
/**
* 返回当前日期
@ -1884,8 +1886,9 @@ public class QueryMethods {
public static <T, S> QueryColumn getFormat(LambdaGetter<T> columnType, LambdaGetter<S> columnS) {
return new FunctionQueryColumn(GET_FORMAT, LambdaUtil.getQueryColumn(columnType), LambdaUtil.getQueryColumn(columnS));
}
//endregion === 日期时间函数 ===
// === 系统信息函数 ===
//region === 系统信息函数 ===
/**
* 返回数据库的版本号
@ -1970,8 +1973,9 @@ public class QueryMethods {
public static QueryColumn lastInsertId() {
return new FunctionQueryColumn(LAST_INSERT_ID);
}
//endregion === 系统信息函数 ===
// === 加密函数 ===
//region === 加密函数 ===
/**
* 对字符串 str 进行加密
@ -2056,8 +2060,9 @@ public class QueryMethods {
public static <C, P> QueryColumn decode(LambdaGetter<C> columnCryptStr, LambdaGetter<P> columnPswdStr) {
return new FunctionQueryColumn(DECODE, LambdaUtil.getQueryColumn(columnCryptStr), LambdaUtil.getQueryColumn(columnPswdStr));
}
//endregion === 加密函数 ===
// === 其他函数 ===
//region === 其他函数 ===
/**
* 格式化函数可以将数字 x 进行格式化 x 保留到小数点后 n 这个过程需要进行四舍五入
@ -2226,8 +2231,9 @@ public class QueryMethods {
public static <T> QueryColumn inetNtoa(LambdaGetter<T> columnN) {
return new FunctionQueryColumn(INET_NTOA, LambdaUtil.getQueryColumn(columnN));
}
//endregion === 其他函数 ===
// === 聚合函数 ===
//region === 聚合函数 ===
/**
* 返回指定列的最大值
@ -2312,8 +2318,9 @@ public class QueryMethods {
public static <T> FunctionQueryColumn sum(LambdaGetter<T> column) {
return new FunctionQueryColumn(SUM, LambdaUtil.getQueryColumn(column));
}
//endregion === 聚合函数 ===
// === COUNT ===
//region === COUNT ===
/**
* 返回指定列的总行数
@ -2342,9 +2349,9 @@ public class QueryMethods {
public static <T> FunctionQueryColumn count(LambdaGetter<T> column) {
return new FunctionQueryColumn(COUNT, LambdaUtil.getQueryColumn(column));
}
//endregion === COUNT ===
// === DISTINCT ===
//region === DISTINCT ===
/**
* 对指定列进行去重
@ -2358,8 +2365,9 @@ public class QueryMethods {
return new DistinctQueryColumn(Arrays.stream(columns)
.map(LambdaUtil::getQueryColumn).toArray(QueryColumn[]::new));
}
//endregion === DISTINCT ===
// === CASE THEN ELSE ===
//region === CASE THEN ELSE ===
/**
* 构建 case then when 语句
@ -2374,8 +2382,9 @@ public class QueryMethods {
public static CaseSearchQueryColumn.Builder case_(QueryColumn column) {
return new CaseSearchQueryColumn.Builder(column);
}
//endregion === CASE THEN ELSE ===
// === CONVERT ===
//region === CONVERT ===
/**
* 将所给类型类型转换为另一种类型
@ -2383,8 +2392,9 @@ public class QueryMethods {
public static StringFunctionQueryColumn convert(String... params) {
return new StringFunctionQueryColumn(CONVERT, params);
}
//endregion === CONVERT ===
// === 构建 column ===
//region === 构建 column ===
/**
* 构建 TRUE 常量
@ -2494,8 +2504,9 @@ public class QueryMethods {
}
return queryColumns;
}
//endregion === 构建 column ===
// === IF 函数 ===
//region === IF 函数 ===
/**
* IF 函数
@ -2552,9 +2563,9 @@ public class QueryMethods {
public static <N> QueryColumn ifNull(LambdaGetter<N> nullColumn, String elseColumn) {
return ifNull(nullColumn, new QueryColumn(elseColumn));
}
//endregion === IF 函数 ===
// === 构建 QueryCondition 查询条件 ===
//region === 构建 QueryCondition 查询条件 ===
/**
* EXIST (SELECT ...)
@ -2597,8 +2608,9 @@ public class QueryMethods {
public static QueryCondition bracket(QueryCondition condition) {
return new Brackets(condition);
}
//endregion === 构建 QueryCondition 查询条件 ===
// === 构建 QueryWrapper 查询 ===
//region === 构建 QueryWrapper 查询 ===
/**
* SELECT queryColumns FROM table
@ -2673,5 +2685,5 @@ public class QueryMethods {
public static FunctionQueryColumn date(QueryColumn column) {
return new FunctionQueryColumn("DATE", column);
}
//endregion === 构建 QueryWrapper 查询 ===
}

View File

@ -51,7 +51,7 @@ public interface IService<T> {
*/
BaseMapper<T> getMapper();
// ===== 保存操作 =====
//region ===== 保存操作 =====
/**
* <p>保存实体类对象数据
@ -128,8 +128,9 @@ public interface IService<T> {
Class<BaseMapper<T>> usefulClass = (Class<BaseMapper<T>>) ClassUtil.getUsefulClass(getMapper().getClass());
return SqlUtil.toBool(Db.executeBatch(entities, batchSize, usefulClass, BaseMapper::insertOrUpdateSelective));
}
//endregion ===== 保存操作 =====
// ===== 删除操作 =====
//region ===== 删除操作 =====
/**
* <p>根据查询条件删除数据
@ -197,8 +198,9 @@ public interface IService<T> {
}
return remove(query().where(query));
}
//endregion ===== 删除操作 =====
// ===== 更新操作 =====
//region ===== 更新操作 =====
/**
* <p>根据数据主键更新数据
@ -313,8 +315,9 @@ public interface IService<T> {
Class<BaseMapper<T>> usefulClass = (Class<BaseMapper<T>>) ClassUtil.getUsefulClass(getMapper().getClass());
return SqlUtil.toBool(Db.executeBatch(entities, batchSize, usefulClass, (mapper, entity) -> mapper.update(entity, ignoreNulls)));
}
//endregion ===== 更新操作 =====
// ===== 查询操作 =====
//region ===== 查询操作 =====
/**
* <p>根据数据主键查询一条数据
@ -546,8 +549,9 @@ public interface IService<T> {
default List<T> listByMap(Map<String, Object> query) {
return list(query().where(query));
}
//endregion ===== 查询操作 =====
// ===== 数量查询操作 =====
//region ===== 数量查询操作 =====
/**
* <p>根据查询条件判断数据是否存在
@ -605,8 +609,9 @@ public interface IService<T> {
default long count(QueryCondition condition) {
return count(query().where(condition));
}
//endregion ===== 数量查询操作 =====
// ===== 分页查询操作 =====
//region ===== 分页查询操作 =====
/**
* <p>分页查询所有数据
@ -651,8 +656,9 @@ public interface IService<T> {
default <R> Page<R> pageAs(Page<R> page, QueryWrapper query, Class<R> asType) {
return getMapper().paginateAs(page, query, asType);
}
//endregion ===== 分页查询操作 =====
// ===== 查询包装器操作 =====
//region ===== 查询包装器操作 =====
/**
* 默认 {@link QueryWrapper} 构建
@ -680,5 +686,5 @@ public interface IService<T> {
default UpdateChain<T> updateChain() {
return UpdateChain.create(getMapper());
}
//endregion ===== 查询包装器操作 =====
}