mybatisplus 数据批量插入 遇到错误该批次改为单条插入

批量插入效率远大于单条数据插入,有事一批数据中有一条数据报错就会导致这一批次数据都插入失败,为了保证数据最大化的插入到数据库中,就需要批量转单条插入,单条插入中遇到错的数据跳过,保证其他数据正确的插入到数据库中。

直接上代码

1、实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@TableName(value = "yc_test_t")
public class YcTestT {
    private static final long serialVersionUID = 1L;
 
    /**
     * ID。
     */
    @TableId
    private Integer id;
    /**
     * 姓名。
     */
    private String name;
    /**
     * 备注。
     */
    private String note;
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public Integer getId() {
        return id;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getName() {
        return name;
    }
 
    public void setNote(String note) {
        this.note = note;
    }
 
    public String getNote() {
        return note;
    }
 
    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("id", getId())
                .append("name", getName())
                .append("note", getNote())
                .toString();
    }
}

 2、mapper类

1
2
3
public interface YcTestTMapper extends BaseMapper<YcTestT> {
 
}

 3、接口类 如下红色部分  

重点:必须继承IService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public interface IYcTestTService extends IService<YcTestT> {
    /**
     * 批量插入测试表。
     *
     * @param entityList 参数说明
     * @return status
     */
    boolean saveBatch(Collection<YcTestT> entityList);
 
 
    /**
     * 单记录新增测试表。
     *
     * @param ycTestT 参数说明
     * @return status
     */
    int insert(YcTestT ycTestT);
 
}

 4、实现类 

重点:如下红色部分继承ServiceImpl类 复写saveBatch方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Service
public class YcTestTServiceImpl extends ServiceImpl<YcTestTMapper, YcTestT> implements IYcTestTService {
    @Autowired
    YcTestTMapper ycTestTMapper;
 
    @Override
    @Transactional
    public boolean saveBatch(Collection<YcTestT> entityList) {
        return super.saveBatch(entityList);
    }
 
 
    /**
     * 单记录新增测试表。
     *
     * @param ycTestT 参数说明
     * @return status
     */
    @Override
    public int insert(YcTestT ycTestT) {
        return ycTestTMapper.insert(ycTestT);
    }
 
}

5、 控制器

重点:红色部分是批量转单条插入的关键

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@RestController
@RequestMapping("/yctestt")
public class YcTestTController extends BaseController {
 
 
    @Autowired
    private IYcTestTService ycTestTService;
 
    /**
     * 单记录新增测试表。
     *
     * @return AjaxResult
     */
    @PostMapping(value = "/saveBatch", headers = API_VERSION_NAME + "=v1")
    @ResponseBody
    public AjaxResult saveBatch() {
 
        List<YcTestT> list = new ArrayList<>();
        for(int i = 0 ;i < 5000;i++){
            YcTestT ycTestT = new YcTestT();
            ycTestT.setId(i);
            ycTestT.setName("张三"+(i+""));
            ycTestT.setNote("备注"+(i+""));
            list.add(ycTestT);
            if ( (i+1)%3000 == 0) {
                try {
                    //打开注释主动抛出异常,会进入单条插入的处理流程中
                    //throw new Exception("自定义异常信息");
                    ycTestTService.saveBatch(list);
                } catch (Exception e) {
                    // 批量插入失败,改为单条插入
                    for (int j = 0; j<list.size();j++) {
                        try {
                            YcTestT testT = list.get(j);
                            //单条插入
                            ycTestTService.insert(testT);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }finally {
                    list.clear();
                }
            }
        }
        //处理除3000余数的数据
        if(list.size()>=1){
            ycTestTService.saveBatch(list);
        }
 
        return AjaxResult.success(HttpStatusCode.POST_SUCCESS_CODE,"success!");
    }
 
 
 
}

 

posted @   万笑佛  阅读(701)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示