mybatis插入记录,参数为list

数据库表结构如图:
分别是身份字段、模块字段、不同的身份对应不同的模块
在这里插入图片描述
插入角色模块关系,思路:先要删除表中该身份对应的模块信息,再插入记录。

Controller层:

@PostMapping(value = "insertIdentityModel")
    @ApiOperation("插入角色模块关系")
    @ApiImplicitParams({
    
    
            @ApiImplicitParam(name = "modelIds", value = "模块ID", allowMultiple = true, required = true, paramType = "query"),
            @ApiImplicitParam(name = "identityId", value = "角色ID", required = true, paramType = "query")
    })
    @SystemLog(module = "角色模块关系管理", methods = "新增角色模块关系", description = "新增角色模块关系数据")
    public CommonResult insertIdentityModel(Long modelIds[], Long identityId) {
    
    
        try {
    
    
            List<BusMemberIdentity> modelList = new ArrayList<>();

            for (Long modelId : modelIds) {
    
    
                BusMemberIdentity identityModel = new BusMemberIdentity();
                identityModel.setIdentityId(identityId);
                identityModel.setModelId(modelId);
                modelList.add(identityModel);
            }
            busMemberIdentityService.insertIdentityModel(modelList);
        } catch (Exception e) {
    
    
            log.error("新增角色模块信息失败", e);
            return CommonResult.failed("新增角色模块信息失败");
        }
        return CommonResult.success(null);
    }

insertIdentityModel接口:

void insertIdentityModel(List<BusMemberIdentity> modelList);

insertIdentityModel实现类

 @Override
    public void insertIdentityModel(List<BusMemberIdentity> modelList) {
    
    
        if (modelList != null && modelList.size() > 0) {
    
    
            //先删除bus_identity_model表中,identityID关联的数据
            memberIdentityMapper.delByidentityId(modelList.get(0).getIdentityId());
            //然后插入数据
            memberIdentityMapper.IdentityModel(modelList);
        }
    }

delByidentityId

 <delete id="delByidentityId">
        delete from bus_identity_model
        <where>
            identity_id = #{
    
    identityId}
        </where>
    </delete>

IdentityModel

 <insert id="IdentityModel">
        insert into bus_identity_model(model_id,identity_id) values
        <foreach
                collection="list"
                item="item"
                index="index"
                separator=",">
            (#{
    
    item.modelId},#{
    
    item.identityId})
        </foreach>
    </insert>

猜你喜欢

转载自blog.csdn.net/weixin_42260782/article/details/113938365