tbModuleInfo.setCreateTime(DateUtil.dateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS)); tbModuleInfo.setIsDel(0); tbModuleInfo.setModuleId("MI" + UUID.randomUUID().toString().split("-")[4].toUpperCase()); int i = tbModuleInfoService.insert(tbModuleInfo); if (i <= 0) { return getSucessMap("模块添加失败"); } Subject subject=SecurityUtils.getSubject();//获得当前用户到登录对象,现在状态为未认证 System.out.println("RealmForDouble授权中---->用户:"+subject); Map<String, Object> user = (Map<String, Object>) subject.getPrincipal(); TbModulePermissions mp1 = new TbModulePermissions(); mp1.setCreateTime(tbModuleInfo.getCreateTime()); mp1.setModuleId(tbModuleInfo.getModuleId()); mp1.setPhone(user.get("phone").toString()); mp1.setpId(0); mp1.setSortId(tbModuleInfo.getSortId()); mp1.setStatus(tbModuleInfo.getModuleStatus()); mp1.setUpdateOpr(user.get("phone").toString()); mp1.setUpdateTime(tbModuleInfo.getCreateTime()); mp1.setIsPerson(tbModuleInfo.getIsPerson()); int n = tbModulePermissionsService.insert(mp1); if (n <= 0) { return getSucessMap("省级模块添加失败"); } 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() }); log.info(tbModuleInfo.getModuleId()+"添加成功s="+s); return getSucessMap("添加成功");怎么改成List方法,不用Custom的方法
时间: 2024-04-14 08:28:10 浏览: 134
03数据库名5.10
如果你想将这段代码改成使用List的方法,而不使用Custom的方法,可以考虑使用批量插入的方式。你可以将需要插入的数据封装成一个List,然后使用JdbcTemplate的`batchUpdate`方法执行批量插入操作。以下是示例代码:
```java
List<TbModulePermissions> batchPermissions = new ArrayList<>();
TbModulePermissions mp1 = new TbModulePermissions();
mp1.setCreateTime(tbModuleInfo.getCreateTime());
mp1.setModuleId(tbModuleInfo.getModuleId());
mp1.setPhone(user.get("phone").toString());
mp1.setpId(0);
mp1.setSortId(tbModuleInfo.getSortId());
mp1.setStatus(tbModuleInfo.getModuleStatus());
mp1.setUpdateOpr(user.get("phone").toString());
mp1.setUpdateTime(tbModuleInfo.getCreateTime());
mp1.setIsPerson(tbModuleInfo.getIsPerson());
batchPermissions.add(mp1);
String sql = "INSERT INTO tb_module_permissions ("
+ "create_time,module_id,phone,p_id,"
+ "sort_id,status,update_opr,update_time,"
+ "is_person) "
+ "(SELECT "
+ "? ,? ,phone,1,"
+ "? ,? ,phone,? ,"
+ "? from tb_user_info where isDelete = 0 and instr(?, level) > 0 and instr(?, channel_id) > 0)";
int[] result = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
TbModulePermissions permissions = batchPermissions.get(i);
ps.setString(1, permissions.getCreateTime());
ps.setString(2, permissions.getModuleId());
ps.setString(3, permissions.getSortId());
ps.setString(4, permissions.getStatus());
ps.setString(5, permissions.getUpdateTime());
ps.setString(6, permissions.getIsPerson());
ps.setString(7, permissions.getLe());
ps.setString(8, permissions.getChannel());
}
@Override
public int getBatchSize() {
return batchPermissions.size();
}
});
if (result.length <= 0) {
return getSucessMap("省级模块添加失败");
}
log.info(tbModuleInfo.getModuleId() + "添加成功s=" + result.length);
return getSucessMap("添加成功");
```
在这段代码中,我们首先创建了一个List对象 `batchPermissions`,用于存储每个待插入的TbModulePermissions对象。然后,将需要插入的TbModulePermissions对象添加到`batchPermissions`中。
接下来,我们定义了插入语句 `sql`,使用`?`作为占位符。然后,使用`jdbcTemplate.batchUpdate`方法执行批量插入操作。在BatchPreparedStatementSetter的实现中,我们通过`setValues`方法将每个TbModulePermissions对象的属性值设置到PreparedStatement中。同时,通过`getBatchSize`方法返回批量插入的大小。
最后,根据返回的结果数组result判断插入是否成功,如果长度小于等于0,则表示省级模块添加失败。
请注意,在使用该方法之前,确保已经正确配置了JdbcTemplate,并且引入了相关的依赖。
阅读全文