Mybatis foreach 批量插入

一、业务:根据角色关联菜单

RelationDto
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = GlobalConstant.API_MODEL_IN + “-关联信息”)
public class RelationDto implements Serializable {

private static final long serialVersionUID = 1890569379952399719L;

@NotNull(message = "关联ID" + RegexpConstant.MSG_NOT_NULL)
@ApiModelProperty(value = "关联ID(例如用户ID)", required = true)
private Integer id;

@NotBlank(message = "ID字符串" + RegexpConstant.MSG_NOT_NULL)
@ApiModelProperty(value = "ID字符串,用于批量操作,以逗号分隔(例如用户所关联的角色ID/用户组ID...)", required = true)
private String ids;

}

Controller

@RestController
@RequestMapping(value = "/uac/role", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "Web - RoleController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class RoleController {
     @PostMapping(value = "/relation/menus")
     @ApiOperation(httpMethod = "POST", value = "根据角色ID添加关联菜单")
     public Wrapper addRelationMenus(
            @ApiParam(name = "relationDto", value = "关联信息", required = true)     @Valid @RequestBody RelationDto relationDto) {
        LOGGER.info("addRelationMenus, relationDto:{}", relationDto);
        menuRoleService.addRelationMenus(relationDto);
        return WrapMapper.ok();
    }
    }

Service

private static final Logger LOGGER = LoggerFactory.getLogger(MenuRoleServiceImpl.class);

    private final MenuRoleMapper menuRoleMapper;
    
    @Autowired
    public MenuRoleServiceImpl(MenuRoleMapper menuRoleMapper) {
        this.menuRoleMapper = menuRoleMapper;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addRelationRoles(RelationDto relationDto) {
        LOGGER.info("addRelationRoles, relationDto:{}", relationDto);
        if (StringUtils.isNotEmpty(relationDto.getIds())) {
            List<MenuRole> list = new ArrayList<>();
            String[] ids = relationDto.getIds().split(GlobalConstant.SYMBOL_COMMA);
            List<Integer> idList = menuRoleMapper.getRelationRoles(relationDto.getId());
            MenuRole menuRole;
            for (String id : ids) {
                if (idList.contains(Integer.parseInt(id))) {
                    continue;
                }
                menuRole = new MenuRole();
                menuRole.setRoleId(Integer.parseInt(id));
                menuRole.setMenuId(relationDto.getId());
                list.add(menuRole);
            }
            if (!list.isEmpty()) {
                menuRoleMapper.addRelations(list);
            }
        }
    }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值