diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexConsts.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexConsts.java index b31bd46c..8d37c2bf 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexConsts.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexConsts.java @@ -26,6 +26,7 @@ public class FlexConsts { public static final String SQL = "$$sql"; public static final String SQL_ARGS = "$$sql_args"; + public static final String SCHEMA_NAME = "$$schemaName"; public static final String TABLE_NAME = "$$tableName"; public static final String FIELD_NAME = "$$fieldName"; public static final String PRIMARY_KEY = "$$primaryKey"; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/IDialect.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/IDialect.java index 2fd7a800..979e311a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/IDialect.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/IDialect.java @@ -36,9 +36,9 @@ public interface IDialect { String forHint(String hintString); - String forInsertRow(String tableName, Row row); + String forInsertRow(String schema,String tableName, Row row); - String forInsertBatchWithFirstRowColumns(String tableName, List rows); + String forInsertBatchWithFirstRowColumns(String schema, String tableName, List rows); String forDeleteById(String schema, String tableName, String[] primaryKeys); @@ -46,13 +46,13 @@ public interface IDialect { String forDeleteByQuery(QueryWrapper queryWrapper); - String forUpdateById(String tableName, Row row); + String forUpdateById(String schema, String tableName, Row row); String forUpdateByQuery(QueryWrapper queryWrapper, Row data); - String forUpdateBatchById(String tableName, List rows); + String forUpdateBatchById(String schema, String tableName, List rows); - String forSelectOneById(String tableName, String[] primaryKeys, Object[] primaryValues); + String forSelectOneById(String schema, String tableName, String[] primaryKeys, Object[] primaryValues); String forSelectByQuery(QueryWrapper queryWrapper); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java index 61c70e3b..2ec03e20 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java @@ -66,7 +66,7 @@ public class CommonsDialectImpl implements IDialect { } @Override - public String forInsertRow(String tableName, Row row) { + public String forInsertRow(String schema, String tableName, Row row) { StringBuilder fields = new StringBuilder(); StringBuilder questions = new StringBuilder(); @@ -81,16 +81,20 @@ public class CommonsDialectImpl implements IDialect { } index++; } - - String sql = "INSERT INTO " + wrap(getRealTable(tableName)) + - "(" + fields + ") VALUES " + - "(" + questions + ")"; - return sql; + StringBuilder sql = new StringBuilder(); + sql.append("INSERT INTO "); + if (StringUtil.isNotBlank(schema)) { + sql.append(wrap(getRealSchema(schema))).append("."); + } + sql.append(wrap(getRealTable(tableName))); + sql.append("(").append(fields).append(") "); + sql.append(" VALUES ").append("(").append(questions).append(")"); + return sql.toString(); } @Override - public String forInsertBatchWithFirstRowColumns(String tableName, List rows) { + public String forInsertBatchWithFirstRowColumns(String schema, String tableName, List rows) { StringBuilder fields = new StringBuilder(); StringBuilder questions = new StringBuilder(); @@ -112,9 +116,16 @@ public class CommonsDialectImpl implements IDialect { } } - String sql = "INSERT INTO " + wrap(getRealTable(tableName)) + - "(" + fields + ") VALUES " + questions; - return sql; + + StringBuilder sql = new StringBuilder(); + sql.append("INSERT INTO "); + if (StringUtil.isNotBlank(schema)) { + sql.append(wrap(getRealSchema(schema))).append("."); + } + sql.append(wrap(getRealTable(tableName))); + sql.append(" (").append(fields).append(") "); + sql.append(" VALUES ").append(questions); + return sql.toString(); } @@ -181,13 +192,17 @@ public class CommonsDialectImpl implements IDialect { } @Override - public String forUpdateById(String tableName, Row row) { + public String forUpdateById(String schema,String tableName, Row row) { StringBuilder sql = new StringBuilder(); Set modifyAttrs = row.obtainModifyAttrs(); String[] primaryKeys = RowCPI.obtainsPrimaryKeyStrings(row); - sql.append("UPDATE ").append(wrap(getRealTable(tableName))).append(" SET "); + sql.append("UPDATE "); + if (StringUtil.isNotBlank(schema)) { + sql.append(wrap(getRealSchema(schema))).append("."); + } + sql.append(wrap(getRealTable(tableName))).append(" SET "); int index = 0; for (Map.Entry e : row.entrySet()) { String colName = e.getKey(); @@ -221,8 +236,10 @@ public class CommonsDialectImpl implements IDialect { throw FlexExceptions.wrap("update sql must need 1 table."); } - String tableName = queryTables.get(0).getName(); - sql.append("UPDATE ").append(wrap(getRealTable(tableName))).append(" SET "); + //fix: support schema + QueryTable queryTable = queryTables.get(0); +// String tableName = queryTables.get(0).getName(); + sql.append("UPDATE ").append(queryTable.toSql(this)).append(" SET "); int index = 0; for (String modifyAttr : modifyAttrs) { if (index > 0) { @@ -241,21 +258,24 @@ public class CommonsDialectImpl implements IDialect { } @Override - public String forUpdateBatchById(String tableName, List rows) { + public String forUpdateBatchById(String schema,String tableName, List rows) { if (rows.size() == 1) { - return forUpdateById(tableName, rows.get(0)); + return forUpdateById(schema, tableName, rows.get(0)); } StringBuilder sql = new StringBuilder(); for (Row row : rows) { - sql.append(forUpdateById(tableName, row)).append("; "); + sql.append(forUpdateById(schema, tableName, row)).append("; "); } return sql.toString(); } @Override - public String forSelectOneById(String tableName, String[] primaryKeys, Object[] primaryValues) { + public String forSelectOneById(String schema, String tableName, String[] primaryKeys, Object[] primaryValues) { StringBuilder sql = new StringBuilder("SELECT * FROM "); + if (StringUtil.isNotBlank(schema)) { + sql.append(wrap(getRealSchema(schema))).append("."); + } sql.append(wrap(getRealTable(tableName))).append(" WHERE "); for (int i = 0; i < primaryKeys.length; i++) { if (i > 0) { diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/ProviderUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/ProviderUtil.java index 0e56bb28..080701d9 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/ProviderUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/ProviderUtil.java @@ -40,6 +40,10 @@ class ProviderUtil { params.put(FlexConsts.SQL_ARGS, args); } + public static String getSchemaName(Map params) { + Object schemaNameObj = params.get(FlexConsts.SCHEMA_NAME); + return schemaNameObj != null ? schemaNameObj.toString().trim() : null; + } public static String getTableName(Map params) { Object tableNameObj = params.get(FlexConsts.TABLE_NAME); return tableNameObj != null ? tableNameObj.toString().trim() : null; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java index 06aa61e2..d58f5db1 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java @@ -59,13 +59,14 @@ public class RowSqlProvider { * * @param params * @return sql - * @see RowMapper#insert(String, Row) + * @see RowMapper#insert(String,String, Row) */ public static String insert(Map params) { String tableName = ProviderUtil.getTableName(params); + String schema = ProviderUtil.getSchemaName(params); Row row = ProviderUtil.getRow(params); ProviderUtil.setSqlArgs(params, RowCPI.obtainModifyValues(row)); - return DialectFactory.getDialect().forInsertRow(tableName, row); + return DialectFactory.getDialect().forInsertRow(schema,tableName, row); } /** @@ -73,10 +74,11 @@ public class RowSqlProvider { * * @param params * @return sql - * @see RowMapper#insertBatchWithFirstRowColumns(String, List) + * @see RowMapper#insertBatchWithFirstRowColumns(String, String, List) */ public static String insertBatchWithFirstRowColumns(Map params) { String tableName = ProviderUtil.getTableName(params); + String schema = ProviderUtil.getSchemaName(params); List rows = ProviderUtil.getRows(params); if (rows == null || rows.isEmpty()) { throw FlexExceptions.wrap("rows can not be null or empty."); @@ -95,7 +97,7 @@ public class RowSqlProvider { ProviderUtil.setSqlArgs(params, values); //sql: INSERT INTO `tb_table`(`name`, `sex`) VALUES (?, ?),(?, ?),(?, ?) - return DialectFactory.getDialect().forInsertBatchWithFirstRowColumns(tableName, rows); + return DialectFactory.getDialect().forInsertBatchWithFirstRowColumns(schema,tableName, rows); } /** @@ -103,9 +105,10 @@ public class RowSqlProvider { * * @param params * @return sql - * @see RowMapper#deleteById(String, String, Object) + * @see RowMapper#deleteById(String,String, String, Object) */ public static String deleteById(Map params) { + String schema = ProviderUtil.getSchemaName(params); String tableName = ProviderUtil.getTableName(params); String[] primaryKeys = ProviderUtil.getPrimaryKeys(params); Object[] primaryValues = ProviderUtil.getPrimaryValues(params); @@ -116,7 +119,7 @@ public class RowSqlProvider { ProviderUtil.setSqlArgs(params, primaryValues); } - return DialectFactory.getDialect().forDeleteById(null, tableName, primaryKeys); + return DialectFactory.getDialect().forDeleteById(schema, tableName, primaryKeys); } /** @@ -124,15 +127,16 @@ public class RowSqlProvider { * * @param params * @return sql - * @see RowMapper#deleteBatchByIds(String, String, Collection) + * @see RowMapper#deleteBatchByIds(String, String, String, Collection) */ public static String deleteBatchByIds(Map params) { + String schema = ProviderUtil.getSchemaName(params); String tableName = ProviderUtil.getTableName(params); String[] primaryKeys = ProviderUtil.getPrimaryKeys(params); Object[] primaryValues = ProviderUtil.getPrimaryValues(params); ProviderUtil.setSqlArgs(params, primaryValues); - return DialectFactory.getDialect().forDeleteBatchByIds(null, tableName, primaryKeys, primaryValues); + return DialectFactory.getDialect().forDeleteBatchByIds(schema, tableName, primaryKeys, primaryValues); } @@ -141,12 +145,13 @@ public class RowSqlProvider { * * @param params * @return sql - * @see RowMapper#deleteByQuery(String, QueryWrapper) + * @see RowMapper#deleteByQuery(String,String, QueryWrapper) */ public static String deleteByQuery(Map params) { + String schema = ProviderUtil.getSchemaName(params); String tableName = ProviderUtil.getTableName(params); QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params); - CPI.setFromIfNecessary(queryWrapper, tableName); + CPI.setFromIfNecessary(queryWrapper, schema,tableName); Object[] valueArray = CPI.getValueArray(queryWrapper); ProviderUtil.setSqlArgs(params, valueArray); @@ -159,13 +164,14 @@ public class RowSqlProvider { * * @param params * @return sql - * @see RowMapper#updateById(String, Row) + * @see RowMapper#updateById(String, String, Row) */ public static String updateById(Map params) { + String schema = ProviderUtil.getSchemaName(params); String tableName = ProviderUtil.getTableName(params); Row row = ProviderUtil.getRow(params); ProviderUtil.setSqlArgs(params, RowCPI.obtainAllModifyValues(row)); - return DialectFactory.getDialect().forUpdateById(tableName, row); + return DialectFactory.getDialect().forUpdateById(schema, tableName, row); } @@ -174,14 +180,15 @@ public class RowSqlProvider { * * @param params * @return sql - * @see RowMapper#updateByQuery(String, Row, QueryWrapper) + * @see RowMapper#updateByQuery(String, String, Row, QueryWrapper) */ public static String updateByQuery(Map params) { + String schema = ProviderUtil.getSchemaName(params); String tableName = ProviderUtil.getTableName(params); Row data = ProviderUtil.getRow(params); QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params); - CPI.setFromIfNecessary(queryWrapper, tableName); + CPI.setFromIfNecessary(queryWrapper,schema, tableName); Object[] modifyValues = RowCPI.obtainModifyValues(data); Object[] valueArray = CPI.getValueArray(queryWrapper); @@ -198,9 +205,10 @@ public class RowSqlProvider { * * @param params * @return sql - * @see RowMapper#updateBatchById(String, List) + * @see RowMapper#updateBatchById(String, String, List) */ public static String updateBatchById(Map params) { + String schema = ProviderUtil.getSchemaName(params); String tableName = ProviderUtil.getTableName(params); List rows = ProviderUtil.getRows(params); if (CollectionUtil.isEmpty(rows)) { @@ -212,7 +220,7 @@ public class RowSqlProvider { values = ArrayUtil.concat(values, RowCPI.obtainAllModifyValues(row)); } ProviderUtil.setSqlArgs(params, values); - return DialectFactory.getDialect().forUpdateBatchById(tableName, rows); + return DialectFactory.getDialect().forUpdateBatchById(schema,tableName, rows); } /** @@ -250,12 +258,12 @@ public class RowSqlProvider { * * @param params * @return sql - * @see RowMapper#updateNumberAddByQuery(String, String, Number, QueryWrapper) + * @see RowMapper#updateNumberAddByQuery(String, String, String, Number, QueryWrapper) */ public static String updateNumberAddByQuery(Map params) { QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params); - + String schema = ProviderUtil.getSchemaName(params); String tableName = ProviderUtil.getTableName(params); String fieldName = ProviderUtil.getFieldName(params); Number value = (Number) ProviderUtil.getValue(params); @@ -265,7 +273,7 @@ public class RowSqlProvider { ProviderUtil.setSqlArgs(params, queryParams); - return DialectFactory.getDialect().forUpdateNumberAddByQuery(null, tableName, fieldName, value, queryWrapper); + return DialectFactory.getDialect().forUpdateNumberAddByQuery(schema, tableName, fieldName, value, queryWrapper); } @@ -274,16 +282,17 @@ public class RowSqlProvider { * * @param params * @return sql - * @see RowMapper#selectOneById(String, String, Object) + * @see RowMapper#selectOneById(String, String, String, Object) */ public static String selectOneById(Map params) { + String schema = ProviderUtil.getSchemaName(params); String tableName = ProviderUtil.getTableName(params); String[] primaryKeys = ProviderUtil.getPrimaryKeys(params); Object[] primaryValues = ProviderUtil.getPrimaryValues(params); ProviderUtil.setSqlArgs(params, primaryValues); - return DialectFactory.getDialect().forSelectOneById(tableName, primaryKeys, primaryValues); + return DialectFactory.getDialect().forSelectOneById(schema,tableName, primaryKeys, primaryValues); } @@ -292,12 +301,13 @@ public class RowSqlProvider { * * @param params * @return sql - * @see RowMapper#selectListByQuery(String, QueryWrapper) + * @see RowMapper#selectListByQuery(String, String, QueryWrapper) */ public static String selectListByQuery(Map params) { + String schema = ProviderUtil.getSchemaName(params); String tableName = ProviderUtil.getTableName(params); QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params); - CPI.setFromIfNecessary(queryWrapper, tableName); + CPI.setFromIfNecessary(queryWrapper,schema, tableName); Object[] valueArray = CPI.getValueArray(queryWrapper); ProviderUtil.setSqlArgs(params, valueArray); @@ -311,13 +321,14 @@ public class RowSqlProvider { * * @param params * @return sql - * @see RowMapper#selectCountByQuery(String, QueryWrapper) + * @see RowMapper#selectCountByQuery(String, String, QueryWrapper) */ public static String selectObjectByQuery(Map params) { + String schema = ProviderUtil.getSchemaName(params); String tableName = ProviderUtil.getTableName(params); QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params); - CPI.setFromIfNecessary(queryWrapper, tableName); + CPI.setFromIfNecessary(queryWrapper,schema,tableName); Object[] valueArray = CPI.getValueArray(queryWrapper); ProviderUtil.setSqlArgs(params, valueArray); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java index 41fa7f58..cfb6ed8b 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java @@ -62,13 +62,23 @@ public class Db { /** - * 网 tableName 插入一条 row 数据 + * 往 schema.tableName 插入一条 row 数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param row 数据 + */ + public static int insert(String schema,String tableName, Row row) { + return invoker().insert(schema,tableName, row); + } + /** + * 往 tableName 插入一条 row 数据 * * @param tableName 表名 * @param row 数据 */ public static int insert(String tableName, Row row) { - return invoker().insert(tableName, row); + return invoker().insert(null,tableName, row); } @@ -83,6 +93,17 @@ public class Db { } + /** + * 批量插入数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param rows 数据 + */ + public static int[] insertBatch(String schema,String tableName, Collection rows) { + return insertBatch(schema,tableName, rows, rows.size()); + } + /** * 批量插入数据 * @@ -90,9 +111,24 @@ public class Db { * @param rows 数据 */ public static int[] insertBatch(String tableName, Collection rows) { - return insertBatch(tableName, rows, rows.size()); + return insertBatch(null,tableName, rows, rows.size()); } + /** + * 批量插入数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param rows 数据 + * @param batchSize 每次提交的数据量 + */ + public static int[] insertBatch(String schema,String tableName, Collection rows, int batchSize) { + List list = CollectionUtil.toList(rows); + return executeBatch(rows.size(), batchSize, RowMapper.class, (mapper, index) -> { + Row row = list.get(index); + mapper.insert(schema,tableName, row); + }); + } /** * 批量插入数据 * @@ -104,18 +140,29 @@ public class Db { List list = CollectionUtil.toList(rows); return executeBatch(rows.size(), batchSize, RowMapper.class, (mapper, index) -> { Row row = list.get(index); - mapper.insert(tableName, row); + mapper.insert(null,tableName, row); }); } /** - * 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String, Collection, int)} 高 + * 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String ,String, Collection, int)} 高 + * + * @param schema 模式 + * @param tableName 表名 + * @param rows 数据 + */ + public static int insertBatchWithFirstRowColumns(String schema,String tableName, List rows) { + return invoker().insertBatchWithFirstRowColumns(schema,tableName, rows); + } + + /** + * 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String ,String, Collection, int)} 高 * * @param tableName 表名 * @param rows 数据 */ public static int insertBatchWithFirstRowColumns(String tableName, List rows) { - return invoker().insertBatchWithFirstRowColumns(tableName, rows); + return invoker().insertBatchWithFirstRowColumns(null,tableName, rows); } /** @@ -128,6 +175,17 @@ public class Db { return invoker().deleteBySql(sql, args); } + /** + * 根据主键来删除数据,其中 row 是通过 {@link Row#ofKey(RowKey, Object)} 来进行构建的 + * + * @param schema 模式 + * @param tableName 表名 + * @param row 主键 和 id值 + */ + public static int deleteById(String schema,String tableName, Row row) { + return invoker().deleteById(schema,tableName, row); + } + /** * 根据主键来删除数据,其中 row 是通过 {@link Row#ofKey(RowKey, Object)} 来进行构建的 * @@ -135,7 +193,20 @@ public class Db { * @param row 主键 和 id值 */ public static int deleteById(String tableName, Row row) { - return invoker().deleteById(tableName, row); + return invoker().deleteById(null,tableName, row); + } + + + /** + * 根据主键来删除 1 条数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param primaryKey 主键字段名称 + * @param id 主键值 + */ + public static int deleteById(String schema,String tableName, String primaryKey, Object id) { + return invoker().deleteById(schema,tableName, primaryKey, id); } /** @@ -146,7 +217,20 @@ public class Db { * @param id 主键值 */ public static int deleteById(String tableName, String primaryKey, Object id) { - return invoker().deleteById(tableName, primaryKey, id); + return invoker().deleteById(null,tableName, primaryKey, id); + } + + + /** + * 根据 id 集合来批量删除数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param primaryKey 主键字段名称 + * @param ids id 集合 + */ + public static int deleteBatchByIds(String schema,String tableName, String primaryKey, Collection ids) { + return invoker().deleteBatchByIds(schema,tableName, primaryKey, ids); } /** @@ -157,7 +241,18 @@ public class Db { * @param ids id 集合 */ public static int deleteBatchByIds(String tableName, String primaryKey, Collection ids) { - return invoker().deleteBatchByIds(tableName, primaryKey, ids); + return invoker().deleteBatchByIds(null,tableName, primaryKey, ids); + } + + /** + * 根据 map 构建的 where 条件来删除数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param whereColumns where 条件 + */ + public static int deleteByMap(String schema,String tableName, Map whereColumns) { + return invoker().deleteByQuery(schema,tableName, new QueryWrapper().where(whereColumns)); } /** @@ -167,9 +262,19 @@ public class Db { * @param whereColumns where 条件 */ public static int deleteByMap(String tableName, Map whereColumns) { - return invoker().deleteByQuery(tableName, new QueryWrapper().where(whereColumns)); + return invoker().deleteByQuery(null,tableName, new QueryWrapper().where(whereColumns)); } + /** + * 根据 condition 条件删除数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param condition 条件内容 + */ + public static int deleteByCondition(String schema,String tableName, QueryCondition condition) { + return invoker().deleteByQuery(schema,tableName, new QueryWrapper().where(condition)); + } /** * 根据 condition 条件删除数据 @@ -178,7 +283,19 @@ public class Db { * @param condition 条件内容 */ public static int deleteByCondition(String tableName, QueryCondition condition) { - return invoker().deleteByQuery(tableName, new QueryWrapper().where(condition)); + return invoker().deleteByQuery(null,tableName, new QueryWrapper().where(condition)); + } + + + /** + * 根据 query 构建的条件来删除数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param queryWrapper query + */ + public static int deleteByQuery(String schema,String tableName, QueryWrapper queryWrapper) { + return invoker().deleteByQuery(schema,tableName, queryWrapper); } /** @@ -188,10 +305,9 @@ public class Db { * @param queryWrapper query */ public static int deleteByQuery(String tableName, QueryWrapper queryWrapper) { - return invoker().deleteByQuery(tableName, queryWrapper); + return invoker().deleteByQuery(null,tableName, queryWrapper); } - /** * 根据原生 sql 来更新数据 * @@ -218,13 +334,38 @@ public class Db { /** * 根据 id 来更新数据 * - * @param tableName 表情 + * @param schema 模式 + * @param tableName 表名 + * @param row id 及其内容 + */ + public static int updateById(String schema,String tableName, Row row) { + return invoker().updateById(schema,tableName, row); + } + + + /** + * 根据 id 来更新数据 + * + * @param tableName 表名 * @param row id 及其内容 */ public static int updateById(String tableName, Row row) { - return invoker().updateById(tableName, row); + return invoker().updateById(null,tableName, row); } + /** + * 根据 map 构建的条件来更新数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param data 数据内容 + * @param whereColumns where 条件 + */ + public static int updateByMap(String schema,String tableName, Row data, Map whereColumns) { + return invoker().updateByQuery(schema,tableName, data, new QueryWrapper().where(whereColumns)); + } + + /** * 根据 map 构建的条件来更新数据 * @@ -233,7 +374,19 @@ public class Db { * @param whereColumns where 条件 */ public static int updateByMap(String tableName, Row data, Map whereColumns) { - return invoker().updateByQuery(tableName, data, new QueryWrapper().where(whereColumns)); + return invoker().updateByQuery(null,tableName, data, new QueryWrapper().where(whereColumns)); + } + + /** + * 根据 condition 来更新数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param data 数据 + * @param condition 条件 + */ + public static int updateByCondition(String schema,String tableName, Row data, QueryCondition condition) { + return invoker().updateByQuery(schema,tableName, data, new QueryWrapper().where(condition)); } /** @@ -244,10 +397,22 @@ public class Db { * @param condition 条件 */ public static int updateByCondition(String tableName, Row data, QueryCondition condition) { - return invoker().updateByQuery(tableName, data, new QueryWrapper().where(condition)); + return invoker().updateByQuery(null,tableName, data, new QueryWrapper().where(condition)); } + /** + * 根据 query 构建的条件来更新数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param data 数据内容 + * @param queryWrapper queryWrapper 条件 + */ + public static int updateByQuery(String schema,String tableName, Row data, QueryWrapper queryWrapper) { + return invoker().updateByQuery(schema,tableName, data, queryWrapper); + } + /** * 根据 query 构建的条件来更新数据 * @@ -256,10 +421,21 @@ public class Db { * @param queryWrapper queryWrapper 条件 */ public static int updateByQuery(String tableName, Row data, QueryWrapper queryWrapper) { - return invoker().updateByQuery(tableName, data, queryWrapper); + return invoker().updateByQuery(null,tableName, data, queryWrapper); } + /** + * 根据主键来批量更新数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param rows 还有主键的数据 + */ + public static int updateBatchById(String schema,String tableName, List rows) { + return invoker().updateBatchById(schema,tableName, rows); + } + /** * 根据主键来批量更新数据 * @@ -267,7 +443,7 @@ public class Db { * @param rows 还有主键的数据 */ public static int updateBatchById(String tableName, List rows) { - return invoker().updateBatchById(tableName, rows); + return invoker().updateBatchById(null,tableName, rows); } @@ -298,8 +474,32 @@ public class Db { } + /** + * 通过 update schema.table set field = field + 1 where ... 的这种方向更新数据库某个字段内容 + * + * + * @param schema 模式 + * @param tableName 表名 + * @param fieldName 字段名 + * @param value 递增值 + * @param queryWrapper 条件 + * @return 受影响行数 + */ + public static int updateNumberAddByQuery(String schema,String tableName, String fieldName, Number value, QueryWrapper queryWrapper){ + return invoker().updateNumberAddByQuery(schema,tableName, fieldName, value, queryWrapper); + } + + /** + * 通过 update table set field = field + 1 where ... 的这种方向更新数据库某个字段内容 + * + * @param tableName + * @param fieldName + * @param value + * @param queryWrapper + * @return + */ public static int updateNumberAddByQuery(String tableName, String fieldName, Number value, QueryWrapper queryWrapper){ - return invoker().updateNumberAddByQuery(tableName, fieldName, value, queryWrapper); + return invoker().updateNumberAddByQuery(null,tableName, fieldName, value, queryWrapper); } @@ -328,6 +528,17 @@ public class Db { } + /** + * 根据 id 来查询 1 条数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param row 主键和 id 值 + */ + public static Row selectOneById(String schema,String tableName, Row row) { + return invoker().selectOneById(schema,tableName, row); + } + /** * 根据 id 来查询 1 条数据 * @@ -335,10 +546,22 @@ public class Db { * @param row 主键和 id 值 */ public static Row selectOneById(String tableName, Row row) { - return invoker().selectOneById(tableName, row); + return invoker().selectOneById(null,tableName, row); } + /** + * 根据主键来查询 1 条数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param primaryKey 主键字段名称 + * @param id 主键值 + */ + public static Row selectOneById(String schema,String tableName, String primaryKey, Object id) { + return invoker().selectOneById(schema,tableName, primaryKey, id); + } + /** * 根据主键来查询 1 条数据 * @@ -347,9 +570,21 @@ public class Db { * @param id 主键值 */ public static Row selectOneById(String tableName, String primaryKey, Object id) { - return invoker().selectOneById(tableName, primaryKey, id); + return invoker().selectOneById(null,tableName, primaryKey, id); } + /** + * 根据 map 来查询 1 条数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param whereColumns where条件 + */ + public static Row selectOneByMap(String schema,String tableName, Map whereColumns) { + return invoker().selectOneByQuery(schema,tableName, new QueryWrapper().where(whereColumns)); + } + + /** * 根据 map 来查询 1 条数据 @@ -358,7 +593,18 @@ public class Db { * @param whereColumns where条件 */ public static Row selectOneByMap(String tableName, Map whereColumns) { - return invoker().selectOneByQuery(tableName, new QueryWrapper().where(whereColumns)); + return invoker().selectOneByQuery(null,tableName, new QueryWrapper().where(whereColumns)); + } + + /** + * 根据 condition 来查询数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param condition 条件 + */ + public static Row selectOneByCondition(String schema,String tableName, QueryCondition condition) { + return invoker().selectOneByQuery(schema,tableName, new QueryWrapper().where(condition)); } /** @@ -368,10 +614,21 @@ public class Db { * @param condition 条件 */ public static Row selectOneByCondition(String tableName, QueryCondition condition) { - return invoker().selectOneByQuery(tableName, new QueryWrapper().where(condition)); + return invoker().selectOneByQuery(null,tableName, new QueryWrapper().where(condition)); } + /** + * 根据 queryWrapper 来查询 1 条数据 + * + * @param schema 模式 + * @param tableName 表名 + * @param queryWrapper queryWrapper + */ + public static Row selectOneByQuery(String schema,String tableName, QueryWrapper queryWrapper) { + return invoker().selectOneByQuery(schema,tableName, queryWrapper); + } + /** * 根据 queryWrapper 来查询 1 条数据 * @@ -379,7 +636,7 @@ public class Db { * @param queryWrapper queryWrapper */ public static Row selectOneByQuery(String tableName, QueryWrapper queryWrapper) { - return invoker().selectOneByQuery(tableName, queryWrapper); + return invoker().selectOneByQuery(null,tableName, queryWrapper); } @@ -393,7 +650,7 @@ public class Db { if (queryTables == null || queryTables.isEmpty()) { throw FlexExceptions.wrap("table must not be null or empty in Db.selectOneByQuery"); } - return invoker().selectOneByQuery(null, queryWrapper); + return invoker().selectOneByQuery(null,null, queryWrapper); } @@ -408,6 +665,17 @@ public class Db { } + /** + * 通过 map 构建的条件来查询数据列表 + * + * @param schema 模式 + * @param tableName 表名 + * @param whereColumns where 条件 + */ + public static List selectListByMap(String schema,String tableName, Map whereColumns) { + return invoker().selectListByQuery(schema,tableName, new QueryWrapper().where(whereColumns)); + } + /** * 通过 map 构建的条件来查询数据列表 * @@ -415,7 +683,20 @@ public class Db { * @param whereColumns where 条件 */ public static List selectListByMap(String tableName, Map whereColumns) { - return invoker().selectListByQuery(tableName, new QueryWrapper().where(whereColumns)); + return invoker().selectListByQuery(null,tableName, new QueryWrapper().where(whereColumns)); + } + + + /** + * 根据 map 构建的条件来查询数据列表 + * + * @param schema 模式 + * @param tableName 表名 + * @param whereColumns 条件 + * @param count 数据量 + */ + public static List selectListByMap(String schema,String tableName, Map whereColumns, int count) { + return invoker().selectListByQuery(schema,tableName, new QueryWrapper().where(whereColumns).limit(count)); } /** @@ -426,7 +707,19 @@ public class Db { * @param count 数据量 */ public static List selectListByMap(String tableName, Map whereColumns, int count) { - return invoker().selectListByQuery(tableName, new QueryWrapper().where(whereColumns).limit(count)); + return invoker().selectListByQuery(null,tableName, new QueryWrapper().where(whereColumns).limit(count)); + } + + + /** + * 通过 condition 条件来查询数据列表 + * + * @param schema 模式 + * @param tableName 表名 + * @param condition where 条件 + */ + public static List selectListByCondition(String schema,String tableName, QueryCondition condition) { + return invoker().selectListByQuery(schema,tableName, new QueryWrapper().where(condition)); } @@ -437,7 +730,18 @@ public class Db { * @param condition where 条件 */ public static List selectListByCondition(String tableName, QueryCondition condition) { - return invoker().selectListByQuery(tableName, new QueryWrapper().where(condition)); + return invoker().selectListByQuery(null,tableName, new QueryWrapper().where(condition)); + } + /** + * 根据 condition 条件来查询数据列表 + * + * @param schema 模式 + * @param tableName 表名 + * @param condition 条件 + * @param count 数据量 + */ + public static List selectListByCondition(String schema,String tableName, QueryCondition condition, int count) { + return invoker().selectListByQuery(schema,tableName, new QueryWrapper().where(condition).limit(count)); } /** @@ -448,7 +752,19 @@ public class Db { * @param count 数据量 */ public static List selectListByCondition(String tableName, QueryCondition condition, int count) { - return invoker().selectListByQuery(tableName, new QueryWrapper().where(condition).limit(count)); + return invoker().selectListByQuery(null,tableName, new QueryWrapper().where(condition).limit(count)); + } + + + /** + * 通过 query 来查询数据列表 + * + * @param schema 模式 + * @param tableName 表名 + * @param queryWrapper query 条件 + */ + public static List selectListByQuery(String schema,String tableName, QueryWrapper queryWrapper) { + return invoker().selectListByQuery(schema,tableName, queryWrapper); } @@ -459,7 +775,7 @@ public class Db { * @param queryWrapper query 条件 */ public static List selectListByQuery(String tableName, QueryWrapper queryWrapper) { - return invoker().selectListByQuery(tableName, queryWrapper); + return invoker().selectListByQuery(null,tableName, queryWrapper); } @@ -473,7 +789,17 @@ public class Db { if (queryTables == null || queryTables.isEmpty()) { throw FlexExceptions.wrap("table must not be null or empty in Db.selectListByQuery"); } - return invoker().selectListByQuery(null, queryWrapper); + return invoker().selectListByQuery(null,null, queryWrapper); + } + + /** + * 查询某张表的所有数据 + * + * @param schema 模式 + * @param tableName 表名 + */ + public static List selectAll(String schema,String tableName) { + return invoker().selectAll(schema,tableName); } /** @@ -482,7 +808,7 @@ public class Db { * @param tableName 表名 */ public static List selectAll(String tableName) { - return invoker().selectAll(tableName); + return invoker().selectAll(null,tableName); } /** @@ -496,6 +822,18 @@ public class Db { } + /** + * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列 + * + * @param schema 模式 + * @param tableName 表名 + * @param queryWrapper query 封装 + * @return 数据内容 + */ + public static Object selectObject(String schema,String tableName, QueryWrapper queryWrapper) { + return invoker().selectObjectByQuery(schema,tableName, queryWrapper); + } + /** * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列 * @@ -504,7 +842,7 @@ public class Db { * @return 数据内容 */ public static Object selectObject(String tableName, QueryWrapper queryWrapper) { - return invoker().selectObjectByQuery(tableName, queryWrapper); + return invoker().selectObjectByQuery(null,tableName, queryWrapper); } @@ -515,10 +853,11 @@ public class Db { * @return 数据内容 */ public static Object selectObject(QueryWrapper queryWrapper) { - return invoker().selectObjectByQuery(null, queryWrapper); + return invoker().selectObjectByQuery(null, null, queryWrapper); } + /** * 查询某列内容,数据返回应该有 多行 1 列 * @@ -530,6 +869,18 @@ public class Db { } + /** + * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列 + * + * @param schema 模式 + * @param tableName 表名 + * @param queryWrapper query 封装 + * @return 数据内容 + */ + public static Object selectObjectList(String schema,String tableName, QueryWrapper queryWrapper) { + return invoker().selectObjectListByQuery(schema,tableName, queryWrapper); + } + /** * 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列 * @@ -538,7 +889,7 @@ public class Db { * @return 数据内容 */ public static Object selectObjectList(String tableName, QueryWrapper queryWrapper) { - return invoker().selectObjectListByQuery(tableName, queryWrapper); + return invoker().selectObjectListByQuery(null,tableName, queryWrapper); } @@ -549,7 +900,7 @@ public class Db { * @return 数据内容 */ public static Object selectObjectList(QueryWrapper queryWrapper) { - return invoker().selectObjectListByQuery(null, queryWrapper); + return invoker().selectObjectListByQuery(null, null, queryWrapper); } @@ -565,6 +916,17 @@ public class Db { } + /** + * 根据 condition 条件来查询数量 + * + * @param schema 模式 + * @param tableName 表名 + * @param condition 条件 + */ + public static long selectCountByCondition(String schema,String tableName, QueryCondition condition) { + return invoker().selectCountByQuery(schema,tableName, new QueryWrapper().where(condition)); + } + /** * 根据 condition 条件来查询数量 * @@ -572,10 +934,21 @@ public class Db { * @param condition 条件 */ public static long selectCountByCondition(String tableName, QueryCondition condition) { - return invoker().selectCountByQuery(tableName, new QueryWrapper().where(condition)); + return invoker().selectCountByQuery(null,tableName, new QueryWrapper().where(condition)); } + /** + * 根据 query 构建的条件来查询数据量 + * + * @param schema 模式 + * @param tableName 表名 + * @param queryWrapper query 条件 + */ + public static long selectCountByQuery(String schema,String tableName, QueryWrapper queryWrapper) { + return invoker().selectCountByQuery(schema,tableName, queryWrapper); + } + /** * 根据 query 构建的条件来查询数据量 * @@ -583,7 +956,7 @@ public class Db { * @param queryWrapper query 条件 */ public static long selectCountByQuery(String tableName, QueryWrapper queryWrapper) { - return invoker().selectCountByQuery(tableName, queryWrapper); + return invoker().selectCountByQuery(null,tableName, queryWrapper); } @@ -598,7 +971,21 @@ public class Db { if (queryTables == null || queryTables.isEmpty()) { throw FlexExceptions.wrap("Query tables must not be null or empty in Db.selectCountByQuery"); } - return invoker().selectCountByQuery(null, queryWrapper); + return invoker().selectCountByQuery(null,null, queryWrapper); + } + + + /** + * 分页查询 + * + * @param schema 模式 + * @param tableName 表名 + * @param pageNumber 当前的页码 + * @param pageSize 每页的数据量 + * @param condition 条件 + */ + public static Page paginate(String schema,String tableName, int pageNumber, int pageSize, QueryCondition condition) { + return invoker().paginate(schema,tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition)); } @@ -611,10 +998,24 @@ public class Db { * @param condition 条件 */ public static Page paginate(String tableName, int pageNumber, int pageSize, QueryCondition condition) { - return invoker().paginate(tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition)); + return invoker().paginate(null,tableName, new Page<>(pageNumber, pageSize), QueryWrapper.create().where(condition)); } + /** + * 分页查询 + * + * @param schema 模式 + * @param tableName 表名 + * @param pageNumber 当前的页码 + * @param pageSize 每页的数据量 + * @param totalRow 数据总量 + * @param condition 条件 + */ + public static Page paginate(String schema,String tableName, int pageNumber, int pageSize, int totalRow, QueryCondition condition) { + return invoker().paginate(schema,tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition)); + } + /** * 分页查询 * @@ -625,10 +1026,23 @@ public class Db { * @param condition 条件 */ public static Page paginate(String tableName, int pageNumber, int pageSize, int totalRow, QueryCondition condition) { - return invoker().paginate(tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition)); + return invoker().paginate(null,tableName, new Page<>(pageNumber, pageSize, totalRow), QueryWrapper.create().where(condition)); } + /** + * 分页查询 + * + * @param schema 模式 + * @param tableName 表名 + * @param pageNumber 当前的页码 + * @param pageSize 每页的数据量 + * @param queryWrapper 条件 + */ + public static Page paginate(String schema,String tableName, int pageNumber, int pageSize, QueryWrapper queryWrapper) { + return invoker().paginate(schema,tableName, new Page<>(pageNumber, pageSize), queryWrapper); + } + /** * 分页查询 * @@ -638,10 +1052,24 @@ public class Db { * @param queryWrapper 条件 */ public static Page paginate(String tableName, int pageNumber, int pageSize, QueryWrapper queryWrapper) { - return invoker().paginate(tableName, new Page<>(pageNumber, pageSize), queryWrapper); + return invoker().paginate(null,tableName, new Page<>(pageNumber, pageSize), queryWrapper); } + /** + * 分页查询 + * + * @param schema 模式 + * @param tableName 表名 + * @param pageNumber 当前的页码 + * @param pageSize 每页的数据量 + * @param totalRow 数据总量 + * @param queryWrapper 条件 + */ + public static Page paginate(String schema,String tableName, int pageNumber, int pageSize, int totalRow, QueryWrapper queryWrapper) { + return invoker().paginate(schema,tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper); + } + /** * 分页查询 * @@ -652,10 +1080,22 @@ public class Db { * @param queryWrapper 条件 */ public static Page paginate(String tableName, int pageNumber, int pageSize, int totalRow, QueryWrapper queryWrapper) { - return invoker().paginate(tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper); + return invoker().paginate(null,tableName, new Page<>(pageNumber, pageSize, totalRow), queryWrapper); } + /** + * 分页查询 + * + * @param schema 模式 + * @param tableName 表名 + * @param page page 对象,若 page 有 totalCount 值,则不会再去查询分类的数据总量 + * @param queryWrapper 条件 + */ + public static Page paginate(String schema,String tableName, Page page, QueryWrapper queryWrapper) { + return invoker().paginate(schema,tableName, page, queryWrapper); + } + /** * 分页查询 * @@ -664,7 +1104,7 @@ public class Db { * @param queryWrapper 条件 */ public static Page paginate(String tableName, Page page, QueryWrapper queryWrapper) { - return invoker().paginate(tableName, page, queryWrapper); + return invoker().paginate(null,tableName, page, queryWrapper); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapper.java index 4f69b538..b1cee652 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapper.java @@ -45,7 +45,7 @@ public interface RowMapper { * @see RowSqlProvider#insert(Map) */ @InsertProvider(value = RowSqlProvider.class, method = "insert") - int insert(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROW) Row row); + int insert(@Param(FlexConsts.SCHEMA_NAME) String schema,@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROW) Row row); /** @@ -71,7 +71,7 @@ public interface RowMapper { * @see RowSqlProvider#insertBatchWithFirstRowColumns(Map) */ @InsertProvider(value = RowSqlProvider.class, method = "insertBatchWithFirstRowColumns") - int insertBatchWithFirstRowColumns(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROWS) List rows); + int insertBatchWithFirstRowColumns(@Param(FlexConsts.SCHEMA_NAME) String schema,@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROWS) List rows); /////// delete ///// @@ -89,17 +89,19 @@ public interface RowMapper { /** * 根据 id 删除数据 * + * @param schema 模式 * @param tableName 表名 * @param row id 和 值的数据,可以通过 {@link Row#ofKey(String, Object)} 来创建 * @return 执行影响的行数 */ - default int deleteById(String tableName, Row row) { - return deleteById(tableName, StringUtil.join(",", row.obtainsPrimaryKeyStrings()), row.obtainsPrimaryValues()); + default int deleteById(String schema,String tableName, Row row) { + return deleteById(schema,tableName, StringUtil.join(",", row.obtainsPrimaryKeyStrings()), row.obtainsPrimaryValues()); } /** * 根据 id 删除数据 * + * @param schema 模式 * @param tableName 表名 * @param primaryKey 主键,多个主键用英文逗号隔开 * @param id 数据,多个主键时传入数组,例如 new Object[]{1,2} @@ -107,12 +109,13 @@ public interface RowMapper { * @see RowSqlProvider#deleteById(Map) */ @DeleteProvider(value = RowSqlProvider.class, method = "deleteById") - int deleteById(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.PRIMARY_KEY) String primaryKey, @Param(FlexConsts.PRIMARY_VALUE) Object id); + int deleteById(@Param(FlexConsts.SCHEMA_NAME) String schema,@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.PRIMARY_KEY) String primaryKey, @Param(FlexConsts.PRIMARY_VALUE) Object id); /** * 根据 多个 id 值删除多条数据 * + * @param schema 模式 * @param tableName 表名 * @param primaryKey 主键 * @param ids id 的集合 @@ -120,19 +123,20 @@ public interface RowMapper { * @see RowSqlProvider#deleteBatchByIds(Map) */ @DeleteProvider(value = RowSqlProvider.class, method = "deleteBatchByIds") - int deleteBatchByIds(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.PRIMARY_KEY) String primaryKey, @Param(FlexConsts.PRIMARY_VALUE) Collection ids); + int deleteBatchByIds(@Param(FlexConsts.SCHEMA_NAME) String schema,@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.PRIMARY_KEY) String primaryKey, @Param(FlexConsts.PRIMARY_VALUE) Collection ids); /** * 根据 queryWrapper 构建 where 条件来删除数据 * + * @param schema 模式 * @param tableName 表名 * @param queryWrapper queryWrapper * @return 执行影响的行数 * @see RowSqlProvider#deleteByQuery(Map) */ @DeleteProvider(value = RowSqlProvider.class, method = "deleteByQuery") - int deleteByQuery(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper); + int deleteByQuery(@Param(FlexConsts.SCHEMA_NAME) String schema,@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper); ////////update //// @@ -151,18 +155,20 @@ public interface RowMapper { /** * 根据主键来更新数据 * + * @param schema 模式 * @param tableName 表名 * @param row 数据,其必须包含主键数据列名和值 * @return 执行影响的行数 * @see RowSqlProvider#updateById(Map) */ @UpdateProvider(value = RowSqlProvider.class, method = "updateById") - int updateById(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROW) Row row); + int updateById(@Param(FlexConsts.SCHEMA_NAME) String schema,@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROW) Row row); /** * 根据 queryWrapper 来构建 where 条件更新数据 * + * @param schema 模式 * @param tableName 表名 * @param data 更新数据 * @param queryWrapper queryWrapper @@ -170,7 +176,7 @@ public interface RowMapper { * @see RowSqlProvider#updateByQuery(Map) */ @UpdateProvider(value = RowSqlProvider.class, method = "updateByQuery") - int updateByQuery(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROW) Row data, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper); + int updateByQuery(@Param(FlexConsts.SCHEMA_NAME) String schema,@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROW) Row data, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper); /** @@ -179,13 +185,14 @@ public interface RowMapper { * 1、此方法需要在 mysql 等链接配置需要开启 allowMultiQueries=true * 2、更新成功返回的结果也可能为 0 * + * @param schema 模式 * @param tableName 表名 * @param rows 数据,其必须包含主键数据列名和值 * @return 执行影响的行数 * @see RowSqlProvider#updateBatchById(Map) */ @UpdateProvider(value = RowSqlProvider.class, method = "updateBatchById") - int updateBatchById(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROWS) List rows); + int updateBatchById(@Param(FlexConsts.SCHEMA_NAME) String schema,@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROWS) List rows); /** @@ -208,7 +215,7 @@ public interface RowMapper { * @see RowSqlProvider#updateNumberAddByQuery(Map) */ @UpdateProvider(type = RowSqlProvider.class, method = "updateNumberAddByQuery") - int updateNumberAddByQuery(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.FIELD_NAME) String fieldName + int updateNumberAddByQuery(@Param(FlexConsts.SCHEMA_NAME) String schema,@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.FIELD_NAME) String fieldName , @Param(FlexConsts.VALUE) Number value, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper); @@ -237,18 +244,18 @@ public interface RowMapper { /** * 通过主键来查询数据 * + * @param schema 模式 * @param tableName 表名 * @param row 主键和ID的描述,通过 {@link Row#ofKey(String, Object)} 来进行构建 * @return 返回一条数据,或者 null */ - default Row selectOneById(String tableName, Row row) { - return selectOneById(tableName, StringUtil.join(",", row.obtainsPrimaryKeyStrings()), row.obtainsPrimaryValues()); + default Row selectOneById(String schema,String tableName, Row row) { + return selectOneById(schema,tableName, StringUtil.join(",", row.obtainsPrimaryKeyStrings()), row.obtainsPrimaryValues()); } - /** * 根据主键来查询数据 - * + * @param schema 模式 * @param tableName 表名 * @param primaryKey 主键 * @param id id 值 @@ -256,18 +263,19 @@ public interface RowMapper { * @see RowSqlProvider#selectOneById(Map) */ @SelectProvider(value = RowSqlProvider.class, method = "selectOneById") - Row selectOneById(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.PRIMARY_KEY) String primaryKey, @Param(FlexConsts.PRIMARY_VALUE) Object id); + Row selectOneById(@Param(FlexConsts.SCHEMA_NAME) String schema,@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.PRIMARY_KEY) String primaryKey, @Param(FlexConsts.PRIMARY_VALUE) Object id); /** * 根据 queryWrapper 来查询 1 条数据 * + * @param schema 模式 * @param tableName 表名 * @param queryWrapper queryWrapper * @return row or null */ - default Row selectOneByQuery(String tableName, QueryWrapper queryWrapper) { - List rows = selectListByQuery(tableName, queryWrapper.limit(1)); + default Row selectOneByQuery(String schema,String tableName, QueryWrapper queryWrapper) { + List rows = selectListByQuery(schema,tableName, queryWrapper.limit(1)); if (rows == null || rows.isEmpty()) { return null; } else { @@ -289,23 +297,25 @@ public interface RowMapper { /** * 根据 queryWrapper 来查询一个 row 列表 * + * @param schema 模式 * @param tableName 表名 * @param queryWrapper queryWrapper * @return row 列表 * @see RowSqlProvider#selectListByQuery(Map) */ @SelectProvider(value = RowSqlProvider.class, method = "selectListByQuery") - List selectListByQuery(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper); + List selectListByQuery(@Param(FlexConsts.SCHEMA_NAME) String schema,@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper); /** * 查询某张表的全部数据 * + * @param schema 模式 * @param tableName 表名 * @return row 列表 */ - default List selectAll(String tableName) { - return selectListByQuery(tableName, QueryWrapper.create()); + default List selectAll(String schema,String tableName) { + return selectListByQuery(schema,tableName, QueryWrapper.create()); } /** @@ -354,13 +364,14 @@ public interface RowMapper { * 根据 queryWrapper 1 条数据 * queryWrapper 执行的结果应该只有 1 列,例如 QueryWrapper.create().select(ACCOUNT.id).where... * + * @param schema 模式 * @param tableName 表名 * @param queryWrapper queryWrapper * @return 数据 */ - default Object selectObjectByQuery(String tableName, QueryWrapper queryWrapper) { + default Object selectObjectByQuery(String schema,String tableName, QueryWrapper queryWrapper) { queryWrapper.limit(1); - List objects = selectObjectListByQuery(tableName, queryWrapper); + List objects = selectObjectListByQuery(schema,tableName, queryWrapper); if (objects == null || objects.isEmpty()) { return null; } @@ -377,23 +388,24 @@ public interface RowMapper { * @see RowSqlProvider#selectObjectByQuery(Map) */ @SelectProvider(type = RowSqlProvider.class, method = "selectObjectByQuery") - List selectObjectListByQuery(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper); + List selectObjectListByQuery(@Param(FlexConsts.SCHEMA_NAME) String schema,@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.QUERY) QueryWrapper queryWrapper); /** * 查询数据量 * + * @param schema 模式 * @param tableName 表名 * @param queryWrapper 查询包装器 * @return 数据量 */ - default long selectCountByQuery(String tableName, QueryWrapper queryWrapper) { + default long selectCountByQuery(String schema,String tableName, QueryWrapper queryWrapper) { List selectColumns = CPI.getSelectColumns(queryWrapper); if (CollectionUtil.isEmpty(selectColumns)) { queryWrapper.select(count()); } - List objects = selectObjectListByQuery(tableName, queryWrapper); + List objects = selectObjectListByQuery(schema,tableName, queryWrapper); Object object = objects == null || objects.isEmpty() ? null : objects.get(0); if (object == null) { return 0; @@ -408,14 +420,15 @@ public interface RowMapper { /** * 分页查询数据 * + * @param schema 模式 * @param tableName 表名 * @param page page 封装类 * @param queryWrapper 条件 * @return */ - default Page paginate(String tableName, Page page, QueryWrapper queryWrapper) { + default Page paginate(String schema,String tableName, Page page, QueryWrapper queryWrapper) { - CPI.setFromIfNecessary(queryWrapper, tableName); + CPI.setFromIfNecessary(queryWrapper,schema, tableName); List selectColumns = CPI.getSelectColumns(queryWrapper); @@ -468,7 +481,7 @@ public interface RowMapper { CPI.setJoins(queryWrapper, null); } - long count = selectCountByQuery(tableName, queryWrapper); + long count = selectCountByQuery(schema,tableName, queryWrapper); page.setTotalRow(count); } @@ -492,7 +505,7 @@ public interface RowMapper { int offset = page.getPageSize() * (page.getPageNumber() - 1); queryWrapper.limit(offset, page.getPageSize()); - List records = selectListByQuery(tableName, queryWrapper); + List records = selectListByQuery(schema,tableName, queryWrapper); page.setRecords(records); return page; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapperInvoker.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapperInvoker.java index 472d9a7d..87c72b78 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapperInvoker.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/RowMapperInvoker.java @@ -42,8 +42,8 @@ public class RowMapperInvoker { } } - public int insert(String tableName, Row row) { - return execute(mapper -> mapper.insert(tableName, row)); + public int insert(String schema,String tableName, Row row) { + return execute(mapper -> mapper.insert(schema,tableName, row)); } @@ -51,29 +51,29 @@ public class RowMapperInvoker { return execute(mapper -> mapper.insertBySql(sql, args)); } - public int insertBatchWithFirstRowColumns(String tableName, List rows) { - return execute(mapper -> mapper.insertBatchWithFirstRowColumns(tableName, rows)); + public int insertBatchWithFirstRowColumns(String schema,String tableName, List rows) { + return execute(mapper -> mapper.insertBatchWithFirstRowColumns(schema,tableName, rows)); } public int deleteBySql(String sql, Object... args) { return execute(mapper -> mapper.deleteBySql(sql, args)); } - public int deleteById(String tableName, Row row) { - return execute(mapper -> mapper.deleteById(tableName, row)); + public int deleteById(String schema,String tableName, Row row) { + return execute(mapper -> mapper.deleteById(schema,tableName, row)); } - public int deleteById(String tableName, String primaryKey, Object id) { - return execute(mapper -> mapper.deleteById(tableName, primaryKey, id)); + public int deleteById(String schema,String tableName, String primaryKey, Object id) { + return execute(mapper -> mapper.deleteById(schema,tableName, primaryKey, id)); } - public int deleteBatchByIds(String tableName, String primaryKey, Collection ids) { - return execute(mapper -> mapper.deleteBatchByIds(tableName, primaryKey, ids)); + public int deleteBatchByIds(String schema,String tableName, String primaryKey, Collection ids) { + return execute(mapper -> mapper.deleteBatchByIds(schema,tableName, primaryKey, ids)); } - public int deleteByQuery(String tableName, QueryWrapper queryWrapper) { - return execute(mapper -> mapper.deleteByQuery(tableName, queryWrapper)); + public int deleteByQuery(String schema,String tableName, QueryWrapper queryWrapper) { + return execute(mapper -> mapper.deleteByQuery(schema,tableName, queryWrapper)); } public int updateBySql(String sql, Object... args) { @@ -115,52 +115,52 @@ public class RowMapperInvoker { return results; } - public int updateById(String tableName, Row row) { - return execute(mapper -> mapper.updateById(tableName, row)); + public int updateById(String schema,String tableName, Row row) { + return execute(mapper -> mapper.updateById(schema,tableName, row)); } - public int updateByQuery(String tableName, Row data, QueryWrapper queryWrapper) { - return execute(mapper -> mapper.updateByQuery(tableName, data, queryWrapper)); + public int updateByQuery(String schema,String tableName, Row data, QueryWrapper queryWrapper) { + return execute(mapper -> mapper.updateByQuery(schema,tableName, data, queryWrapper)); } - public int updateBatchById(String tableName, List rows) { - return execute(mapper -> mapper.updateBatchById(tableName, rows)); + public int updateBatchById(String schema,String tableName, List rows) { + return execute(mapper -> mapper.updateBatchById(schema,tableName, rows)); } public Row selectOneBySql(String sql, Object... args) { return execute(mapper -> mapper.selectOneBySql(sql, args)); } - public Row selectOneById(String tableName, Row row) { - return execute(mapper -> mapper.selectOneById(tableName, row)); + public Row selectOneById(String schema,String tableName, Row row) { + return execute(mapper -> mapper.selectOneById(schema,tableName, row)); } - public Row selectOneById(String tableName, String primaryKey, Object id) { - return execute(mapper -> mapper.selectOneById(tableName, primaryKey, id)); + public Row selectOneById(String schema,String tableName, String primaryKey, Object id) { + return execute(mapper -> mapper.selectOneById(schema,tableName, primaryKey, id)); } - public Row selectOneByQuery(String tableName, QueryWrapper queryWrapper) { - return execute(mapper -> mapper.selectOneByQuery(tableName, queryWrapper)); + public Row selectOneByQuery(String schema,String tableName, QueryWrapper queryWrapper) { + return execute(mapper -> mapper.selectOneByQuery(schema,tableName, queryWrapper)); } public List selectListBySql(String sql, Object... args) { return execute(mapper -> mapper.selectListBySql(sql, args)); } - public List selectListByQuery(String tableName, QueryWrapper queryWrapper) { - return execute(mapper -> mapper.selectListByQuery(tableName, queryWrapper)); + public List selectListByQuery(String schema,String tableName, QueryWrapper queryWrapper) { + return execute(mapper -> mapper.selectListByQuery(schema,tableName, queryWrapper)); } - public List selectAll(String tableName) { - return execute(mapper -> mapper.selectAll(tableName)); + public List selectAll(String schema,String tableName) { + return execute(mapper -> mapper.selectAll(schema,tableName)); } - public Object selectObjectByQuery(String tableName, QueryWrapper queryWrapper) { - return execute(mapper -> mapper.selectObjectByQuery(tableName, queryWrapper)); + public Object selectObjectByQuery(String schema,String tableName, QueryWrapper queryWrapper) { + return execute(mapper -> mapper.selectObjectByQuery(schema,tableName, queryWrapper)); } - public List selectObjectListByQuery(String tableName, QueryWrapper queryWrapper) { - return execute(mapper -> mapper.selectObjectListByQuery(tableName, queryWrapper)); + public List selectObjectListByQuery(String schema,String tableName, QueryWrapper queryWrapper) { + return execute(mapper -> mapper.selectObjectListByQuery(schema,tableName, queryWrapper)); } public Object selectObject(String sql, Object... args) { @@ -176,16 +176,16 @@ public class RowMapperInvoker { } - public long selectCountByQuery(String tableName, QueryWrapper queryWrapper) { - return execute(mapper -> mapper.selectCountByQuery(tableName, queryWrapper)); + public long selectCountByQuery(String schema,String tableName, QueryWrapper queryWrapper) { + return execute(mapper -> mapper.selectCountByQuery(schema,tableName, queryWrapper)); } - public Page paginate(String tableName, Page page, QueryWrapper queryWrapper) { - return execute(mapper -> mapper.paginate(tableName, page, queryWrapper)); + public Page paginate(String schema,String tableName, Page page, QueryWrapper queryWrapper) { + return execute(mapper -> mapper.paginate(schema,tableName, page, queryWrapper)); } - public int updateNumberAddByQuery(String tableName, String fieldName, Number value, QueryWrapper queryWrapper) { - return execute(mapper -> mapper.updateNumberAddByQuery(tableName, fieldName, value, queryWrapper)); + public int updateNumberAddByQuery(String schema,String tableName, String fieldName, Number value, QueryWrapper queryWrapper) { + return execute(mapper -> mapper.updateNumberAddByQuery(schema,tableName, fieldName, value, queryWrapper)); } } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java index 02366b9e..cc931115 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTestStarter.java @@ -37,11 +37,11 @@ public class DbTestStarter { .setDataSource(dataSource) .start(); - Row row1 = Db.selectOneById("tb_account", "id", 1); + Row row1 = Db.selectOneById(null,"tb_account", "id", 1); RowUtil.printPretty(row1); //查询全部 - List rows = Db.selectAll("tb_account"); + List rows = Db.selectAll(null,"tb_account"); RowUtil.printPretty(rows); @@ -53,7 +53,7 @@ public class DbTestStarter { row.set("user_name", "michael yang"); row.set("age", 18); row.set("birthday", new Date()); - Db.insert("tb_account", row); + Db.insert(null,"tb_account", row); //查看刚刚插入数据的主键 id System.out.println(">>>>>>>>>id: " + row.get("id")); @@ -79,7 +79,7 @@ public class DbTestStarter { //再次查询全部数据 - rows = Db.selectAll("tb_account"); + rows = Db.selectAll(null,"tb_account"); RowUtil.printPretty(rows); // for (Row row2 : rows) { @@ -96,10 +96,10 @@ public class DbTestStarter { r.prepareAttrsByKeySet(); r.setPrimaryKeys(RowKey.AUTO); }); - Db.insertBatch("tb_account", rows, 100); + Db.insertBatch(null,"tb_account", rows, 100); //再次查询全部数据 - rows = Db.selectAll("tb_account"); + rows = Db.selectAll(null,"tb_account"); RowUtil.printPretty(rows); } } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MultiDataSourceTester.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MultiDataSourceTester.java index ee362935..236b47db 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MultiDataSourceTester.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MultiDataSourceTester.java @@ -61,7 +61,7 @@ public class MultiDataSourceTester { //默认查询 db1 System.out.println("\n------ds1"); - List rows1 = Db.selectAll("tb_account"); + List rows1 = Db.selectAll(null,"tb_account"); RowUtil.printPretty(rows1); @@ -74,7 +74,7 @@ public class MultiDataSourceTester { // //查询数据源 ds2 System.out.println("\n------ds2"); DataSourceKey.use("ds2"); - List rows = Db.selectAll("tb_account"); + List rows = Db.selectAll(null,"tb_account"); RowUtil.printPretty(rows); // // boolean success = Db.tx(() -> { diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java index f748af46..ac3d11bc 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java @@ -50,7 +50,7 @@ public class RowTestStarter { AuditManager.setAuditEnable(true); AuditManager.setMessageCollector(new ConsoleMessageCollector()); - Page rowPage = Db.paginate("tb_account", 1, 10, QueryWrapper.create().hint("USE_MERGE")); + Page rowPage = Db.paginate("flex","tb_account", 1, 10, QueryWrapper.create().hint("USE_MERGE")); System.out.println(rowPage); @@ -83,13 +83,13 @@ public class RowTestStarter { rowList.add(row); } - Db.insertBatch("tb_account",rowList); + Db.insertBatch(null,"tb_account",rowList); for (Row row : rowList) { System.out.println(">>>>>>>id: " + row.get("id")); } - List rows1 = Db.selectAll("tb_account"); + List rows1 = Db.selectAll(null,"tb_account"); RowUtil.printPretty(rows1); // //新增一条数据,自增 diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java index 79ebb645..678529bf 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java @@ -73,10 +73,10 @@ public class AccountController { // account.setUserName("heihei"); // accountMapper.update(account); - Row row1 = Db.selectOneById("tb_account", "id", 1); + Row row1 = Db.selectOneById(null,"tb_account", "id", 1); System.out.println(">>>>>>> row1: " + row1); - Row row2 = Db.selectOneById("tb_account", "id", 2); + Row row2 = Db.selectOneById(null,"tb_account", "id", 2); System.out.println(">>>>>>> row2: " + row2); // Account account1 = accountMapper.selectOneById(1L); diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/AccountTest.java b/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/AccountTest.java index 4b4f882f..efb482f8 100644 --- a/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/AccountTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/AccountTest.java @@ -41,7 +41,7 @@ public class AccountTest implements WithAssertions { @Test public void testSelectOneByRow() { - Row row = Db.selectOneById("tb_account", "id", 1); + Row row = Db.selectOneById(null,"tb_account", "id", 1); System.out.println(row); }