业务场景:表
group_ids字段中包含以逗号分隔的多个id,需要判断该字段中是否包含传入的id值。
一、表数据

需要添加判断处理的代码,此处使用了mybatis-plus的wrapper组件:
@PostMapping("list")fun groupList(@RequestBody saleGroupReq: SaleGroupReq): Result<IPage<SaleGroup>> {val result = Result<IPage<SaleGroup>>()val queryWrapper = QueryWrapper<SaleGroup>()val groups = saleGroupService.page(Page(saleGroupReq.getCurrentPage().toLong(), saleGroupReq.getPageSize().toLong()),queryWrapper)// TODO 此处需要添加判断role表中的group_ids字段是否包含group返回数据的idresult.content = groupsresult.status = -1result.message = "SUCCESS"return result}
二、代码处理
@PostMapping("list")fun groupList(@RequestBody saleGroupReq: SaleGroupReq): Result<IPage<SaleGroup>> {val result = Result<IPage<SaleGroup>>()val queryWrapper = QueryWrapper<SaleGroup>()val groups = saleGroupService.page(Page(saleGroupReq.getCurrentPage().toLong(), saleGroupReq.getPageSize().toLong()),queryWrapper)// 有id引用的情况下不允许删除groups.records.forEach { e ->val roleList = roleService.list(QueryWrapper<Role>().apply("FIND_IN_SET({0}, group_ids)", e.id))if (roleList.isNotEmpty()){e.delGroup = - 1}}result.content = groupsresult.status = -1result.message = "SUCCESS"return result}
三、函数详解
FIND_IN_SET(str, strlist)查询字段(strlist)中包含(str)的结果,返回结果为null或记录。
- str:要查询的字符串
- strlist:字段名 参数以 “,” 分隔,如 (1,2,6,8,10,22)
- 假如字符串str在由N个子链组成的字符串列表strlist 中,则返回值的范围在 1 到 N 之间。
- 如果str不在strlist 或strlist 为空字符串,则返回值为 0 。
- 如任意一个参数为NULL,则返回值为 NULL。
- 这个函数在第一个参数包含一个逗号(‘,’)时将无法正常运行。
