mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
add BaseMapper.insertOrUpdate method
This commit is contained in:
parent
8b221b6298
commit
f4d2f06cda
@ -22,6 +22,8 @@ import com.mybatisflex.core.query.QueryColumn;
|
||||
import com.mybatisflex.core.query.QueryCondition;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.query.CPI;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
import com.mybatisflex.core.util.ObjectUtil;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
import org.apache.ibatis.builder.annotation.ProviderContext;
|
||||
@ -55,6 +57,23 @@ public interface BaseMapper<T> {
|
||||
@InsertProvider(type = EntitySqlProvider.class, method = FlexConsts.METHOD_INSERT_BATCH)
|
||||
int insertBatch(@Param(FlexConsts.ENTITIES) List<T> entities);
|
||||
|
||||
|
||||
/**
|
||||
* 新增 或者 更新,若主键有值,则更新,若没有主键值,则插入
|
||||
*
|
||||
* @param entity 实体类
|
||||
* @return 返回影响的行数
|
||||
*/
|
||||
default int insertOrUpdate(T entity) {
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entity.getClass());
|
||||
Object[] pkArgs = tableInfo.buildPkSqlArgs(entity);
|
||||
if (pkArgs.length == 0 || pkArgs[0] == null) {
|
||||
return insert(entity);
|
||||
} else {
|
||||
return update(entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 id 删除数据
|
||||
* 如果是多个主键的情况下,需要传入数组 new Object[]{100,101}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user