You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.demo.service.impl;
import com.demo.dao.RoleMapper;
import com.demo.dao.UserRoleMapper;
import com.demo.pojo.*;
import com.demo.service.RoleService;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Service
public class RoleServiceImpl implements RoleService {
@Autowired
RoleMapper roleMapper;
@Autowired
UserRoleMapper userRoleMapper;
@Autowired
UserService userService;
@Override //name的所有角色
public Set<String> listRoleNames(String userName) {
Set<String> result = new HashSet<>();
//查出管理员相应的角色
List<Role> roles = listRoles(userName);
//把每个角色名放到set集合中
for (Role role : roles) {
result.add(role.getName());
}
return result;
}
@Override //查出管理员相应的角色
public List<Role> listRoles(String userName) {
List<Role> roles = new ArrayList<>();
//通过name获得user实体
User user = userService.getByName(userName);
if(null==user)
return roles;
roles = listRoles(user);//调用下面的方法
return roles;
}
@Override //查出管理员相应的角色
public List<Role> listRoles(User user) {
List<Role> roles = new ArrayList<>();
//通过管理员id获取管理员的角色
UserRoleExample example = new UserRoleExample();
example.createCriteria().andUidEqualTo(user.getId());
List<UserRole> userRoles = userRoleMapper.selectByExample(example);//关联查询 查出用户id对应的角色id的记录
//通过关联查询 查出表Role的给出指定主键id的记录
for (UserRole userRole : userRoles){
Role role = roleMapper.selectByPrimaryKey(userRole.getRid());
roles.add(role);
}
return roles;
}
@Override
public List<Role> list() {
RoleExample example =new RoleExample();
example.setOrderByClause("id desc");
return roleMapper.selectByExample(example);
}
@Override
public void add(Role role) {
roleMapper.insert(role);
}
@Override
public void delete(Long id) {
roleMapper.deleteByPrimaryKey(id);
}
@Override
public Role get(Long id) {
return roleMapper.selectByPrimaryKey(id);
}
@Override
public void update(Role role) {
roleMapper.updateByPrimaryKeySelective(role);
}
}