mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: 新增根据实体类对象,生成查询条件
This commit is contained in:
parent
9544aeebd6
commit
2e88aeea59
@ -24,6 +24,7 @@ import com.mybatisflex.core.dialect.DbType;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -108,6 +109,10 @@ public class FlexGlobalConfig {
|
||||
|
||||
public void setConfiguration(Configuration configuration) {
|
||||
this.configuration = configuration;
|
||||
DataSource dataSource = configuration.getEnvironment().getDataSource();
|
||||
if (dataSource instanceof FlexDataSource){
|
||||
this.dbType = ((FlexDataSource) dataSource).getDefaultDbType();
|
||||
}
|
||||
}
|
||||
|
||||
public SqlSessionFactory getSqlSessionFactory() {
|
||||
|
||||
@ -44,13 +44,20 @@ public class FlexDataSource extends AbstractDataSource {
|
||||
private final Map<String, DataSource> dataSourceMap = new HashMap<>();
|
||||
private final Map<String, DbType> dbTypeHashMap = new HashMap<>();
|
||||
|
||||
private final DbType defaultDbType;
|
||||
private final String defaultDataSourceKey;
|
||||
private final DataSource defaultDataSource;
|
||||
|
||||
public FlexDataSource(String dataSourceKey, DataSource dataSource) {
|
||||
|
||||
DataSourceManager.decryptDataSource(dataSource);
|
||||
|
||||
this.defaultDataSourceKey = dataSourceKey;
|
||||
this.defaultDataSource = dataSource;
|
||||
addDataSource(dataSourceKey, dataSource);
|
||||
this.defaultDbType = DbTypeUtil.getDbType(dataSource);
|
||||
|
||||
dataSourceMap.put(dataSourceKey, dataSource);
|
||||
dbTypeHashMap.put(dataSourceKey, defaultDbType);
|
||||
}
|
||||
|
||||
public void addDataSource(String dataSourceKey, DataSource dataSource) {
|
||||
@ -80,10 +87,15 @@ public class FlexDataSource extends AbstractDataSource {
|
||||
return defaultDataSource;
|
||||
}
|
||||
|
||||
public DbType getDefaultDbType() {
|
||||
return defaultDbType;
|
||||
}
|
||||
|
||||
public DbType getDbType(String dataSourceKey) {
|
||||
return dbTypeHashMap.get(dataSourceKey);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
String xid = TransactionContext.getXID();
|
||||
|
||||
@ -17,8 +17,6 @@ package com.mybatisflex.core.mybatis;
|
||||
|
||||
import com.mybatisflex.core.FlexConsts;
|
||||
import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.dialect.DbType;
|
||||
import com.mybatisflex.core.dialect.DbTypeUtil;
|
||||
import com.mybatisflex.core.exception.FlexExceptions;
|
||||
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
|
||||
import org.apache.ibatis.exceptions.ExceptionFactory;
|
||||
@ -85,10 +83,9 @@ public class FlexSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder {
|
||||
}
|
||||
|
||||
SqlSessionFactory sessionFactory = super.build(configuration);
|
||||
DbType dbType = DbTypeUtil.getDbType(configuration.getEnvironment().getDataSource());
|
||||
|
||||
//设置全局配置的 sessionFactory 和 dbType
|
||||
initGlobalConfig(configuration, sessionFactory, dbType);
|
||||
// 设置全局配置的 sessionFactory
|
||||
initGlobalConfig(configuration, sessionFactory);
|
||||
|
||||
printBanner();
|
||||
|
||||
@ -115,10 +112,9 @@ public class FlexSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder {
|
||||
* @param config
|
||||
* @param sessionFactory
|
||||
*/
|
||||
private void initGlobalConfig(Configuration config, SqlSessionFactory sessionFactory, DbType dbType) {
|
||||
private void initGlobalConfig(Configuration config, SqlSessionFactory sessionFactory) {
|
||||
FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig();
|
||||
flexGlobalConfig.setSqlSessionFactory(sessionFactory);
|
||||
flexGlobalConfig.setDbType(dbType);
|
||||
flexGlobalConfig.setConfiguration(config);
|
||||
|
||||
String environmentId = config.getEnvironment().getId();
|
||||
|
||||
@ -15,24 +15,95 @@
|
||||
*/
|
||||
package com.mybatisflex.core.query;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.mybatisflex.core.FlexConsts;
|
||||
import com.mybatisflex.core.constant.SqlConsts;
|
||||
import com.mybatisflex.core.dialect.DialectFactory;
|
||||
import com.mybatisflex.core.table.ColumnInfo;
|
||||
import com.mybatisflex.core.table.TableDef;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
import com.mybatisflex.core.util.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
|
||||
|
||||
public static QueryWrapper create() {
|
||||
return new QueryWrapper();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据实体类对象,构建查询条件
|
||||
* 查询出的列,不包含大字段列
|
||||
* @param entity 实体类对象
|
||||
* @return 查询对象
|
||||
*/
|
||||
public static QueryWrapper create(Object entity) {
|
||||
return create(entity, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据实体类对象,构建查询条件
|
||||
* @param entity 实体类对象
|
||||
* @param needLargeColumn 查询出的列,是否包含大字段列
|
||||
* @return 查询对象
|
||||
*/
|
||||
public static QueryWrapper create(Object entity, boolean needLargeColumn) {
|
||||
QueryWrapper queryWrapper = create();
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entity.getClass());
|
||||
Map<String, Object> whereMap = getWhereMap(entity);
|
||||
String[] selectColumn = needLargeColumn ? tableInfo.getAllColumns() : tableInfo.getDefaultQueryColumns();
|
||||
queryWrapper.select(selectColumn);
|
||||
queryWrapper.where(whereMap);
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据实体类对象,获取查询条件的参数
|
||||
* @param entity 实体类对象
|
||||
* @return key:实体对象的字段对应的列名称,value 字段值
|
||||
*/
|
||||
public static Map<String, Object> getWhereMap(Object entity) {
|
||||
Class<?> entityClass = entity.getClass();
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entity.getClass());
|
||||
Map<String, Object> dataMap = new HashMap<>();
|
||||
// 添加非主键列
|
||||
for (ColumnInfo columnInfo : tableInfo.getColumnInfoList()) {
|
||||
// 跳过逻辑删除列
|
||||
if (columnInfo.getColumn().equals(tableInfo.getLogicDeleteColumn())) {
|
||||
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Field declaredField = entityClass.getDeclaredField(columnInfo.getProperty());
|
||||
declaredField.setAccessible(true);
|
||||
Object value = declaredField.get(entity);
|
||||
if (Objects.isNull(value)) {
|
||||
continue;
|
||||
}
|
||||
dataMap.put(columnInfo.getColumn(), value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// 添加主键列
|
||||
for (ColumnInfo columnInfo : tableInfo.getPrimaryKeyList()) {
|
||||
try {
|
||||
Field declaredField = entityClass.getDeclaredField(columnInfo.getProperty());
|
||||
declaredField.setAccessible(true);
|
||||
Object value = declaredField.get(entity);
|
||||
if (Objects.isNull(value)) {
|
||||
continue;
|
||||
}
|
||||
dataMap.put(columnInfo.getColumn(), value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
public WithBuilder with(String name) {
|
||||
if (with == null) {
|
||||
@ -62,12 +133,10 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return new WithBuilder(this, with, name, Arrays.asList(params));
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper select() {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper select(String... columns) {
|
||||
for (String column : columns) {
|
||||
addSelectColumn(new StringQueryColumn(column));
|
||||
@ -75,7 +144,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public <T> QueryWrapper select(LambdaGetter<T>... lambdaGetters) {
|
||||
for (LambdaGetter<T> lambdaGetter : lambdaGetters) {
|
||||
QueryColumn queryColumn = LambdaUtil.getQueryColumn(lambdaGetter);
|
||||
@ -84,7 +152,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper select(QueryColumn... queryColumns) {
|
||||
for (QueryColumn column : queryColumns) {
|
||||
if (column != null) {
|
||||
@ -107,7 +174,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper from(TableDef... tableDefs) {
|
||||
for (TableDef tableDef : tableDefs) {
|
||||
from(new QueryTable(tableDef));
|
||||
@ -115,7 +181,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper from(Class<?>... entityClasses) {
|
||||
for (Class<?> entityClass : entityClasses) {
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
|
||||
@ -124,7 +189,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper from(String... tables) {
|
||||
for (String table : tables) {
|
||||
if (StringUtil.isBlank(table)) {
|
||||
@ -142,7 +206,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper from(QueryTable... tables) {
|
||||
if (CollectionUtil.isEmpty(queryTables)) {
|
||||
queryTables = new ArrayList<>();
|
||||
@ -163,12 +226,10 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper from(QueryWrapper queryWrapper) {
|
||||
return from(new SelectQueryTable(queryWrapper));
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper as(String alias) {
|
||||
if (CollectionUtil.isEmpty(queryTables)) {
|
||||
throw new IllegalArgumentException("query table must not be empty.");
|
||||
@ -178,7 +239,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper where(QueryCondition queryCondition) {
|
||||
this.setWhereQueryCondition(queryCondition);
|
||||
return this;
|
||||
@ -189,13 +249,11 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper where(String sql, Object... params) {
|
||||
this.setWhereQueryCondition(new RawFragment(sql, params));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper where(Map<String, Object> whereConditions) {
|
||||
if (whereConditions != null) {
|
||||
whereConditions.forEach((s, o) -> and(QueryCondition.create(new QueryColumn(s), o)));
|
||||
@ -225,7 +283,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return new QueryConditionBuilder(this, LambdaUtil.getQueryColumn(fn), SqlConnector.AND);
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper and(Consumer<QueryWrapper> consumer) {
|
||||
QueryWrapper newWrapper = new QueryWrapper();
|
||||
consumer.accept(newWrapper);
|
||||
@ -268,7 +325,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(SqlConsts.LEFT_JOIN, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> leftJoin(String table, boolean when) {
|
||||
return joining(SqlConsts.LEFT_JOIN, new QueryTable(table), when);
|
||||
}
|
||||
@ -297,7 +353,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(SqlConsts.LEFT_JOIN, table, when);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> rightJoin(String table) {
|
||||
return joining(SqlConsts.RIGHT_JOIN, new QueryTable(table), true);
|
||||
}
|
||||
@ -330,7 +385,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(SqlConsts.RIGHT_JOIN, table, when);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> innerJoin(String table) {
|
||||
return joining(SqlConsts.INNER_JOIN, new QueryTable(table), true);
|
||||
}
|
||||
@ -363,7 +417,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(SqlConsts.INNER_JOIN, table, when);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> fullJoin(String table) {
|
||||
return joining(SqlConsts.FULL_JOIN, new QueryTable(table), true);
|
||||
}
|
||||
@ -396,7 +449,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(SqlConsts.FULL_JOIN, table, when);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> crossJoin(String table) {
|
||||
return joining(SqlConsts.CROSS_JOIN, new QueryTable(table), true);
|
||||
}
|
||||
@ -429,7 +481,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(SqlConsts.CROSS_JOIN, table, when);
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> join(String table) {
|
||||
return joining(SqlConsts.JOIN, new QueryTable(table), true);
|
||||
}
|
||||
@ -462,7 +513,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(SqlConsts.JOIN, table, when);
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper union(QueryWrapper unionQuery) {
|
||||
if (unions == null) {
|
||||
unions = new ArrayList<>();
|
||||
@ -489,12 +539,10 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
// public QueryWrapper end(String sqlPart){
|
||||
// addEndFragment(sqlPart);
|
||||
// return this;
|
||||
// }
|
||||
|
||||
// public QueryWrapper end(String sqlPart){
|
||||
// addEndFragment(sqlPart);
|
||||
// return this;
|
||||
// }
|
||||
|
||||
protected Joiner<QueryWrapper> joining(String type, QueryTable table, boolean when) {
|
||||
Join join = new Join(type, table, when);
|
||||
@ -514,7 +562,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return new Joiner<>(addJoin(join), join);
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper groupBy(String name) {
|
||||
addGroupByColumns(new QueryColumn(name));
|
||||
return this;
|
||||
@ -539,7 +586,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper having(QueryCondition queryCondition) {
|
||||
addHavingQueryCondition(queryCondition, SqlConnector.AND);
|
||||
return this;
|
||||
@ -565,7 +611,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper limit(Integer rows) {
|
||||
setLimitRows(rows);
|
||||
return this;
|
||||
@ -687,7 +732,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return returnValues;
|
||||
}
|
||||
|
||||
|
||||
List<QueryWrapper> getChildSelect() {
|
||||
|
||||
List<QueryWrapper> tableChildQuery = null;
|
||||
@ -703,7 +747,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
List<QueryWrapper> whereChildQuery = WrapperUtil.getChildQueryWrapper(whereQueryCondition);
|
||||
List<QueryWrapper> havingChildQuery = WrapperUtil.getChildQueryWrapper(havingQueryCondition);
|
||||
|
||||
@ -711,22 +754,19 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
||||
List<QueryWrapper> childQueryWrappers = tableChildQuery == null
|
||||
? new ArrayList<>() : new ArrayList<>(tableChildQuery);
|
||||
List<QueryWrapper> childQueryWrappers = tableChildQuery == null ? new ArrayList<>()
|
||||
: new ArrayList<>(tableChildQuery);
|
||||
childQueryWrappers.addAll(whereChildQuery);
|
||||
childQueryWrappers.addAll(havingChildQuery);
|
||||
|
||||
return childQueryWrappers;
|
||||
}
|
||||
|
||||
|
||||
public String toSQL() {
|
||||
String sql = DialectFactory.getDialect().forSelectByQuery(this);
|
||||
return SqlUtil.replaceSqlParams(sql, getValueArray());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public QueryWrapper clone() {
|
||||
return super.clone();
|
||||
|
||||
@ -0,0 +1,47 @@
|
||||
package com.mybatisflex.test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||
import com.mybatisflex.core.audit.AuditManager;
|
||||
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||
import com.mybatisflex.core.audit.MessageCollector;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.mapper.Entity04Mapper;
|
||||
|
||||
public class QueryWrapperTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
DataSource dataSource = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("schema04.sql")
|
||||
.addScript("data04.sql").build();
|
||||
|
||||
MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance().setDataSource(dataSource)
|
||||
.addMapper(Entity04Mapper.class).start();
|
||||
|
||||
//开启审计功能
|
||||
AuditManager.setAuditEnable(true);
|
||||
|
||||
//设置 SQL 审计收集器
|
||||
MessageCollector collector = new ConsoleMessageCollector();
|
||||
AuditManager.setMessageCollector(collector);
|
||||
|
||||
Entity04Mapper mapper = bootstrap.getMapper(Entity04Mapper.class);
|
||||
|
||||
Entity04 entity04 = new Entity04();
|
||||
entity04.setId("1");
|
||||
entity04.setAge(200);
|
||||
|
||||
mapper.insertSelective(entity04);
|
||||
|
||||
System.out.println("--------------------selectListByQuery");
|
||||
QueryWrapper queryWrapper = QueryWrapper.create(entity04);
|
||||
List<Entity04> entity04s = mapper.selectListByQuery(queryWrapper);
|
||||
System.out.println(entity04s);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user