rename "Db.insertRow()" to "Db.insert()"

This commit is contained in:
开源海哥 2023-03-28 15:13:38 +08:00
parent 1c6e3eda5c
commit 242e58464b
9 changed files with 43 additions and 38 deletions

View File

@ -12,7 +12,7 @@ Db.insertBySql(sql,1,"michael");
Row account = new Row(); Row account = new Row();
account.set("id",100); account.set("id",100);
account.set(ACCOUNT.USER_NAME,"Michael"); account.set(ACCOUNT.USER_NAME,"Michael");
Db.insertRow("tb_account",account); Db.insert("tb_account",account);
//根据主键查询数据 //根据主键查询数据
@ -67,7 +67,7 @@ Map result = row.toUnderlineKeysMap();
Row row = Row.ofKey(RowKey.ID_AUTO); Row row = Row.ofKey(RowKey.ID_AUTO);
row.set(ACCOUNT.USER_NAME,"Michael"); row.set(ACCOUNT.USER_NAME,"Michael");
Db.insertRow("tb_account",row); Db.insert("tb_account",row);
``` ```
**ID 为 UUID** **ID 为 UUID**
@ -77,7 +77,7 @@ Db.insertRow("tb_account",row);
Row row = Row.ofKey(RowKey.ID_UUID); Row row = Row.ofKey(RowKey.ID_UUID);
row.set(ACCOUNT.USER_NAME,"Michael"); row.set(ACCOUNT.USER_NAME,"Michael");
Db.insertRow("tb_account",row); Db.insert("tb_account",row);
``` ```
**自定义 Row 主键生成方式** **自定义 Row 主键生成方式**
@ -89,7 +89,7 @@ RowKey myRowKey = RowKey.of("id", KeyType.Generator, "uuid", true);
Row row = Row.ofKey(myRowKey); Row row = Row.ofKey(myRowKey);
row.set(ACCOUNT.USER_NAME,"Michael"); row.set(ACCOUNT.USER_NAME,"Michael");
Db.insertRow("tb_account",row); Db.insert("tb_account",row);
``` ```
## Db 中的 RowMapperInvoker ## Db 中的 RowMapperInvoker

View File

