mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
add insertSelective method. close #I6XS9Z
This commit is contained in:
parent
04842a05a9
commit
95f369847f
@ -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);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@ -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]);
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user