add BaseMapper.insertOrUpdate method

This commit is contained in:
开源海哥 2023-04-17 10:08:31 +08:00
parent 8b221b6298
commit f4d2f06cda

View File

@ -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}