当前位置: 代码网 > it编程>编程语言>Java > mybatis-plus根据任意字段saveOrUpdateBatch更新插入的方法实现

mybatis-plus根据任意字段saveOrUpdateBatch更新插入的方法实现

2026年04月02日 Java 我要评论
mybatisplus iservice接口下的saveorupdatebatch方法默认是根据主键来决定是要更新还是插入的,如果要根据其他字段(必须是唯一约束,唯一约束字段可以是多个)更新的话,则需

mybatisplus iservice接口下的saveorupdatebatch方法默认是根据主键来决定是要更新还是插入的,如果要根据其他字段(必须是唯一约束,唯一约束字段可以是多个)更新的话,则需要在项目的service层重写该方法。

方法源码

@transactional(
        rollbackfor = {exception.class}
    )
    public boolean saveorupdatebatch(collection<t> entitylist, int batchsize) {
        tableinfo tableinfo = tableinfohelper.gettableinfo(this.entityclass);
        assert.notnull(tableinfo, "error: can not execute. because can not find cache of tableinfo for entity!", new object[0]);
        string keyproperty = tableinfo.getkeyproperty();
        assert.notempty(keyproperty, "error: can not execute. because can not find column for id from entity!", new object[0]);
        return sqlhelper.saveorupdatebatch(this.entityclass, this.mapperclass, this.log, entitylist, batchsize, (sqlsession, entity) -> {
            object idval = reflectionkit.getfieldvalue(entity, keyproperty);
            return stringutils.checkvalnull(idval) || collectionutils.isempty(sqlsession.selectlist(this.getsqlstatement(sqlmethod.select_by_id), entity));
        }, (sqlsession, entity) -> {
            mappermethod.parammap<t> param = new mappermethod.parammap();
            param.put("et", entity);
            sqlsession.update(this.getsqlstatement(sqlmethod.update_by_id), param);
        });
    }

从源码中可以看出实现saveorupdatebatch的主要方法就是sqlhelper.saveorupdatebatch

public static <e> boolean saveorupdatebatch(class<?> entityclass, class<?> mapper, log log, collection<e> list, int batchsize, bipredicate<sqlsession, e> predicate, biconsumer<sqlsession, e> consumer) {
        string sqlstatement = getsqlstatement(mapper, sqlmethod.insert_one);
        return executebatch(entityclass, log, list, batchsize, (sqlsession, entity) -> {
            if (predicate.test(sqlsession, entity)) {
                sqlsession.insert(sqlstatement, entity);
            } else {
                consumer.accept(sqlsession, entity);
            }
        });
    }

该方法的最后两个参数predicate,consumer
predicate 这个函数是用于判断是否要进行插入操作 true插入,false:则通过consumer 函数执行更新

方法改造

注意:写在项目操作对应表的service层
首先在service层定义接口

boolean saveorupdatebatchbyagentidandperiodandtype(list<entity> list);

类为数据库表对应的实体类 ,agentid,period,type,这个三个字段为表的唯一约束,即当表中存在这三个字段组合对应的记录时则进行更新操作,不存在则进行插入操作
service层接口实现

@transactional(rollbackfor = exception.class)
    @ds("xxxx")//如果为多数据源,这里要指明具体操作的数据源名称
    public boolean saveorupdatebatchbyagentidandperiodandtype(list<entity> list) {
        return sqlhelper.saveorupdatebatch(entityclass, this.mapperclass, super.log, list, default_batch_size, (sqlsession, entity) -> {//这里主要是查询唯一约束对应的记录是否存在
            lambdaquerywrapper<entity> querywrapper = wrappers.<entity>lambdaquery()
                    .eq(entity::getagentid, entity.getagentid()).eq(entity::getperiod,entity.getperiod())
                    .eq(entity::gettype,entity.gettype());
            map<string, object> map = collectionutils.newhashmapwithexpectedsize(1);
            map.put(constants.wrapper, querywrapper);
            return collectionutils.isempty(sqlsession.selectlist(getsqlstatement(sqlmethod.select_list), map));
        }, (sqlsession, entity) -> {
            lambdaupdatewrapper<entity> lambdaupdatewrapper = new lambdaupdatewrapper<>();
            lambdaupdatewrapper.eq(entity::getagentid, entity.getagentid()).eq(entity::getperiod,entity.getperiod())
                    .eq(entity::gettype,entity.gettype());
            map<string, object> param = collectionutils.newhashmapwithexpectedsize(2);
            param.put(constants.entity, entity);
            param.put(constants.wrapper, lambdaupdatewrapper);
            sqlsession.update(getsqlstatement(sqlmethod.update), param);
        });
    }

非批量的saveorupdate也可以按照这种方式进行改造

到此这篇关于mybatis-plus根据任意字段saveorupdatebatch更新插入的方法实现的文章就介绍到这了,更多相关mybatis-plus saveorupdatebatch内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com