你见过一个类中注入很多的bean吗?比如说像这样
这个是接着上次的文章,虽然也是用程序实习关联的一种解决方案
再来看看Controller中的方法 (service中的方法就是上次文章中的方法体中的方法)
/**
* 查询该用户下所有的权限
*/
@RequestMapping("/queryauth")
public List<String> queryUserAuthorities(User user){
UserVo userVo = userService.queryAuth(user);
List<String> authorityList = new ArrayList<>();
HashSet<RoleVo> roleVos = userVo.getRoleVos();
Iterator<RoleVo> iterator = roleVos.iterator();
while (iterator.hasNext()){
RoleVo next = iterator.next();
HashSet<AuthorityVo> authorityVos = next.getAuthorityVos();
Iterator<AuthorityVo> it = authorityVos.iterator();
while (it.hasNext()){
AuthorityVo authorityVo = it.next();
authorityList.add(authorityVo.getAname());
}
}
return authorityList;
}
看起来是不是有点南辕北辙的感觉了,所以上次预留的连接查询的接口就有用了(这里有个细节就是在创建意义上的中间表的时候,具体请去查看上一篇文章)
测试结果:
Controller
@RequestMapping("/selectauth")
public List<Authority> selectUserAuthorities(User user){
List<Authority> authorityList = userService.selectUserAuth(user);
return authorityList;
}
Service
@Override
public List<Authority> selectUserAuth(User user) {
Integer uid = user.getUid();
List<Authority> authorityList = new ArrayList<>();
List<Integer> aids = userRoleMapper.selectUserAuth(uid);
for (Integer id:aids
) {
Authority authority = authorityMapper.getAuthorityById(id);
authorityList.add(authority);
}
return authorityList;
}
Mapper:
<select id="selectUserAuth" parameterType="INTEGER" resultType="integer">
select t_role_authority.aid from t_user_role inner JOIN
t_role_authority on t_user_role.rid = t_role_authority.rid where uid=#{uid};
</select>
一样的实现了 查询该用户下拥有的所有权限