mybatisPlus批量插入

Administrator
发布于 2020-08-08 / 2580 阅读 / 0 评论 / 0 点赞

mybatisPlus批量插入

mybatisPlus批量插入

1、只有一个Service类没有impl的时候

  • 可以这么写
@Service
@Slf4j
public class OrgService extends ServiceImpl<OrgMapper,Org> implements IService<Org> {

    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private JxOrgService jxOrgService;


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean insertBatch(){
        ArrayList<Org> orgs = new ArrayList<>();
        //批量插入方法
        boolean b = saveBatch(orgs);
        //如果超过1000条了就多加个参数
        //saveBatch(orgs,2000);
        return b;
    }
}

//继承ServiceImpl<OrgMapper,Org> 第一个是service对应的mapper,第二个是实体类
//实现implements IService<Org> Org为实体类
  • 然后就可以调用savaBatch()方法了,默认只插入1000条,如果多就添加第二个方法参数

2、有Service接口和ServiceImpl实现类的时候

  • OrgService
public interface OrgService extends IService<Org> {

    public boolean insertBatch();
}
  • OrgServiceImpl
@Service
@Slf4j
public class OrgServiceImpl extends ServiceImpl<OrgMapper,Org> implements OrgService {

    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private JxOrgService jxOrgService;

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean insertBatch(){
        ArrayList<Org> orgs = new ArrayList<>();
        //批量插入方法
        boolean b = saveBatch(orgs);
        //如果超过1000条了就多加个参数
        //saveBatch(orgs,2000);
        return b;
    }

}

评论