saveAll支持有id则更新和无id则插入
This commit is contained in:
parent
8ffe111086
commit
545f0bd4b0
@ -1,6 +1,8 @@
|
||||
package com.ruoyi.common.core.mybatisplus.core;
|
||||
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@ -11,6 +13,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -118,7 +123,27 @@ public class ServicePlusImpl<M extends BaseMapperPlus<T>, T, K> extends ServiceI
|
||||
*/
|
||||
@Override
|
||||
public boolean saveAll(Collection<T> entityList) {
|
||||
return baseMapper.insertAll(entityList) == entityList.size();
|
||||
ArrayList<T> list = new ArrayList<>();
|
||||
for (T t : entityList) {
|
||||
try {
|
||||
//获取属性注解的value值
|
||||
Field f = t.getClass().getDeclaredField("id");
|
||||
f.setAccessible( true );//设置可以范围private
|
||||
Object o = f.get(t);//获取出id的值
|
||||
System.out.println(o);
|
||||
if (o == null) {
|
||||
//如果id为null,插入
|
||||
list.add(t);
|
||||
} else {
|
||||
//否则更新
|
||||
baseMapper.updateById(t);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return baseMapper.insertAll(list) == list.size();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -39,8 +39,13 @@ public class TestBatchController extends BaseController {
|
||||
// @DataSource(DataSourceType.SLAVE)
|
||||
public AjaxResult<Void> add() {
|
||||
List<TestDemo> list = new ArrayList<>();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
list.add(new TestDemo().setOrderNum(-1L).setTestKey("批量新增").setValue("测试新增"));
|
||||
for (int i = 0; i < 10; i++) {
|
||||
TestDemo testDemo = new TestDemo();
|
||||
if (i % 2 == 0) {
|
||||
list.add(testDemo.setId((long) i + 1).setTestKey("批量更新").setValue("批量更新"));
|
||||
} else {
|
||||
list.add(testDemo.setOrderNum(-1L).setTestKey("批量新增").setValue("测试新增"));
|
||||
}
|
||||
}
|
||||
return toAjax(iTestDemoService.saveAll(list) ? 1 : 0);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user