mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
Merge branch 'main' of https://github.com/mybatis-flex/mybatis-flex
This commit is contained in:
commit
7667b04c85
@ -272,21 +272,7 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
List<QueryColumn> selectColumns = CPI.getSelectColumns(queryWrapper);
|
List<QueryColumn> selectColumns = CPI.getSelectColumns(queryWrapper);
|
||||||
|
|
||||||
StringBuilder sqlBuilder = new StringBuilder("SELECT ");
|
StringBuilder sqlBuilder = buildSelectColumnSql(allTables, selectColumns);
|
||||||
if (selectColumns == null || selectColumns.isEmpty()) {
|
|
||||||
sqlBuilder.append("*");
|
|
||||||
} else {
|
|
||||||
int index = 0;
|
|
||||||
|
|
||||||
for (QueryColumn selectColumn : selectColumns) {
|
|
||||||
String selectColumnSql = CPI.toSelectSql(selectColumn, allTables, this);
|
|
||||||
sqlBuilder.append(selectColumnSql);
|
|
||||||
if (index != selectColumns.size() - 1) {
|
|
||||||
sqlBuilder.append(", ");
|
|
||||||
}
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sqlBuilder.append(" FROM ").append(StringUtil.join(", ", queryTables, queryTable -> queryTable.toSql(this)));
|
sqlBuilder.append(" FROM ").append(StringUtil.join(", ", queryTables, queryTable -> queryTable.toSql(this)));
|
||||||
|
|
||||||
buildJoinSql(sqlBuilder, queryWrapper, allTables);
|
buildJoinSql(sqlBuilder, queryWrapper, allTables);
|
||||||
@ -312,6 +298,25 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
return sqlBuilder.toString();
|
return sqlBuilder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private StringBuilder buildSelectColumnSql(List<QueryTable> queryTables, List<QueryColumn> selectColumns) {
|
||||||
|
StringBuilder sqlBuilder = new StringBuilder("SELECT ");
|
||||||
|
if (selectColumns == null || selectColumns.isEmpty()) {
|
||||||
|
sqlBuilder.append("*");
|
||||||
|
} else {
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
for (QueryColumn selectColumn : selectColumns) {
|
||||||
|
String selectColumnSql = CPI.toSelectSql(selectColumn, queryTables, this);
|
||||||
|
sqlBuilder.append(selectColumnSql);
|
||||||
|
if (index != selectColumns.size() - 1) {
|
||||||
|
sqlBuilder.append(", ");
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sqlBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String buildSelectCountSql(QueryWrapper queryWrapper) {
|
public String buildSelectCountSql(QueryWrapper queryWrapper) {
|
||||||
@ -663,8 +668,8 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String forSelectOneEntityById(TableInfo tableInfo) {
|
public String forSelectOneEntityById(TableInfo tableInfo) {
|
||||||
StringBuilder sql = new StringBuilder("SELECT * FROM ");
|
StringBuilder sql = buildSelectColumnSql(null, tableInfo.getDefaultQueryColumn());
|
||||||
sql.append(wrap(tableInfo.getTableName()));
|
sql.append(" FROM ").append(wrap(tableInfo.getTableName()));
|
||||||
sql.append(" WHERE ");
|
sql.append(" WHERE ");
|
||||||
String[] pKeys = tableInfo.getPrimaryKeys();
|
String[] pKeys = tableInfo.getPrimaryKeys();
|
||||||
for (int i = 0; i < pKeys.length; i++) {
|
for (int i = 0; i < pKeys.length; i++) {
|
||||||
@ -692,8 +697,8 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String forSelectEntityListByIds(TableInfo tableInfo, Object[] primaryValues) {
|
public String forSelectEntityListByIds(TableInfo tableInfo, Object[] primaryValues) {
|
||||||
StringBuilder sql = new StringBuilder("SELECT * FROM ");
|
StringBuilder sql = buildSelectColumnSql(null, tableInfo.getDefaultQueryColumn());
|
||||||
sql.append(wrap(tableInfo.getTableName()));
|
sql.append(" FROM ").append(wrap(tableInfo.getTableName()));
|
||||||
sql.append(" WHERE ");
|
sql.append(" WHERE ");
|
||||||
String[] primaryKeys = tableInfo.getPrimaryKeys();
|
String[] primaryKeys = tableInfo.getPrimaryKeys();
|
||||||
|
|
||||||
|
|||||||
@ -315,6 +315,7 @@ public class EntitySqlProvider {
|
|||||||
Object[] values = CPI.getValueArray(queryWrapper);
|
Object[] values = CPI.getValueArray(queryWrapper);
|
||||||
ProviderUtil.setSqlArgs(params, values);
|
ProviderUtil.setSqlArgs(params, values);
|
||||||
|
|
||||||
|
CPI.setSelectColumnsIfNecessary(queryWrapper, tableInfo.getDefaultQueryColumn());
|
||||||
CPI.setFromIfNecessary(queryWrapper, tableInfo.getTableName());
|
CPI.setFromIfNecessary(queryWrapper, tableInfo.getTableName());
|
||||||
|
|
||||||
return DialectFactory.getDialect().forSelectListByQuery(queryWrapper);
|
return DialectFactory.getDialect().forSelectListByQuery(queryWrapper);
|
||||||
|
|||||||
@ -63,6 +63,13 @@ public class CPI {
|
|||||||
queryWrapper.setSelectColumns(selectColumns);
|
queryWrapper.setSelectColumns(selectColumns);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setSelectColumnsIfNecessary(QueryWrapper queryWrapper, List<QueryColumn> selectColumns) {
|
||||||
|
if (CollectionUtil.isEmpty(queryWrapper.getSelectColumns())
|
||||||
|
&& CollectionUtil.isNotEmpty(selectColumns)) {
|
||||||
|
queryWrapper.setSelectColumns(selectColumns);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static List<Join> getJoins(QueryWrapper queryWrapper) {
|
public static List<Join> getJoins(QueryWrapper queryWrapper) {
|
||||||
return queryWrapper.getJoins();
|
return queryWrapper.getJoins();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,6 +38,7 @@ import org.apache.ibatis.session.Configuration;
|
|||||||
import org.apache.ibatis.type.TypeHandler;
|
import org.apache.ibatis.type.TypeHandler;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class TableInfo {
|
public class TableInfo {
|
||||||
|
|
||||||
@ -71,6 +72,9 @@ public class TableInfo {
|
|||||||
//主键字段
|
//主键字段
|
||||||
private String[] primaryKeys = new String[0];
|
private String[] primaryKeys = new String[0];
|
||||||
|
|
||||||
|
// 默认查询列
|
||||||
|
private String[] defaultColumns = new String[0];
|
||||||
|
|
||||||
//在插入数据的时候,支持主动插入的主键字段
|
//在插入数据的时候,支持主动插入的主键字段
|
||||||
//通过自定义生成器生成 或者 Sequence 在 before 生成的时候,是需要主动插入数据的
|
//通过自定义生成器生成 或者 Sequence 在 before 生成的时候,是需要主动插入数据的
|
||||||
private String[] insertPrimaryKeys;
|
private String[] insertPrimaryKeys;
|
||||||
@ -183,6 +187,14 @@ public class TableInfo {
|
|||||||
this.largeColumns = largeColumns;
|
this.largeColumns = largeColumns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String[] getDefaultColumns() {
|
||||||
|
return defaultColumns;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDefaultColumns(String[] defaultColumns) {
|
||||||
|
this.defaultColumns = defaultColumns;
|
||||||
|
}
|
||||||
|
|
||||||
public String[] getInsertPrimaryKeys() {
|
public String[] getInsertPrimaryKeys() {
|
||||||
return insertPrimaryKeys;
|
return insertPrimaryKeys;
|
||||||
}
|
}
|
||||||
@ -588,6 +600,12 @@ public class TableInfo {
|
|||||||
return joiner.toString();
|
return joiner.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<QueryColumn> getDefaultQueryColumn() {
|
||||||
|
return Arrays.stream(defaultColumns)
|
||||||
|
.map(name -> new QueryColumn(getTableName(), name))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
public ResultMap buildResultMap(Configuration configuration) {
|
public ResultMap buildResultMap(Configuration configuration) {
|
||||||
String resultMapId = entityClass.getName();
|
String resultMapId = entityClass.getName();
|
||||||
List<ResultMapping> resultMappings = new ArrayList<>();
|
List<ResultMapping> resultMappings = new ArrayList<>();
|
||||||
|
|||||||
@ -159,6 +159,8 @@ public class TableInfoFactory {
|
|||||||
|
|
||||||
//大字段列
|
//大字段列
|
||||||
Set<String> largeColumns = new LinkedHashSet<>();
|
Set<String> largeColumns = new LinkedHashSet<>();
|
||||||
|
// 默认查询列
|
||||||
|
Set<String> defaultColumns = new LinkedHashSet<>();
|
||||||
|
|
||||||
|
|
||||||
List<Field> entityFields = ClassUtil.getAllFields(entityClass);
|
List<Field> entityFields = ClassUtil.getAllFields(entityClass);
|
||||||
@ -230,6 +232,9 @@ public class TableInfoFactory {
|
|||||||
largeColumns.add(columnName);
|
largeColumns.add(columnName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (column == null || (!column.isLarge() && !column.isLogicDelete())) {
|
||||||
|
defaultColumns.add(columnName);
|
||||||
|
}
|
||||||
|
|
||||||
Id id = field.getAnnotation(Id.class);
|
Id id = field.getAnnotation(Id.class);
|
||||||
ColumnInfo columnInfo;
|
ColumnInfo columnInfo;
|
||||||
@ -301,6 +306,9 @@ public class TableInfoFactory {
|
|||||||
if (!largeColumns.isEmpty()) {
|
if (!largeColumns.isEmpty()) {
|
||||||
tableInfo.setLargeColumns(largeColumns.toArray(new String[0]));
|
tableInfo.setLargeColumns(largeColumns.toArray(new String[0]));
|
||||||
}
|
}
|
||||||
|
if (!defaultColumns.isEmpty()) {
|
||||||
|
tableInfo.setDefaultColumns(defaultColumns.toArray(new String[0]));
|
||||||
|
}
|
||||||
|
|
||||||
tableInfo.setColumnInfoList(columnInfoList);
|
tableInfo.setColumnInfoList(columnInfoList);
|
||||||
tableInfo.setPrimaryKeyList(idInfos);
|
tableInfo.setPrimaryKeyList(idInfos);
|
||||||
|
|||||||
@ -57,6 +57,12 @@
|
|||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>3.22.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.test;
|
package com.mybatisflex.test;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.mybatis.FlexConfiguration;
|
||||||
import com.mybatisflex.spring.FlexSqlSessionFactoryBean;
|
import com.mybatisflex.spring.FlexSqlSessionFactoryBean;
|
||||||
|
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
||||||
import org.apache.ibatis.session.SqlSessionFactory;
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||||
import org.mybatis.spring.annotation.MapperScan;
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
@ -48,6 +50,9 @@ public class AppConfig implements ApplicationListener<ContextRefreshedEvent> {
|
|||||||
// SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
|
// SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
|
||||||
SqlSessionFactoryBean factoryBean = new FlexSqlSessionFactoryBean();
|
SqlSessionFactoryBean factoryBean = new FlexSqlSessionFactoryBean();
|
||||||
factoryBean.setDataSource(dataSource);
|
factoryBean.setDataSource(dataSource);
|
||||||
|
FlexConfiguration configuration = new FlexConfiguration();
|
||||||
|
configuration.setLogImpl(StdOutImpl.class);
|
||||||
|
factoryBean.setConfiguration(configuration);
|
||||||
return factoryBean.getObject();
|
return factoryBean.getObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,18 +1,24 @@
|
|||||||
package com.mybatisflex.test;
|
package com.mybatisflex.test;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.core.row.Db;
|
import com.mybatisflex.core.row.Db;
|
||||||
import com.mybatisflex.core.row.Row;
|
import com.mybatisflex.core.row.Row;
|
||||||
import com.mybatisflex.test.mapper.AccountMapper;
|
import com.mybatisflex.test.mapper.AccountMapper;
|
||||||
import com.mybatisflex.test.model.Account;
|
import com.mybatisflex.test.model.Account;
|
||||||
|
import org.assertj.core.api.WithAssertions;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.mybatisflex.test.model.table.Tables.ACCOUNT;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = AppConfig.class)
|
@ContextConfiguration(classes = AppConfig.class)
|
||||||
public class AccountTest {
|
public class AccountTest implements WithAssertions {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
AccountMapper accountMapper;
|
AccountMapper accountMapper;
|
||||||
@ -20,7 +26,17 @@ public class AccountTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testSelectOne() {
|
public void testSelectOne() {
|
||||||
Account account = accountMapper.selectOneById(1);
|
Account account = accountMapper.selectOneById(1);
|
||||||
System.out.println(account);
|
assertThat(account).isNotNull()
|
||||||
|
.satisfies(a -> assertThat(a.getId()).isEqualTo(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSelectByQuery() {
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
|
.where(ACCOUNT.AGE.eq(18));
|
||||||
|
List<Account> accounts = accountMapper.selectListByQuery(queryWrapper);
|
||||||
|
assertThat(accounts.size()).isEqualTo(1);
|
||||||
|
assertThat(accounts.get(0).getAge()).isEqualTo(18);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user