添加新角色是在弹窗页面里面填写角色的信息,其中设置角色关联的权限,用到了ElementUI组件库的穿梭框。我们选中的权限,最终是以数组的形式保存到前端页面,提交到后端Web方法也是数组格式的权限ID值。
在TbRoleDao.xml
文件中,定义插入记录的INSERT语句。
<insert id="insert" parameterType="com.example.emos.api.db.pojo.TbRole"> INSERT INTO tb_role SET role_name=#{roleName}, permissions=#{permissions} <if test="desc!=null"> ,`desc`=#{desc} </if> </insert>
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
在TbRoleDao.java
接口中,声明抽象DAO方法。
public interface TbRoleDao { …… public int insert(TbRole role); }
代码块预览 复制
- 1
- 2
- 3
- 4
在RoleService.java
接口中,定义抽象方法。
public interface RoleService{ …… public int insert(TbRole role); }
代码块预览 复制
- 1
- 2
- 3
- 4
在RoleServiceImpl.java
类中,实现抽象方法。
public class RoleServiceImpl implements RoleService { …… @Override public int insert(TbRole role) { int rows = roleDao.insert(role); return rows; } }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
创建InsertRoleForm.java
类,用于封装Ajax提交的数据。
@Data @Schema(description = "添加角色表单") public class InsertRoleForm { @NotBlank(message = "roleName不能为空") @Schema(description = "角色名称") private String roleName; @NotEmpty(message = "permissions不能为空") @Schema(description = "权限") private Integer[] permissions; @Length(max = 20,message = "desc不能超过20个字符") @Schema(description = "备注") private String desc; }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
在RoleController.java
类中,定义Web方法。利用Swagger先登录系统,然后测试Web方法。
public class RoleController { …… @PostMapping("/insert") @Operation(summary = "添加角色") @SaCheckPermission(value = {"ROOT", "ROLE:INSERT"}, mode = SaMode.OR) public R insert(@Valid @RequestBody InsertRoleForm form) { TbRole role = new TbRole(); role.setRoleName(form.getRoleName()); role.setPermissions(JSONUtil.parseArray(form.getPermissions()).toString()); role.setDesc(form.getDesc()); int rows = roleService.insert(role); return R.ok().put("rows", rows); } }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14