From 2e88aeea59a35eab080160e051e2c2a8ccb9b98a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Wed, 19 Jul 2023 12:44:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E5=AE=9E=E4=BD=93=E7=B1=BB=E5=AF=B9=E8=B1=A1=EF=BC=8C=E7=94=9F?= =?UTF-8?q?=E6=88=90=E6=9F=A5=E8=AF=A2=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/core/FlexGlobalConfig.java | 5 + .../core/datasource/FlexDataSource.java | 14 +- .../mybatis/FlexSqlSessionFactoryBuilder.java | 10 +- .../mybatisflex/core/query/QueryWrapper.java | 122 ++++++++++++------ .../mybatisflex/test/QueryWrapperTest.java | 47 +++++++ 5 files changed, 149 insertions(+), 49 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/QueryWrapperTest.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java index 66baa55a..8a028bba 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java @@ -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() { diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/FlexDataSource.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/FlexDataSource.java index c55fbed1..32949192 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/FlexDataSource.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/datasource/FlexDataSource.java @@ -44,13 +44,20 @@ public class FlexDataSource extends AbstractDataSource { private final Map dataSourceMap = new HashMap<>(); private final Map 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(); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexSqlSessionFactoryBuilder.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexSqlSessionFactoryBuilder.java index 7275dc27..0b34a378 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexSqlSessionFactoryBuilder.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexSqlSessionFactoryBuilder.java @@ -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(); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java index 0a3591c7..b95aba4b 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java @@ -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 { - 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 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 getWhereMap(Object entity) { + Class entityClass = entity.getClass(); + TableInfo tableInfo = TableInfoFactory.ofEntityClass(entity.getClass()); + Map 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 { 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 { return this; } - public QueryWrapper select(LambdaGetter... lambdaGetters) { for (LambdaGetter lambdaGetter : lambdaGetters) { QueryColumn queryColumn = LambdaUtil.getQueryColumn(lambdaGetter); @@ -84,7 +152,6 @@ public class QueryWrapper extends BaseQueryWrapper { return this; } - public QueryWrapper select(QueryColumn... queryColumns) { for (QueryColumn column : queryColumns) { if (column != null) { @@ -107,7 +174,6 @@ public class QueryWrapper extends BaseQueryWrapper { return this; } - public QueryWrapper from(TableDef... tableDefs) { for (TableDef tableDef : tableDefs) { from(new QueryTable(tableDef)); @@ -115,7 +181,6 @@ public class QueryWrapper extends BaseQueryWrapper { 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 { return this; } - public QueryWrapper from(String... tables) { for (String table : tables) { if (StringUtil.isBlank(table)) { @@ -142,7 +206,6 @@ public class QueryWrapper extends BaseQueryWrapper { return this; } - public QueryWrapper from(QueryTable... tables) { if (CollectionUtil.isEmpty(queryTables)) { queryTables = new ArrayList<>(); @@ -163,12 +226,10 @@ public class QueryWrapper extends BaseQueryWrapper { 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 { return this; } - public QueryWrapper where(QueryCondition queryCondition) { this.setWhereQueryCondition(queryCondition); return this; @@ -189,13 +249,11 @@ public class QueryWrapper extends BaseQueryWrapper { return this; } - public QueryWrapper where(String sql, Object... params) { this.setWhereQueryCondition(new RawFragment(sql, params)); return this; } - public QueryWrapper where(Map whereConditions) { if (whereConditions != null) { whereConditions.forEach((s, o) -> and(QueryCondition.create(new QueryColumn(s), o))); @@ -225,7 +283,6 @@ public class QueryWrapper extends BaseQueryWrapper { return new QueryConditionBuilder(this, LambdaUtil.getQueryColumn(fn), SqlConnector.AND); } - public QueryWrapper and(Consumer consumer) { QueryWrapper newWrapper = new QueryWrapper(); consumer.accept(newWrapper); @@ -268,7 +325,6 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(SqlConsts.LEFT_JOIN, new QueryTable(table), true); } - public Joiner leftJoin(String table, boolean when) { return joining(SqlConsts.LEFT_JOIN, new QueryTable(table), when); } @@ -297,7 +353,6 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(SqlConsts.LEFT_JOIN, table, when); } - public Joiner rightJoin(String table) { return joining(SqlConsts.RIGHT_JOIN, new QueryTable(table), true); } @@ -330,7 +385,6 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(SqlConsts.RIGHT_JOIN, table, when); } - public Joiner innerJoin(String table) { return joining(SqlConsts.INNER_JOIN, new QueryTable(table), true); } @@ -363,7 +417,6 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(SqlConsts.INNER_JOIN, table, when); } - public Joiner fullJoin(String table) { return joining(SqlConsts.FULL_JOIN, new QueryTable(table), true); } @@ -396,7 +449,6 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(SqlConsts.FULL_JOIN, table, when); } - public Joiner crossJoin(String table) { return joining(SqlConsts.CROSS_JOIN, new QueryTable(table), true); } @@ -429,7 +481,6 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(SqlConsts.CROSS_JOIN, table, when); } - public Joiner join(String table) { return joining(SqlConsts.JOIN, new QueryTable(table), true); } @@ -462,7 +513,6 @@ public class QueryWrapper extends BaseQueryWrapper { 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 { return this; } - -// public QueryWrapper end(String sqlPart){ -// addEndFragment(sqlPart); -// return this; -// } - + // public QueryWrapper end(String sqlPart){ + // addEndFragment(sqlPart); + // return this; + // } protected Joiner joining(String type, QueryTable table, boolean when) { Join join = new Join(type, table, when); @@ -514,7 +562,6 @@ public class QueryWrapper extends BaseQueryWrapper { 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 { return this; } - public QueryWrapper having(QueryCondition queryCondition) { addHavingQueryCondition(queryCondition, SqlConnector.AND); return this; @@ -565,7 +611,6 @@ public class QueryWrapper extends BaseQueryWrapper { return this; } - public QueryWrapper limit(Integer rows) { setLimitRows(rows); return this; @@ -687,7 +732,6 @@ public class QueryWrapper extends BaseQueryWrapper { return returnValues; } - List getChildSelect() { List tableChildQuery = null; @@ -703,7 +747,6 @@ public class QueryWrapper extends BaseQueryWrapper { } } - List whereChildQuery = WrapperUtil.getChildQueryWrapper(whereQueryCondition); List havingChildQuery = WrapperUtil.getChildQueryWrapper(havingQueryCondition); @@ -711,22 +754,19 @@ public class QueryWrapper extends BaseQueryWrapper { return Collections.emptyList(); } - - List childQueryWrappers = tableChildQuery == null - ? new ArrayList<>() : new ArrayList<>(tableChildQuery); + List 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(); diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/QueryWrapperTest.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/QueryWrapperTest.java new file mode 100644 index 00000000..39760a12 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/QueryWrapperTest.java @@ -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 entity04s = mapper.selectListByQuery(queryWrapper); + System.out.println(entity04s); + } + +}