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("添加成功");中user报错
时间: 2024-04-14 22:28:12 浏览: 129
如果在使用上述代码时,`user`报错了,可能是因为`user`对象没有正确的赋值或者不存在。请确保`user`对象已经正确地初始化,并且包含了`phone`属性。
你可以添加以下代码片段进行调试:
```java
System.out.println("user: " + user);
```
这将打印出`user`对象的值,以便你确认是否正确获取了`phone`属性。
如果仍然存在问题,请检查代码的其他部分,确保`user`对象已经正确地获取到并且包含了有效的值。
相关问题
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的方法
如果你想将这段代码改成使用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,并且引入了相关的依赖。
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方法,怎么优化
如果要将上述代码优化为使用`list`方法而不使用`updateCustom`方法,可以考虑使用MyBatis的批量插入功能来实现。
首先,在Mapper接口中定义一个插入方法,如下所示:
```java
void insertModulePermissionsList(List<TbModulePermissions> permissionsList);
```
然后,在Mapper文件中编写插入语句,如下所示:
```xml
<insert id="insertModulePermissionsList" parameterType="java.util.List">
INSERT INTO tb_module_permissions (
create_time, module_id, phone, p_id,
sort_id, status, update_opr, update_time,
is_person
)
SELECT
#{createTime}, #{moduleId}, phone, 1,
#{sortId}, #{moduleStatus}, phone, #{createTime},
#{isPerson}
FROM tb_user_info
WHERE isDelete = 0
AND instr(#{le}, level) > 0
AND instr(#{channel}, channel_id) > 0
</insert>
```
接下来,在Service层调用插入方法,如下所示:
```java
List<TbModulePermissions> permissionsList = new ArrayList<>();
TbModulePermissions permissions = new TbModulePermissions();
permissions.setCreateTime(tbModuleInfo.getCreateTime());
permissions.setModuleId(tbModuleInfo.getModuleId());
permissions.setPhone("phone");
permissions.setPId(1);
permissions.setSortId(tbModuleInfo.getSortId());
permissions.setStatus(tbModuleInfo.getModuleStatus());
permissions.setUpdateOpr("phone");
permissions.setUpdateTime(tbModuleInfo.getCreateTime());
permissions.setIsPerson(tbModuleInfo.getIsPerson());
permissionsList.add(permissions);
tbModulePermissionsService.insertModulePermissionsList(permissionsList);
```
通过使用批量插入的方式,将需要插入的数据封装到一个`List`中,并调用插入方法来一次性插入多条数据。这样可以减少与数据库的交互次数,提高插入效率。
请注意,在使用批量插入时,需要根据具体的业务需求来确定如何构建`TbModulePermissions`对象的集合,并将其传递给插入方法。
阅读全文