mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
QueryWrapper.from support multi table
This commit is contained in:
parent
b9a152c582
commit
35384a02c1
@ -21,6 +21,7 @@ import com.mybatisflex.core.util.ArrayUtil;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
@ -40,29 +41,37 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper from(TableDef tableDef) {
|
||||
return from(tableDef.getTableName());
|
||||
public QueryWrapper from(TableDef... tableDefs) {
|
||||
for (TableDef tableDef : tableDefs) {
|
||||
from(new QueryTable(tableDef.getTableName()));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper from(String table) {
|
||||
return from(new QueryTable(table));
|
||||
public QueryWrapper from(String... tables) {
|
||||
for (String table : tables) {
|
||||
from(new QueryTable(table));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper from(QueryTable table) {
|
||||
public QueryWrapper from(QueryTable... tables) {
|
||||
if (CollectionUtil.isEmpty(queryTables)) {
|
||||
queryTables = new ArrayList<>();
|
||||
queryTables.add(table);
|
||||
queryTables.addAll(Arrays.asList(tables));
|
||||
} else {
|
||||
boolean contains = false;
|
||||
for (QueryTable queryTable : queryTables) {
|
||||
if (queryTable.isSameTable(table)) {
|
||||
contains = true;
|
||||
for (QueryTable table : tables) {
|
||||
boolean contains = false;
|
||||
for (QueryTable queryTable : queryTables) {
|
||||
if (queryTable.isSameTable(table)) {
|
||||
contains = true;
|
||||
}
|
||||
}
|
||||
if (!contains) {
|
||||
queryTables.add(table);
|
||||
}
|
||||
}
|
||||
if (!contains) {
|
||||
queryTables.add(table);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
@ -73,6 +82,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return from(new SelectQueryTable(queryWrapper));
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper as(String alias) {
|
||||
if (CollectionUtil.isEmpty(queryTables)) {
|
||||
throw new IllegalArgumentException("query table must not be empty.");
|
||||
@ -212,6 +222,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
public Joiner<QueryWrapper> fullJoinIf(QueryWrapper table, boolean condition) {
|
||||
return joining(Join.TYPE_FULL, table, condition);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> crossJoin(String table) {
|
||||
return joining(Join.TYPE_CROSS, table, true);
|
||||
}
|
||||
@ -229,7 +240,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected Joiner<QueryWrapper> joining(String type, String table, boolean condition) {
|
||||
Join join = new Join(type, table, condition);
|
||||
addJoinTable(join.getQueryTable());
|
||||
|
||||
@ -41,6 +41,19 @@ public class AccountSqlTester {
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelect1ColumnsSql() {
|
||||
QueryWrapper query = new QueryWrapper()
|
||||
.select(ACCOUNT.ID, ACCOUNT.USER_NAME,
|
||||
ARTICLE.ID.as("articleId"), ARTICLE.TITLE)
|
||||
.from(ACCOUNT.as("a"), ARTICLE.as("b"))
|
||||
.where(ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID));
|
||||
|
||||
IDialect dialect = new CommonsDialectImpl(KeywordWrap.NONE,LimitOffsetProcesser.MYSQL);
|
||||
String sql = dialect.forSelectListByQuery(query);
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectColumnsAndFunctionsSql() {
|
||||
QueryWrapper query = new QueryWrapper()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user