int s = tbModulePermissionsService.updateCustom("INSERT INTO tb_module_permissions (" + "create_time,module_id,phone,p_id," + "sort_id,status,update_opr,update_time," + "is_person) " + "(SELECT " + "#{p[0]},#{p[1]},phone,1," + "#{p[2]},#{p[3]},phone,#{p[4]}," + "#{p[5]} from tb_user_info where isDelete = 0 and instr(#{p[6]}, level) > 0 and instr(#{p[7]}, channel_id) > 0)", new Object[]{ tbModuleInfo.getCreateTime(), tbModuleInfo.getModuleId(), tbModuleInfo.getSortId(), tbModuleInfo.getModuleStatus(),tbModuleInfo.getCreateTime(), tbModuleInfo.getIsPerson(), tbModuleInfo.getLe(),tbModuleInfo.getChannel() });不要updateCustom方法,要用list方法,怎么优化
时间: 2024-04-13 16:30:48 浏览: 21
您可以使用批量插入的方式来优化代码。首先,将要插入的数据组织成一个列表,每个元素表示一条记录。然后,使用`tbModulePermissionsService.insertBatch`方法来进行批量插入。
下面是示例代码:
```java
List<TbModulePermission> permissions = new ArrayList<>();
List<TbUserInfo> users = tbUserInfoService.getUserInfoByCondition(tbModuleInfo.getLe(), tbModuleInfo.getChannel());
for (TbUserInfo user : users) {
TbModulePermission permission = new TbModulePermission();
permission.setCreateTime(tbModuleInfo.getCreateTime());
permission.setModuleId(tbModuleInfo.getModuleId());
permission.setPhone(user.getPhone());
permission.setPId(1);
permission.setSortId(tbModuleInfo.getSortId());
permission.setStatus(tbModuleInfo.getModuleStatus());
permission.setUpdateOpr(user.getPhone());
permission.setUpdateTime(tbModuleInfo.getCreateTime());
permission.setIsPerson(tbModuleInfo.getIsPerson());
permissions.add(permission);
}
tbModulePermissionsService.insertBatch(permissions);
```
以上代码通过调用`tbUserInfoService.getUserInfoByCondition`方法获取符合条件的用户信息,然后根据每个用户创建对应的权限记录,并将所有权限记录添加到一个列表中。最后,通过调用`tbModulePermissionsService.insertBatch`方法将列表中的权限记录批量插入到数据库中。
请根据您的具体需求进行适当修改。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)