add insertSelective method. close #I6XS9Z

This commit is contained in:
开源海哥 2023-04-22 09:44:20 +08:00
parent 04842a05a9
commit 95f369847f
6 changed files with 81 additions and 15 deletions

View File

@ -35,6 +35,30 @@ import java.util.Map;
public interface BaseMapper<T> {
/**
* 插入 entity 数据
*
* @param entity 实体类
* @return 返回影响的行数
*/
default int insert(T entity){
return insert(entity,false);
}
/**
* 插入 entity 数据但是忽略 null 的数据只对有值的内容进行插入
* 这样的好处是数据库已经配置了一些默认值这些默认值才会生效
*
* @param entity 实体类
* @return 返回影响的行数
*/
default int insertSelective(T entity){
return insert(entity,true);
}
/**
* 插入 entity 数据
*
@ -43,7 +67,8 @@ public interface BaseMapper<T> {
* @see com.mybatisflex.core.provider.EntitySqlProvider#insert(Map, ProviderContext)
*/
@InsertProvider(type = EntitySqlProvider.class, method = "insert")
int insert(@Param(FlexConsts.ENTITY) T entity);
int insert(@Param(FlexConsts.ENTITY) T entity, @Param(FlexConsts.IGNORE_NULLS) boolean ignoreNulls);
/**

View File

@ -365,11 +365,11 @@ public class CommonsDialectImpl implements IDialect {
}
@Override
public String forInsertEntity(TableInfo tableInfo, Object entity) {
public String forInsertEntity(TableInfo tableInfo, Object entity, boolean ignoreNulls) {
StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO ").append(wrap(tableInfo.getTableName()));
String[] insertColumns = tableInfo.obtainInsertColumns();
String[] insertColumns = tableInfo.obtainInsertColumns(entity, ignoreNulls);
Map<String, String> onInsertColumns = tableInfo.getOnInsertColumns();
StringJoiner sqlFields = new StringJoiner(", ");
@ -393,7 +393,7 @@ public class CommonsDialectImpl implements IDialect {
public String forInsertEntityBatch(TableInfo tableInfo, List<Object> entities) {
StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO ").append(wrap(tableInfo.getTableName()));
String[] insertColumns = tableInfo.obtainInsertColumns();
String[] insertColumns = tableInfo.obtainInsertColumns(null, false);
String[] warpedInsertColumns = new String[insertColumns.length];
for (int i = 0; i < insertColumns.length; i++) {
warpedInsertColumns[i] = wrap(insertColumns[i]);

View File

@ -59,7 +59,7 @@ public interface IDialect {
//////for entity /////
String forInsertEntity(TableInfo tableInfo, Object entity);
String forInsertEntity(TableInfo tableInfo, Object entity, boolean ignoreNulls);
String forInsertEntityBatch(TableInfo tableInfo, List<Object> entities);

View File

@ -53,6 +53,8 @@ public class EntitySqlProvider {
throw FlexExceptions.wrap("entity can not be null.");
}
boolean ignoreNulls = ProviderUtil.isIgnoreNulls(params);
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
//设置乐观锁版本字段的初始化数据
@ -67,13 +69,14 @@ public class EntitySqlProvider {
//执行 onInsert 监听器
tableInfo.invokeOnInsertListener(entity);
Object[] values = tableInfo.buildInsertSqlArgs(entity);
Object[] values = tableInfo.buildInsertSqlArgs(entity, ignoreNulls);
ProviderUtil.setSqlArgs(params, values);
return DialectFactory.getDialect().forInsertEntity(tableInfo, entity);
return DialectFactory.getDialect().forInsertEntity(tableInfo, entity, ignoreNulls);
}
/**
* insertBatch sql 构建
*
@ -102,7 +105,7 @@ public class EntitySqlProvider {
Object[] allValues = new Object[0];
for (Object entity : entities) {
allValues = ArrayUtil.concat(allValues, tableInfo.buildInsertSqlArgs(entity));
allValues = ArrayUtil.concat(allValues, tableInfo.buildInsertSqlArgs(entity, false));
}
ProviderUtil.setSqlArgs(params, allValues);
@ -151,7 +154,7 @@ public class EntitySqlProvider {
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
Object[] tenantIdArgs = tableInfo.buildTenantIdArgs();
ProviderUtil.setSqlArgs(params, ArrayUtil.concat(primaryValues,tenantIdArgs));
ProviderUtil.setSqlArgs(params, ArrayUtil.concat(primaryValues, tenantIdArgs));
return DialectFactory.getDialect().forDeleteEntityBatchByIds(tableInfo, primaryValues);
}

View File

@ -289,27 +289,51 @@ public class TableInfo {
/**
* 插入新增数据时获取所有要插入的字段
*
* @param entity
* @param ignoreNulls
* @return 字段列表
*/
public String[] obtainInsertColumns() {
return ArrayUtil.concat(insertPrimaryKeys, columns);
public String[] obtainInsertColumns(Object entity, boolean ignoreNulls) {
String[] defaultInsertColumns = ArrayUtil.concat(insertPrimaryKeys, columns);
if (!ignoreNulls) {
return defaultInsertColumns;
} else {
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
List<String> retColumns = new ArrayList<>();
for (String insertColumn : defaultInsertColumns) {
if (onInsertColumns != null && onInsertColumns.containsKey(insertColumn)) {
retColumns.add(insertColumn);
} else {
Object value = buildColumnSqlArg(metaObject, insertColumn);
if (value == null) {
continue;
}
retColumns.add(insertColumn);
}
}
return retColumns.toArray(new String[0]);
}
}
/**
* 构建 insert Sql 参数
*
* @param entity entity 中获取
* @param entity entity 中获取
* @param ignoreNulls 是否忽略 null
* @return 数组
*/
public Object[] buildInsertSqlArgs(Object entity) {
public Object[] buildInsertSqlArgs(Object entity, boolean ignoreNulls) {
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
String[] insertColumns = obtainInsertColumns();
String[] insertColumns = obtainInsertColumns(entity, ignoreNulls);
List<Object> values = new ArrayList<>(insertColumns.length);
for (String insertColumn : insertColumns) {
if (onInsertColumns == null || !onInsertColumns.containsKey(insertColumn)) {
Object value = buildColumnSqlArg(metaObject, insertColumn);
if (ignoreNulls && value == null) {
continue;
}
values.add(value);
}
}

View File

@ -32,10 +32,24 @@ public class ArticleSqlTester {
IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
String sql = dialect.forInsertEntity(tableInfo, article);
String sql = dialect.forInsertEntity(tableInfo, article, false);
System.out.println(sql);
}
@Test
public void testInsert1Sql() {
Article article = new Article();
article.setAccountId(1L);
article.setContent("aaa");
IDialect dialect = new CommonsDialectImpl();
TableInfo tableInfo = TableInfoFactory.ofEntityClass(Article.class);
String sql = dialect.forInsertEntity(tableInfo, article, true);
System.out.println(sql);
}
@Test
public void testInsertBatchSql() {
Article article1 = new Article();