mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
feat: apt 默认生成独立文件,之前所有 apt 生成在同一个文件修改为可配置。
This commit is contained in:
parent
c21bccc5aa
commit
12a4efd121
@ -16,6 +16,8 @@
|
|||||||
package com.mybatisflex.core;
|
package com.mybatisflex.core;
|
||||||
|
|
||||||
import com.mybatisflex.core.exception.FlexExceptions;
|
import com.mybatisflex.core.exception.FlexExceptions;
|
||||||
|
import com.mybatisflex.core.field.FieldQuery;
|
||||||
|
import com.mybatisflex.core.field.FieldQueryBuilder;
|
||||||
import com.mybatisflex.core.mybatis.MappedStatementTypes;
|
import com.mybatisflex.core.mybatis.MappedStatementTypes;
|
||||||
import com.mybatisflex.core.paginate.Page;
|
import com.mybatisflex.core.paginate.Page;
|
||||||
import com.mybatisflex.core.provider.EntitySqlProvider;
|
import com.mybatisflex.core.provider.EntitySqlProvider;
|
||||||
@ -28,9 +30,12 @@ import com.mybatisflex.core.util.ObjectUtil;
|
|||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
import org.apache.ibatis.annotations.*;
|
import org.apache.ibatis.annotations.*;
|
||||||
import org.apache.ibatis.builder.annotation.ProviderContext;
|
import org.apache.ibatis.builder.annotation.ProviderContext;
|
||||||
|
import org.apache.ibatis.reflection.MetaObject;
|
||||||
|
import org.apache.ibatis.reflection.SystemMetaObject;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import static com.mybatisflex.core.query.QueryMethods.count;
|
import static com.mybatisflex.core.query.QueryMethods.count;
|
||||||
|
|
||||||
@ -407,6 +412,59 @@ public interface BaseMapper<T> {
|
|||||||
List<T> selectListByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper);
|
List<T> selectListByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper);
|
||||||
|
|
||||||
|
|
||||||
|
default List<T> selectListByQuery(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper
|
||||||
|
, Consumer<FieldQueryBuilder<T>>... consumers) {
|
||||||
|
|
||||||
|
List<T> list = selectListByQuery(queryWrapper);
|
||||||
|
if (list == null || list.isEmpty()) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
list.forEach(entity -> {
|
||||||
|
for (Consumer<FieldQueryBuilder<T>> consumer : consumers) {
|
||||||
|
FieldQueryBuilder<T> fieldQueryBuilder = new FieldQueryBuilder<>(entity);
|
||||||
|
consumer.accept(fieldQueryBuilder);
|
||||||
|
FieldQuery fieldQuery = fieldQueryBuilder.build();
|
||||||
|
QueryWrapper childQuery = fieldQuery.getQueryWrapper();
|
||||||
|
MetaObject entityMetaObject = SystemMetaObject.forObject(entity);
|
||||||
|
Class<?> setterType = entityMetaObject.getSetterType(fieldQuery.getField());
|
||||||
|
|
||||||
|
Class<?> mappingType = fieldQuery.getMappingType();
|
||||||
|
if (mappingType == null) {
|
||||||
|
if (setterType.isAssignableFrom(Collection.class)) {
|
||||||
|
throw new IllegalStateException("Mapping Type can not be null for query Many.");
|
||||||
|
} else if (setterType.isArray()) {
|
||||||
|
mappingType = setterType.getComponentType();
|
||||||
|
} else {
|
||||||
|
mappingType = setterType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object value;
|
||||||
|
try {
|
||||||
|
MappedStatementTypes.setCurrentType(mappingType);
|
||||||
|
if (setterType.isAssignableFrom(List.class)) {
|
||||||
|
value = selectListByQueryAs(childQuery, mappingType);
|
||||||
|
} else if (setterType.isAssignableFrom(Set.class)) {
|
||||||
|
value = selectListByQueryAs(childQuery, mappingType);
|
||||||
|
value = new HashSet<>((Collection<?>) value);
|
||||||
|
} else if (setterType.isArray()) {
|
||||||
|
value = selectListByQueryAs(childQuery, mappingType);
|
||||||
|
value = ((List<?>) value).toArray();
|
||||||
|
} else {
|
||||||
|
value = selectOneByQueryAs(childQuery, mappingType);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
MappedStatementTypes.clear();
|
||||||
|
}
|
||||||
|
entityMetaObject.setValue(fieldQuery.getField(), value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据 query 来构建条件查询数据列表,要求返回的数据为 asType
|
* 根据 query 来构建条件查询数据列表,要求返回的数据为 asType
|
||||||
* 这种场景一般用在 left join 时,有多出了 entity 本身的字段内容,可以转换为 dto、vo 等场景时
|
* 这种场景一般用在 left join 时,有多出了 entity 本身的字段内容,可以转换为 dto、vo 等场景时
|
||||||
@ -418,6 +476,69 @@ public interface BaseMapper<T> {
|
|||||||
@SelectProvider(type = EntitySqlProvider.class, method = "selectListByQuery")
|
@SelectProvider(type = EntitySqlProvider.class, method = "selectListByQuery")
|
||||||
<R> List<R> selectListByQueryAs(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper, Class<R> asType);
|
<R> List<R> selectListByQueryAs(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper, Class<R> asType);
|
||||||
|
|
||||||
|
|
||||||
|
default <R> List<R> selectListByQueryAs(@Param(FlexConsts.QUERY) QueryWrapper queryWrapper, Class<R> asType
|
||||||
|
, Consumer<FieldQueryBuilder<R>>... consumers) {
|
||||||
|
List<R> list;
|
||||||
|
try {
|
||||||
|
MappedStatementTypes.setCurrentType(asType);
|
||||||
|
list = selectListByQueryAs(queryWrapper, asType);
|
||||||
|
} finally {
|
||||||
|
MappedStatementTypes.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (list == null || list.isEmpty()) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
list.forEach(entity -> {
|
||||||
|
for (Consumer<FieldQueryBuilder<R>> consumer : consumers) {
|
||||||
|
FieldQueryBuilder<R> fieldQueryBuilder = new FieldQueryBuilder<>(entity);
|
||||||
|
consumer.accept(fieldQueryBuilder);
|
||||||
|
FieldQuery fieldQuery = fieldQueryBuilder.build();
|
||||||
|
QueryWrapper childQuery = fieldQuery.getQueryWrapper();
|
||||||
|
|
||||||
|
MetaObject entityMetaObject = SystemMetaObject.forObject(entity);
|
||||||
|
Class<?> setterType = entityMetaObject.getSetterType(fieldQuery.getField());
|
||||||
|
|
||||||
|
Class<?> mappingType = fieldQuery.getMappingType();
|
||||||
|
if (mappingType == null) {
|
||||||
|
if (setterType.isAssignableFrom(Collection.class)) {
|
||||||
|
throw new IllegalStateException("Mapping Type can not be null for query Many.");
|
||||||
|
} else if (setterType.isArray()) {
|
||||||
|
mappingType = setterType.getComponentType();
|
||||||
|
} else {
|
||||||
|
mappingType = setterType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object value;
|
||||||
|
try {
|
||||||
|
MappedStatementTypes.setCurrentType(mappingType);
|
||||||
|
if (setterType.isAssignableFrom(List.class)) {
|
||||||
|
value = selectListByQueryAs(childQuery, mappingType);
|
||||||
|
} else if (setterType.isAssignableFrom(Set.class)) {
|
||||||
|
value = selectListByQueryAs(childQuery, mappingType);
|
||||||
|
value = new HashSet<>((Collection<?>) value);
|
||||||
|
} else if (setterType.isArray()) {
|
||||||
|
value = selectListByQueryAs(childQuery, mappingType);
|
||||||
|
value = ((List<?>) value).toArray();
|
||||||
|
} else {
|
||||||
|
value = selectOneByQueryAs(childQuery, mappingType);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
MappedStatementTypes.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
entityMetaObject.setValue(fieldQuery.getField(), value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询全部数据
|
* 查询全部数据
|
||||||
*
|
*
|
||||||
|
|||||||
@ -18,16 +18,17 @@ package com.mybatisflex.core.provider;
|
|||||||
import com.mybatisflex.core.dialect.DialectFactory;
|
import com.mybatisflex.core.dialect.DialectFactory;
|
||||||
import com.mybatisflex.core.exception.FlexExceptions;
|
import com.mybatisflex.core.exception.FlexExceptions;
|
||||||
import com.mybatisflex.core.query.CPI;
|
import com.mybatisflex.core.query.CPI;
|
||||||
|
import com.mybatisflex.core.query.QueryTable;
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.core.table.TableInfo;
|
import com.mybatisflex.core.table.TableInfo;
|
||||||
|
import com.mybatisflex.core.table.TableInfoFactory;
|
||||||
import com.mybatisflex.core.util.ArrayUtil;
|
import com.mybatisflex.core.util.ArrayUtil;
|
||||||
import com.mybatisflex.core.util.CollectionUtil;
|
import com.mybatisflex.core.util.CollectionUtil;
|
||||||
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
import org.apache.ibatis.builder.annotation.ProviderContext;
|
import org.apache.ibatis.builder.annotation.ProviderContext;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class EntitySqlProvider {
|
public class EntitySqlProvider {
|
||||||
|
|
||||||
@ -76,7 +77,6 @@ public class EntitySqlProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* insertBatch 的 sql 构建
|
* insertBatch 的 sql 构建
|
||||||
*
|
*
|
||||||
@ -308,19 +308,41 @@ public class EntitySqlProvider {
|
|||||||
if (queryWrapper == null) {
|
if (queryWrapper == null) {
|
||||||
throw FlexExceptions.wrap("queryWrapper can not be null.");
|
throw FlexExceptions.wrap("queryWrapper can not be null.");
|
||||||
}
|
}
|
||||||
|
List<TableInfo> tableInfos = getTableInfos(context, queryWrapper);
|
||||||
|
for (TableInfo tableInfo : tableInfos) {
|
||||||
|
tableInfo.appendConditions(null, queryWrapper);
|
||||||
|
|
||||||
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
|
Object[] values = CPI.getValueArray(queryWrapper);
|
||||||
tableInfo.appendConditions(null, queryWrapper);
|
ProviderUtil.setSqlArgs(params, values);
|
||||||
|
|
||||||
Object[] values = CPI.getValueArray(queryWrapper);
|
CPI.setSelectColumnsIfNecessary(queryWrapper, tableInfo.getDefaultQueryColumn());
|
||||||
ProviderUtil.setSqlArgs(params, values);
|
CPI.setFromIfNecessary(queryWrapper, tableInfo.getTableName());
|
||||||
|
}
|
||||||
CPI.setSelectColumnsIfNecessary(queryWrapper, tableInfo.getDefaultQueryColumn());
|
|
||||||
CPI.setFromIfNecessary(queryWrapper, tableInfo.getTableName());
|
|
||||||
|
|
||||||
return DialectFactory.getDialect().forSelectByQuery(queryWrapper);
|
return DialectFactory.getDialect().forSelectByQuery(queryWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static List<TableInfo> getTableInfos(ProviderContext context, QueryWrapper queryWrapper) {
|
||||||
|
List<TableInfo> tableInfos;
|
||||||
|
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
|
||||||
|
if (CollectionUtil.isNotEmpty(queryTables)) {
|
||||||
|
tableInfos = new ArrayList<>();
|
||||||
|
for (QueryTable queryTable : queryTables) {
|
||||||
|
String tableName = queryTable.getName();
|
||||||
|
if (StringUtil.isNotBlank(tableName)) {
|
||||||
|
TableInfo tableInfo = TableInfoFactory.ofTableName(tableName);
|
||||||
|
if (tableInfo != null) {
|
||||||
|
tableInfos.add(tableInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tableInfos = Collections.singletonList(ProviderUtil.getTableInfo(context));
|
||||||
|
}
|
||||||
|
return tableInfos;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* selectCountByQuery 的 sql 构建
|
* selectCountByQuery 的 sql 构建
|
||||||
*
|
*
|
||||||
@ -335,13 +357,17 @@ public class EntitySqlProvider {
|
|||||||
throw FlexExceptions.wrap("queryWrapper can not be null.");
|
throw FlexExceptions.wrap("queryWrapper can not be null.");
|
||||||
}
|
}
|
||||||
|
|
||||||
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
|
List<TableInfo> tableInfos = getTableInfos(context, queryWrapper);
|
||||||
tableInfo.appendConditions(null, queryWrapper);
|
|
||||||
|
|
||||||
Object[] values = CPI.getValueArray(queryWrapper);
|
for (TableInfo tableInfo : tableInfos) {
|
||||||
ProviderUtil.setSqlArgs(params, values);
|
tableInfo.appendConditions(null, queryWrapper);
|
||||||
|
|
||||||
|
Object[] values = CPI.getValueArray(queryWrapper);
|
||||||
|
ProviderUtil.setSqlArgs(params, values);
|
||||||
|
|
||||||
|
CPI.setFromIfNecessary(queryWrapper, tableInfo.getTableName());
|
||||||
|
}
|
||||||
|
|
||||||
CPI.setFromIfNecessary(queryWrapper, tableInfo.getTableName());
|
|
||||||
return DialectFactory.getDialect().forSelectByQuery(queryWrapper);
|
return DialectFactory.getDialect().forSelectByQuery(queryWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,8 +13,8 @@ import org.junit.Test;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static com.mybatisflex.core.query.QueryMethods.*;
|
import static com.mybatisflex.core.query.QueryMethods.*;
|
||||||
import static com.mybatisflex.coretest.table.Tables.ACCOUNT;
|
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
|
||||||
import static com.mybatisflex.coretest.table.Tables.ARTICLE;
|
import static com.mybatisflex.coretest.table.ArticleTableDef.ARTICLE;
|
||||||
|
|
||||||
public class AccountSqlTester {
|
public class AccountSqlTester {
|
||||||
|
|
||||||
|
|||||||
@ -8,7 +8,8 @@ import com.mybatisflex.core.table.TableInfoFactory;
|
|||||||
import com.mybatisflex.core.util.CollectionUtil;
|
import com.mybatisflex.core.util.CollectionUtil;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static com.mybatisflex.coretest.table.Tables.ARTICLE;
|
import static com.mybatisflex.coretest.table.ArticleTableDef.ARTICLE;
|
||||||
|
|
||||||
|
|
||||||
public class ArticleSqlTester {
|
public class ArticleSqlTester {
|
||||||
|
|
||||||
|
|||||||
@ -83,10 +83,29 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
|
|
||||||
private static final String tableDefTemplate = "\n\n public static final @entityClassTableDef @tableField = new @entityClassTableDef(\"@tableName\");\n";
|
private static final String tableDefTemplate = "\n\n public static final @entityClassTableDef @tableField = new @entityClassTableDef(\"@tableName\");\n";
|
||||||
|
|
||||||
|
private static final String singleEntityClassTemplate = "package @package;\n" +
|
||||||
|
"\n" +
|
||||||
|
"import com.mybatisflex.core.query.QueryColumn;\n" +
|
||||||
|
"import com.mybatisflex.core.table.TableDef;\n" +
|
||||||
|
"\n" +
|
||||||
|
"// Auto generate by mybatis-flex, do not modify it.\n" +
|
||||||
|
"public class @entityClassTableDef extends TableDef {\n" +
|
||||||
|
"\n" +
|
||||||
|
"@selfDef" +
|
||||||
|
"@queryColumns" +
|
||||||
|
"@defaultColumns" +
|
||||||
|
"@allColumns" +
|
||||||
|
"\n" +
|
||||||
|
" public @entityClassTableDef(String tableName) {\n" +
|
||||||
|
" super(tableName);\n" +
|
||||||
|
" }\n" +
|
||||||
|
"}\n";
|
||||||
|
|
||||||
private static final String classTemplate = "\n" +
|
|
||||||
|
private static final String allInTableEntityClassTemplate = "\n" +
|
||||||
" public static class @entityClassTableDef extends TableDef {\n" +
|
" public static class @entityClassTableDef extends TableDef {\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
|
"@selfDef" +
|
||||||
"@queryColumns" +
|
"@queryColumns" +
|
||||||
"@defaultColumns" +
|
"@defaultColumns" +
|
||||||
"@allColumns" +
|
"@allColumns" +
|
||||||
@ -100,8 +119,7 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
private static final String columnsTemplate = " public QueryColumn @property = new QueryColumn(this, \"@columnName\");\n";
|
private static final String columnsTemplate = " public QueryColumn @property = new QueryColumn(this, \"@columnName\");\n";
|
||||||
|
|
||||||
private static final String defaultColumnsTemplate = "\n public QueryColumn[] DEFAULT_COLUMNS = new QueryColumn[]{@allColumns};\n";
|
private static final String defaultColumnsTemplate = "\n public QueryColumn[] DEFAULT_COLUMNS = new QueryColumn[]{@allColumns};\n";
|
||||||
// private static final String allColumnsTemplate = " public QueryColumn[] ALL_COLUMNS = new QueryColumn[]{@allColumns};\n\n";
|
private static final String allColumnsTemplate = " public QueryColumn ALL_COLUMNS = new QueryColumn(this, \"*\");\n";
|
||||||
private static final String allColumnsTemplate = " public QueryColumn ALL_COLUMNS = new QueryColumn(this, \"*\");\n\n";
|
|
||||||
|
|
||||||
private Filer filer;
|
private Filer filer;
|
||||||
private Elements elementUtils;
|
private Elements elementUtils;
|
||||||
@ -132,6 +150,8 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
String baseMapperClass = props.getProperties().getProperty("processor.baseMapperClass", "com.mybatisflex.core.BaseMapper");
|
String baseMapperClass = props.getProperties().getProperty("processor.baseMapperClass", "com.mybatisflex.core.BaseMapper");
|
||||||
String mappersGenerateEnable = props.getProperties().getProperty("processor.mappersGenerateEnable", "false");
|
String mappersGenerateEnable = props.getProperties().getProperty("processor.mappersGenerateEnable", "false");
|
||||||
String genMappersPackage = props.getProperties().getProperty("processor.mappersPackage");
|
String genMappersPackage = props.getProperties().getProperty("processor.mappersPackage");
|
||||||
|
|
||||||
|
boolean allInTables = "true".equalsIgnoreCase(props.getProperties().getProperty("processor.allInTables", "false"));
|
||||||
String className = props.getProperties().getProperty("processor.tablesClassName", "Tables");
|
String className = props.getProperties().getProperty("processor.tablesClassName", "Tables");
|
||||||
|
|
||||||
//upperCase, lowerCase, upperCamelCase, lowerCamelCase
|
//upperCase, lowerCase, upperCamelCase, lowerCamelCase
|
||||||
@ -142,7 +162,6 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
|
|
||||||
StringBuilder guessPackage = new StringBuilder();
|
StringBuilder guessPackage = new StringBuilder();
|
||||||
|
|
||||||
|
|
||||||
StringBuilder tablesContent = new StringBuilder();
|
StringBuilder tablesContent = new StringBuilder();
|
||||||
roundEnv.getElementsAnnotatedWith(Table.class).forEach((Consumer<Element>) entityClassElement -> {
|
roundEnv.getElementsAnnotatedWith(Table.class).forEach((Consumer<Element>) entityClassElement -> {
|
||||||
|
|
||||||
@ -184,7 +203,16 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tablesContent.append(buildTablesClass(entitySimpleName, tableName, propertyAndColumns, defaultColumns, tablesNameStyle));
|
if (allInTables) {
|
||||||
|
String content = buildTablesClass(entitySimpleName, tableName, propertyAndColumns, defaultColumns, tablesNameStyle, null, allInTables);
|
||||||
|
tablesContent.append(content);
|
||||||
|
}
|
||||||
|
//每一个 entity 生成一个独立的文件
|
||||||
|
else {
|
||||||
|
String realGenPackage = genTablesPackage == null || genTablesPackage.trim().length() == 0 ? guessPackage.toString() : genTablesPackage;
|
||||||
|
String content = buildTablesClass(entitySimpleName, tableName, propertyAndColumns, defaultColumns, tablesNameStyle, realGenPackage, allInTables);
|
||||||
|
genClass(genPath, realGenPackage, entitySimpleName + "TableDef", content);
|
||||||
|
}
|
||||||
|
|
||||||
//是否开启 mapper 生成功能
|
//是否开启 mapper 生成功能
|
||||||
if ("true".equalsIgnoreCase(mappersGenerateEnable) && table.mapperGenerateEnable()) {
|
if ("true".equalsIgnoreCase(mappersGenerateEnable) && table.mapperGenerateEnable()) {
|
||||||
@ -194,7 +222,7 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (tablesContent.length() > 0) {
|
if (allInTables && tablesContent.length() > 0) {
|
||||||
String realGenPackage = genTablesPackage == null || genTablesPackage.trim().length() == 0 ? guessPackage.toString() : genTablesPackage;
|
String realGenPackage = genTablesPackage == null || genTablesPackage.trim().length() == 0 ? guessPackage.toString() : genTablesPackage;
|
||||||
genTablesClass(genPath, realGenPackage, className, tablesContent.toString());
|
genTablesClass(genPath, realGenPackage, className, tablesContent.toString());
|
||||||
}
|
}
|
||||||
@ -300,7 +328,7 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
|
|
||||||
|
|
||||||
private String buildTablesClass(String entityClass, String tableName, Map<String, String> propertyAndColumns
|
private String buildTablesClass(String entityClass, String tableName, Map<String, String> propertyAndColumns
|
||||||
, List<String> defaultColumns, String tablesNameStyle) {
|
, List<String> defaultColumns, String tablesNameStyle, String realGenPackage, boolean allInTables) {
|
||||||
|
|
||||||
// tableDefTemplate = " public static final @entityClassTableDef @tableField = new @entityClassTableDef(\"@tableName\");\n";
|
// tableDefTemplate = " public static final @entityClassTableDef @tableField = new @entityClassTableDef(\"@tableName\");\n";
|
||||||
|
|
||||||
@ -312,7 +340,7 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
//columnsTemplate = " public QueryColumn @property = new QueryColumn(this, \"@columnName\");\n";
|
//columnsTemplate = " public QueryColumn @property = new QueryColumn(this, \"@columnName\");\n";
|
||||||
StringBuilder queryColumns = new StringBuilder();
|
StringBuilder queryColumns = new StringBuilder();
|
||||||
propertyAndColumns.forEach((property, column) ->
|
propertyAndColumns.forEach((property, column) ->
|
||||||
queryColumns.append(columnsTemplate
|
queryColumns.append(columnsTemplate.substring(allInTables ? 0 : 4) //移除 4 个空格
|
||||||
.replace("@property", buildName(property, tablesNameStyle))
|
.replace("@property", buildName(property, tablesNameStyle))
|
||||||
.replace("@columnName", column)
|
.replace("@columnName", column)
|
||||||
));
|
));
|
||||||
@ -349,12 +377,24 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
// " }\n" +
|
// " }\n" +
|
||||||
// " }\n";
|
// " }\n";
|
||||||
|
|
||||||
String tableClass = classTemplate.replace("@entityClass", entityClass)
|
String tableClass;
|
||||||
.replace("@queryColumns", queryColumns)
|
if (allInTables) {
|
||||||
.replace("@defaultColumns", defaultColumnsString)
|
tableClass = allInTableEntityClassTemplate.replace("@entityClass", entityClass)
|
||||||
.replace("@allColumns", allColumnsString);
|
.replace("@queryColumns", queryColumns)
|
||||||
|
.replace("@defaultColumns", defaultColumnsString)
|
||||||
|
.replace("@allColumns", allColumnsString);
|
||||||
|
} else {
|
||||||
|
tableClass = singleEntityClassTemplate
|
||||||
|
.replace("@package", realGenPackage)
|
||||||
|
.replace("@entityClass", entityClass)
|
||||||
|
.replace("@selfDef", tableDef.replace("\n\n", "") + "\n")
|
||||||
|
.replace("@queryColumns", queryColumns)
|
||||||
|
.replace("@defaultColumns", "\n" + defaultColumnsString.substring(5)) //移除 "换行 + 4 个空格"
|
||||||
|
.replace("@allColumns", allColumnsString.substring(4)); //移除 4 个空格
|
||||||
|
}
|
||||||
|
|
||||||
return tableDef + tableClass;
|
|
||||||
|
return allInTables ? tableDef + tableClass : tableClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -363,6 +403,11 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
.replace("@classesInfo", classContent)
|
.replace("@classesInfo", classContent)
|
||||||
.replace("@tablesClassName", className);
|
.replace("@tablesClassName", className);
|
||||||
|
|
||||||
|
genClass(genBasePath, genPackageName, className, genContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void genClass(String genBasePath, String genPackageName, String className, String genContent) {
|
||||||
Writer writer = null;
|
Writer writer = null;
|
||||||
try {
|
try {
|
||||||
JavaFileObject sourceFile = filer.createSourceFile(genPackageName + "." + className);
|
JavaFileObject sourceFile = filer.createSourceFile(genPackageName + "." + className);
|
||||||
@ -370,8 +415,6 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
writer = sourceFile.openWriter();
|
writer = sourceFile.openWriter();
|
||||||
writer.write(genContent);
|
writer.write(genContent);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
|
|
||||||
// printMessage(">>>>> mybatis-flex success generate tables class: \n" + sourceFile.toUri());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -408,9 +451,6 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
writer = new PrintWriter(new FileOutputStream(genJavaFile));
|
writer = new PrintWriter(new FileOutputStream(genJavaFile));
|
||||||
writer.write(genContent);
|
writer.write(genContent);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
|
|
||||||
// printMessage(">>>>> mybatis-flex success generate tables class: \n" + genJavaFile.toURI());
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
@ -440,64 +480,7 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
.replace("@baseMapperClzName", baseMapperClzName);
|
.replace("@baseMapperClzName", baseMapperClzName);
|
||||||
|
|
||||||
String mapperClassName = entityName + "Mapper";
|
String mapperClassName = entityName + "Mapper";
|
||||||
Writer writer = null;
|
genClass(genBasePath, genPackageName, mapperClassName, genContent);
|
||||||
try {
|
|
||||||
JavaFileObject sourceFile = filer.createSourceFile(genPackageName + "." + mapperClassName);
|
|
||||||
if (genBasePath == null || genBasePath.trim().length() == 0) {
|
|
||||||
writer = sourceFile.openWriter();
|
|
||||||
writer.write(genContent);
|
|
||||||
writer.flush();
|
|
||||||
|
|
||||||
// printMessage(">>>>> mybatis-flex success generate mapper class: \n" + sourceFile.toUri());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
String defaultGenPath = sourceFile.toUri().getPath();
|
|
||||||
|
|
||||||
//真实的生成代码的目录
|
|
||||||
String realPath;
|
|
||||||
|
|
||||||
//用户配置的路径为绝对路径
|
|
||||||
if (isAbsolutePath(genBasePath)) {
|
|
||||||
realPath = genBasePath;
|
|
||||||
}
|
|
||||||
//配置的是相对路径,那么则以项目根目录为相对路径
|
|
||||||
else {
|
|
||||||
String projectRootPath = getProjectRootPath(defaultGenPath);
|
|
||||||
realPath = new File(projectRootPath, genBasePath).getAbsolutePath();
|
|
||||||
}
|
|
||||||
|
|
||||||
//通过在 test/java 目录下执行编译生成的
|
|
||||||
boolean fromTestSource = isFromTestSource(defaultGenPath);
|
|
||||||
if (fromTestSource) {
|
|
||||||
realPath = new File(realPath, "src/test/java").getAbsolutePath();
|
|
||||||
} else {
|
|
||||||
realPath = new File(realPath, "src/main/java").getAbsolutePath();
|
|
||||||
}
|
|
||||||
|
|
||||||
File genJavaFile = new File(realPath, (genPackageName + "." + mapperClassName).replace(".", "/") + ".java");
|
|
||||||
if (!genJavaFile.getParentFile().exists() && !genJavaFile.getParentFile().mkdirs()) {
|
|
||||||
System.out.println(">>>>>ERROR: can not mkdirs by mybatis-flex processor for: " + genJavaFile.getParentFile());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
writer = new PrintWriter(new FileOutputStream(genJavaFile));
|
|
||||||
writer.write(genContent);
|
|
||||||
writer.flush();
|
|
||||||
|
|
||||||
// printMessage(">>>>> mybatis-flex success generate mapper class: \n" + genJavaFile.toURI());
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
if (writer != null) {
|
|
||||||
try {
|
|
||||||
writer.close();
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -595,8 +578,6 @@ public class QueryEntityProcessor extends AbstractProcessor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 当前 Maven 模块所在所在的目录
|
* 当前 Maven 模块所在所在的目录
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
private String getModuleRootPath(String genFilePath) {
|
private String getModuleRootPath(String genFilePath) {
|
||||||
File file = new File(genFilePath);
|
File file = new File(genFilePath);
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import javax.validation.constraints.NotBlank;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Table(value = "tb_account", dataSource = "ds2", onSet = AccountOnSetListener.class)
|
@Table(value = "tb_account", dataSource = "ds2", onSet = AccountOnSetListener.class)
|
||||||
@ -32,6 +33,8 @@ public class Account extends BaseEntity implements Serializable, AgeAware {
|
|||||||
@Column(isLogicDelete = true)
|
@Column(isLogicDelete = true)
|
||||||
private Boolean isDelete;
|
private Boolean isDelete;
|
||||||
|
|
||||||
|
private List<Article> articles;
|
||||||
|
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
@ -82,6 +85,21 @@ public class Account extends BaseEntity implements Serializable, AgeAware {
|
|||||||
options.put(key, value);
|
options.put(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean getDelete() {
|
||||||
|
return isDelete;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDelete(Boolean delete) {
|
||||||
|
isDelete = delete;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Article> getArticles() {
|
||||||
|
return articles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArticles(List<Article> articles) {
|
||||||
|
this.articles = articles;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
@ -92,6 +110,7 @@ public class Account extends BaseEntity implements Serializable, AgeAware {
|
|||||||
", birthday=" + birthday +
|
", birthday=" + birthday +
|
||||||
", options=" + options +
|
", options=" + options +
|
||||||
", isDelete=" + isDelete +
|
", isDelete=" + isDelete +
|
||||||
|
", articles=" + articles +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,8 +28,8 @@ import javax.sql.DataSource;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.mybatisflex.core.query.QueryMethods.select;
|
import static com.mybatisflex.core.query.QueryMethods.select;
|
||||||
import static com.mybatisflex.test.table.Tables.ACCOUNT;
|
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
||||||
import static com.mybatisflex.test.table.Tables.ARTICLE;
|
import static com.mybatisflex.test.table.ArticleTableDef.ARTICLE;
|
||||||
|
|
||||||
public class EntityTestStarter {
|
public class EntityTestStarter {
|
||||||
|
|
||||||
@ -55,6 +55,17 @@ public class EntityTestStarter {
|
|||||||
|
|
||||||
|
|
||||||
AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class);
|
AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class);
|
||||||
|
|
||||||
|
List<Account> accounts1 = accountMapper.selectListByQuery(QueryWrapper.create()
|
||||||
|
, accountFieldQueryBuilder -> accountFieldQueryBuilder
|
||||||
|
.field(Account::getArticles)
|
||||||
|
.type(Article.class)
|
||||||
|
.queryWrapper(entity ->
|
||||||
|
select().from(ARTICLE).where(ARTICLE.ACCOUNT_ID.eq(entity.getId()))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
System.out.println(accounts1);
|
||||||
|
|
||||||
// MyAccountMapper myAccountMapper = bootstrap.getMapper(MyAccountMapper.class);
|
// MyAccountMapper myAccountMapper = bootstrap.getMapper(MyAccountMapper.class);
|
||||||
|
|
||||||
// List<Account> accounts1 = myAccountMapper.selectAll();
|
// List<Account> accounts1 = myAccountMapper.selectAll();
|
||||||
@ -86,8 +97,8 @@ public class EntityTestStarter {
|
|||||||
QueryWrapper asWrapper = QueryWrapper.create()
|
QueryWrapper asWrapper = QueryWrapper.create()
|
||||||
.select(ARTICLE.ALL_COLUMNS)
|
.select(ARTICLE.ALL_COLUMNS)
|
||||||
.select(ACCOUNT.USER_NAME.as(ArticleDTO::getAuthorName)
|
.select(ACCOUNT.USER_NAME.as(ArticleDTO::getAuthorName)
|
||||||
,ACCOUNT.AGE.as(ArticleDTO::getAuthorAge)
|
, ACCOUNT.AGE.as(ArticleDTO::getAuthorAge)
|
||||||
,ACCOUNT.BIRTHDAY
|
, ACCOUNT.BIRTHDAY
|
||||||
)
|
)
|
||||||
.from(ARTICLE)
|
.from(ARTICLE)
|
||||||
.leftJoin(ACCOUNT).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
|
.leftJoin(ACCOUNT).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
|
||||||
@ -99,7 +110,6 @@ public class EntityTestStarter {
|
|||||||
System.out.println(paginate);
|
System.out.println(paginate);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// QueryWrapper queryWrapper = new QueryWrapper();
|
// QueryWrapper queryWrapper = new QueryWrapper();
|
||||||
// queryWrapper.where(ACCOUNT.ID.in(
|
// queryWrapper.where(ACCOUNT.ID.in(
|
||||||
// select(ACCOUNT.ID).from(ACCOUNT).where(ACCOUNT.ID.in(
|
// select(ACCOUNT.ID).from(ACCOUNT).where(ACCOUNT.ID.in(
|
||||||
@ -112,7 +122,6 @@ public class EntityTestStarter {
|
|||||||
// System.out.println(paginate);
|
// System.out.println(paginate);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// QueryWrapper query = QueryWrapper.create()
|
// QueryWrapper query = QueryWrapper.create()
|
||||||
// .where(ACCOUNT.ID.in(
|
// .where(ACCOUNT.ID.in(
|
||||||
// select(ACCOUNT.ID).from(ACCOUNT)
|
// select(ACCOUNT.ID).from(ACCOUNT)
|
||||||
|
|||||||
@ -31,7 +31,7 @@ import javax.sql.DataSource;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.mybatisflex.test.table.Tables.ACCOUNT;
|
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
||||||
|
|
||||||
public class RowTestStarter {
|
public class RowTestStarter {
|
||||||
|
|
||||||
|
|||||||
@ -28,8 +28,8 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
|||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import static com.mybatisflex.core.query.QueryMethods.select;
|
import static com.mybatisflex.core.query.QueryMethods.select;
|
||||||
import static com.mybatisflex.test.table.Tables.ACCOUNT;
|
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
||||||
import static com.mybatisflex.test.table.Tables.TENANT_ACCOUNT;
|
import static com.mybatisflex.test.table.TenantAccountTableDef.TENANT_ACCOUNT;
|
||||||
|
|
||||||
public class TenantTester {
|
public class TenantTester {
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import com.alibaba.fastjson2.JSONWriter;
|
|||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import org.nustaq.serialization.FSTConfiguration;
|
import org.nustaq.serialization.FSTConfiguration;
|
||||||
|
|
||||||
import static com.mybatisflex.test.table.Tables.ACCOUNT;
|
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
||||||
|
|
||||||
public class WrapperSerializeTest {
|
public class WrapperSerializeTest {
|
||||||
|
|
||||||
|
|||||||
@ -5,4 +5,5 @@ VALUES (1, '张三', 18, 0,'2020-01-11', null,0),
|
|||||||
|
|
||||||
INSERT INTO tb_article
|
INSERT INTO tb_article
|
||||||
VALUES (1, 1,'标题1', '内容1',0),
|
VALUES (1, 1,'标题1', '内容1',0),
|
||||||
(2, 2,'标题2', '内容2',0);
|
(2, 2,'标题2', '内容2',0),
|
||||||
|
(3, 1,'标题3', '内容3',0);
|
||||||
|
|||||||
@ -14,7 +14,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.mybatisflex.test.model.table.Tables.ACCOUNT;
|
import static com.mybatisflex.test.model.table.AccountTableDef.ACCOUNT;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = AppConfig.class)
|
@ContextConfiguration(classes = AppConfig.class)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user