<!--代码内容-->
@Override
public Set<String> findPermissions(String username) {
//TODO 此处可以优化,比如查询到role后,一起获取roleId,然后直接根据roleId获取即可
String sql = "select permission from sys_users u, sys_roles r, sys_permissions p, sys_users_roles ur, sys_roles_permissions rp where u.username=? and u.id=ur.user_id and r.id=ur.role_id and r.id=rp.role_id and p.id=rp.permission_id";
return new HashSet(jdbcTemplate.queryForList(sql, String.class, username));
}
<!--这一句有警告-->
return new HashSet(jdbcTemplate.queryForList(sql, String.class, username));
<!--警告内容-->
Unchecked assignment: 'java.util.HashSet' to 'java.util.Set<java.lang.String>'. Unchecked call to 'HashSet(Collection<? extends E>)' as a member of raw type 'java.util.HashSet'.
上百度查询,无果
上Stack Overflow查询,给出的答案是如何让idea不再警告
这显然不是我想要的答案,面对警告肯定有解决的办法,而不是掩盖警告,这不是掩耳盗铃么
查询了一堆关于泛型相关的文章之后,决定拆开来写:
@Override
public Set<String> findPermissions(String username) {
//TODO 此处可以优化,比如查询到role后,一起获取roleId,然后直接根据roleId获取即可
String sql = "select permission from sys_users u, sys_roles r, sys_permissions p, sys_users_roles ur, sys_roles_permissions rp where u.username=? and u.id=ur.user_id and r.id=ur.role_id and r.id=rp.role_id and p.id=rp.permission_id";
List<String> list = jdbcTemplate.queryForList(sql, String.class, username;
Set<String> set = new HashSet<>(list);
return set;
}
<!--关键代码:-->
List<String> list = jdbcTemplate.queryForList(sql, String.class, username;
Set<String> set = new HashSet<>(list);
return set;
原来仅仅是少写了一对尖括号<>
同样的还有:
List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper(User.class), username);
<!--改为:-->
List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class), username);