1. First create a new default role to be assigned
(1) You must log in with an administrator account to see role management
(2) Remember the role ID you want to set by default during registration, which is the role number in the figure below
2. Assign default roles to users
2.1 Configure the UserRoleMapper.xml file (SQL)
Insert the following statement in the UserRoleMapper.xml file
<insert id="insertDefaultRole" >
insert into sys_user_role(user_id,role_id)
values (#{userId},3)
</insert>
Note: the id should not conflict with the id of other statements, the second parameter of values is the role ID you want to set
2.2 Add a new method in UserRoleMapper.java
(1) Find this java file first (some comments say that it cannot be found, please add it)
(2) Declare the method in UserRoleMapper.java
public int insertDefaultRole(Long userId);
Note: The method name should be consistent with the id in UserRoleMapper.xml
2.3 Add a new method in IRoleService and implement the method in the implementation class
(1) First find these two java files
(2) Create a new method in IRoleService.java
public int insertDefaultRole(Long userId);
Note: The method name should be consistent with the id in UserRoleMapper.xml
(3) Implement the method in roleServiceImpl.java
@Override
public int insertDefaultRole(Long userId) {
return userRoleMapper.insertDefaultRole(userId);
}
Note: Be sure to write @Override, otherwise the controller will not recognize
3. Call the function that assigns the default role when registering
(1) First find RegisterController.java
(2) Call the function in the registration method
Others are provided by the Ruoyi registration function, and the picture frame needs to be added
roleService.insertDefaultRole(user.getUserId());
Note: roleService must be defined at the front
@Autowired
private IRoleService roleService;