@ -96,7 +96,7 @@ public class FlexConfiguration extends Configuration {
@Override @Override
public void addMappedStatement(MappedStatement ms) { public void addMappedStatement(MappedStatement ms) {
//替换 RowMapper.insertRow 的主键生成器 //替换 RowMapper.insert 的主键生成器
//替换 RowMapper.insertBatchWithFirstRowColumns 的主键生成器 //替换 RowMapper.insertBatchWithFirstRowColumns 的主键生成器
if (ms.getId().startsWith("com.mybatisflex.core.row.RowMapper.insert")) { if (ms.getId().startsWith("com.mybatisflex.core.row.RowMapper.insert")) {
ms = replaceRowKeyGenerator(ms); ms = replaceRowKeyGenerator(ms);

View File

@ -52,13 +52,13 @@ public class RowSqlProvider {
} }
/** /**
* insertRow sql 构建 * insert sql 构建
* *
* @param params * @param params
* @return sql * @return sql
* @see RowMapper#insertRow(String, Row) * @see RowMapper#insert(String, Row)
*/ */
public static String insertRow(Map params) { public static String insert(Map params) {
String tableName = ProviderUtil.getTableName(params); String tableName = ProviderUtil.getTableName(params);
Row row = ProviderUtil.getRow(params); Row row = ProviderUtil.getRow(params);
ProviderUtil.setSqlArgs(params, row.obtainModifyValues()); ProviderUtil.setSqlArgs(params, row.obtainModifyValues());

View File

@ -49,6 +49,18 @@ public class Db {
}); });
} }
/**
* tableName 插入一条 row 数据
*
* @param tableName 表名
* @param row 数据
*/
public static int insert(String tableName, Row row) {
return invoker().insert(tableName, row);
}
/** /**
* 直接编写 sql 插入数据 * 直接编写 sql 插入数据
* *
@ -59,15 +71,6 @@ public class Db {
return invoker().insertBySql(sql, args); return invoker().insertBySql(sql, args);
} }
/**
* tableName 插入一条 row 数据
*
* @param tableName 表名
* @param row 数据
*/
public static int insertRow(String tableName, Row row) {
return invoker().insertRow(tableName, row);
}
/** /**
* 批量插入数据 * 批量插入数据

View File

@ -35,6 +35,19 @@ public interface RowMapper {
//////insert ////// //////insert //////
/**
* 插入 row 到数据表
*
* @param tableName 表名
* @param row 数据内容当设置有主键时主键会自动填充
* @return 执行影响的行数
* @see RowSqlProvider#insert(Map)
*/
@InsertProvider(value = RowSqlProvider.class, method = "insert")
int insert(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROW) Row row);
/** /**
* 执行 insert sql 语句 * 执行 insert sql 语句
* *
@ -47,18 +60,6 @@ public interface RowMapper {
int insertBySql(@Param(FlexConsts.SQL) String sql, @Param(FlexConsts.SQL_ARGS) Object... args); int insertBySql(@Param(FlexConsts.SQL) String sql, @Param(FlexConsts.SQL_ARGS) Object... args);
/**
* 插入 row 到数据表
*
* @param tableName 表名
* @param row 数据内容当设置有主键时主键会自动填充
* @return 执行影响的行数
* @see RowSqlProvider#insertRow(Map)
*/
@InsertProvider(value = RowSqlProvider.class, method = "insertRow")
int insertRow(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROW) Row row);
/** /**
* 批量插入 rows 到数据表 * 批量插入 rows 到数据表
* <p> * <p>

View File

@ -61,14 +61,15 @@ public class RowMapperInvoker {
} }
} }
public int insert(String tableName, Row row) {
return execute(mapper -> mapper.insert(tableName, row));
}
public int insertBySql(String sql, Object... args) { public int insertBySql(String sql, Object... args) {
return execute(mapper -> mapper.insertBySql(sql, args)); return execute(mapper -> mapper.insertBySql(sql, args));
} }
public int insertRow(String tableName, Row row) {
return execute(mapper -> mapper.insertRow(tableName, row));
}
public int[] insertBatch(String tableName, Collection<Row> rows, int batchSize) { public int[] insertBatch(String tableName, Collection<Row> rows, int batchSize) {
int[] results = new int[rows.size()]; int[] results = new int[rows.size()];
@ -89,7 +90,7 @@ public class RowMapperInvoker {
} }
} }
} else { } else {
mapper.insertRow(tableName, row); mapper.insert(tableName, row);
} }
} }
} finally { } finally {

View File

@ -49,7 +49,7 @@ public class DbTestStarter {
row.set("user_name", "michael yang"); row.set("user_name", "michael yang");
row.set("age", 18); row.set("age", 18);
row.set("birthday", new Date()); row.set("birthday", new Date());
Db.insertRow("tb_account", row); Db.insert("tb_account", row);
//查看刚刚插入数据的主键 id //查看刚刚插入数据的主键 id
System.out.println(">>>>>>>>>id: " + row.get("id")); System.out.println(">>>>>>>>>id: " + row.get("id"));

View File

@ -53,7 +53,7 @@ public class RowTestStarter {
.set("age", 22) .set("age", 22)
.set("birthday", new Date()); .set("birthday", new Date());
bootstrap.execute(RowMapper.class, rowMapper -> bootstrap.execute(RowMapper.class, rowMapper ->
rowMapper.insertRow("tb_account", newRow)); rowMapper.insert("tb_account", newRow));
// //
// //新增后查看newRow id会自动被赋值 // //新增后查看newRow id会自动被赋值
@ -69,11 +69,11 @@ public class RowTestStarter {
// //
// List<Row> newRowList = new ArrayList<>(); // List<Row> newRowList = new ArrayList<>();
// for (int i = 0; i < 5; i++) { // for (int i = 0; i < 5; i++) {
// Row insertRow = Row.ofKey(RowKey.ID_AUTO) //id 自增 // Row insert = Row.ofKey(RowKey.ID_AUTO) //id 自增
// .set("user_name", "new_user_" + i) // .set("user_name", "new_user_" + i)
// .set("age", 22) // .set("age", 22)
// .set("birthday", new Date()); // .set("birthday", new Date());
// newRowList.add(insertRow); // newRowList.add(insert);
// } // }
// //
// //批量插入数据 // //批量插入数据

View File

@ -412,7 +412,7 @@ Db.insertBySql(sql,1,"michael");
Row account=new Row(); Row account=new Row();
account.set("id",100); account.set("id",100);
account.set("name","Michael"); account.set("name","Michael");
Db.insertRow("tb_account",account); Db.insert("tb_account",account);
//根据主键查询数据 //根据主键查询数据