Skip to content

Commit 819ae31

Browse files
chachaektdynamic
authored andcommitted
update:完成角色权限接口
1 parent 93a1836 commit 819ae31

28 files changed

+743
-49
lines changed

mybatis-plus/src/main/java/com/chachae/bean/REnum.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public enum REnum {
1212
// 接口请求成功
1313
SUCCESS(200, "API request success"),
1414
// 接口请求失败
15-
FAIL(400, "API request fail");
15+
FAIL(400, "API request fail"),
16+
// 服务器异常
17+
SERVER_ERROR(500, "Internal Server Error");
1618

1719
/** 取值 */
1820
private final Integer value;

mybatis-plus/src/main/java/com/chachae/bean/Result.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
import com.chachae.util.DateUtil;
66
import lombok.Data;
77
import lombok.EqualsAndHashCode;
8-
import org.springframework.http.HttpStatus;
8+
import lombok.NoArgsConstructor;
99

1010
/**
1111
* @author chachae
1212
* @date 2019/12/17 10:27
1313
*/
14-
@Data
1514
@EqualsAndHashCode(callSuper = true)
15+
@Data
16+
@NoArgsConstructor
1617
public class Result<T> extends Model<Result<T>> {
1718

1819
private Integer code;
19-
private T data;
2020
private String msg;
2121
private String time;
22+
private T data;
2223

2324
/**
2425
* 普通返回结果
@@ -29,7 +30,7 @@ public class Result<T> extends Model<Result<T>> {
2930
*/
3031
public static <T> Result<T> ok(T t) {
3132
Result<T> result = new Result<>();
32-
result.code = HttpStatus.OK.value();
33+
result.code = REnum.SUCCESS.value();
3334
result.msg = REnum.SUCCESS.desc();
3435
result.time = DateUtil.now();
3536
result.data = t;
@@ -52,4 +53,22 @@ public static <T> Result<T> ok(IPage<T> t) {
5253
result.data = (T) PageResult.warp(t);
5354
return result;
5455
}
56+
57+
public static <T> Result<T> fail(T t) {
58+
Result<T> result = new Result<>();
59+
result.code = REnum.FAIL.value();
60+
result.msg = REnum.FAIL.desc();
61+
result.time = DateUtil.now();
62+
result.data = t;
63+
return result;
64+
}
65+
66+
public static <T> Result<T> fail(T t, Integer code) {
67+
Result<T> result = new Result<>();
68+
result.code = code;
69+
result.msg = REnum.FAIL.desc();
70+
result.time = DateUtil.now();
71+
result.data = t;
72+
return result;
73+
}
5574
}

mybatis-plus/src/main/java/com/chachae/config/MyBatisPlusConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.chachae.config;
22

3+
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
34
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
45
import org.mybatis.spring.annotation.MapperScan;
56
import org.springframework.context.annotation.Bean;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.chachae.controller;
2+
3+
import com.baomidou.mybatisplus.core.metadata.IPage;
4+
import com.chachae.bean.Page;
5+
import com.chachae.bean.Result;
6+
import com.chachae.entity.bo.Permission;
7+
import com.chachae.entity.dto.PermissionDTO;
8+
import com.chachae.service.PermissionService;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
import javax.annotation.Resource;
12+
import javax.validation.Valid;
13+
14+
/**
15+
* @author chachae
16+
* @date 2019/12/19 21:15
17+
*/
18+
@RestController
19+
@RequestMapping("/permission")
20+
public class PermissionController {
21+
22+
@Resource private PermissionService permissionService;
23+
24+
@GetMapping("/list")
25+
public Result<Permission> list(Page<Permission> page, PermissionDTO dto) {
26+
IPage<Permission> result = this.permissionService.selectPage(page, dto);
27+
return Result.ok(result);
28+
}
29+
30+
@PostMapping("/save")
31+
public Result<Boolean> save(@Valid Permission permission) {
32+
boolean res = this.permissionService.save(permission);
33+
return Result.ok(res);
34+
}
35+
36+
@PutMapping("/update")
37+
public Result<Boolean> update(@Valid Permission permission) {
38+
boolean res = this.permissionService.updateById(permission);
39+
return Result.ok(res);
40+
}
41+
42+
@DeleteMapping("/delete/{id}")
43+
public Result<Boolean> delete(@PathVariable Long id) {
44+
boolean res = this.permissionService.removeById(id);
45+
return Result.ok(res);
46+
}
47+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.chachae.controller;
2+
3+
import com.baomidou.mybatisplus.core.metadata.IPage;
4+
import com.chachae.bean.Page;
5+
import com.chachae.bean.Result;
6+
import com.chachae.entity.bo.Role;
7+
import com.chachae.entity.dto.RoleDTO;
8+
import com.chachae.service.RoleService;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
import javax.annotation.Resource;
12+
13+
/**
14+
* @author chachae
15+
* @date 2019/12/19 13:46
16+
*/
17+
@RestController
18+
@RequestMapping("/role")
19+
public class RoleController {
20+
21+
@Resource private RoleService roleService;
22+
23+
@GetMapping("/list")
24+
public Result<Role> list(Page<Role> page, RoleDTO dto) {
25+
IPage<Role> result = this.roleService.selectPage(page, dto);
26+
return Result.ok(result);
27+
}
28+
29+
@PostMapping("/save")
30+
public Result<Boolean> save(Role role, Long[] ids) {
31+
boolean res = this.roleService.save(role, ids);
32+
return Result.ok(res);
33+
}
34+
35+
@DeleteMapping("/delete/{id}")
36+
public Result<Boolean> delete(@PathVariable Long id) {
37+
boolean res = this.roleService.removeById(id);
38+
return Result.ok(res);
39+
}
40+
41+
@PutMapping("/update")
42+
public Result<Boolean> update(Role role, Long[] ids) {
43+
boolean res = this.roleService.updateById(role, ids);
44+
return Result.ok(res);
45+
}
46+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.chachae.controller;
2+
3+
import com.chachae.exceptions.ApiException;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
/**
9+
* @author chachae
10+
* @date 2019/12/19 19:26
11+
*/
12+
@RestController
13+
@RequestMapping("/test")
14+
public class TestController {
15+
16+
@GetMapping("/exception")
17+
public void exception(String args) {
18+
try {
19+
int a = Integer.valueOf(args) / 0;
20+
} catch (Exception e) {
21+
throw ApiException.argError("参数异常");
22+
}
23+
}
24+
}

mybatis-plus/src/main/java/com/chachae/controller/UserController.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.web.bind.annotation.*;
1010

1111
import javax.annotation.Resource;
12+
import javax.validation.Valid;
1213

1314
/**
1415
* @author chachae
@@ -20,22 +21,29 @@ public class UserController {
2021

2122
@Resource private UserService userService;
2223

24+
/**
25+
* 模糊查询
26+
*
27+
* @param page 分页参数
28+
* @param dto 模糊条件
29+
* @return 分页结果
30+
*/
2331
@GetMapping("/list")
2432
public Result<User> list(Page<User> page, UserDTO dto) {
2533
IPage<User> result = this.userService.selectPage(page, dto);
2634
return Result.ok(result);
2735
}
2836

2937
@PostMapping("/save")
30-
public Result<User> save(User user) {
31-
this.userService.save(user);
32-
return Result.ok(user);
38+
public Result<Boolean> save(@Valid User user, Long[] ids) {
39+
boolean res = this.userService.save(user, ids);
40+
return Result.ok(res);
3341
}
3442

3543
@PutMapping("/update")
36-
public Result<User> update(User user) {
37-
this.userService.updateById(user);
38-
return Result.ok(user);
44+
public Result<Boolean> update(@Valid User user, Long[] ids) {
45+
boolean res = this.userService.updateById(user, ids);
46+
return Result.ok(res);
3947
}
4048

4149
@DeleteMapping("/delete/{id}")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.chachae.dao;
2+
3+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4+
import com.chachae.entity.bo.Permission;
5+
6+
/**
7+
* @author chachae
8+
* @date 2019/12/19 21:12
9+
*/
10+
public interface PermissionDAO extends BaseMapper<Permission> {}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.chachae.dao;
2+
3+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4+
import com.chachae.entity.bo.Role;
5+
import org.apache.ibatis.annotations.Param;
6+
7+
/**
8+
* @author chachae
9+
* @date 2019/12/19 13:39
10+
*/
11+
public interface RoleDAO extends BaseMapper<Role> {
12+
13+
/**
14+
* 新增角色权限中间表数据
15+
*
16+
* @param roleId 角色id
17+
* @param ids 权限id 集合
18+
* @return boolean
19+
*/
20+
boolean saveRelation(@Param("roleId") Long roleId, @Param("permissionId") Long ids);
21+
22+
/**
23+
* 删除角色权限中间表数据
24+
*
25+
* @param roleId 角色id
26+
* @return boolean
27+
*/
28+
boolean removeRelation(@Param("roleId") Long roleId);
29+
30+
/**
31+
* 通过权限id 删除角色权限中间表数据
32+
*
33+
* @param permissionId 权限id
34+
* @return boolean
35+
*/
36+
boolean removeRelationByPermissionId(@Param("permissionId") Long permissionId);
37+
}

mybatis-plus/src/main/java/com/chachae/dao/UserDAO.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,28 @@
22

33
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
44
import com.chachae.entity.bo.User;
5+
import org.apache.ibatis.annotations.Param;
56

67
/**
78
* @author chachae
89
* @date 2019/12/16 14:37
910
*/
10-
public interface UserDAO extends BaseMapper<User> {}
11+
public interface UserDAO extends BaseMapper<User> {
12+
13+
/**
14+
* 新增用户角色中间表数据
15+
*
16+
* @param userId 用户id
17+
* @param ids 角色id 集合
18+
* @return boolean
19+
*/
20+
boolean saveRelation(@Param("userId") Long userId, @Param("roleId") Long ids);
21+
22+
/**
23+
* 删除用户角色中间表数据
24+
*
25+
* @param userId 用户id
26+
* @return boolean
27+
*/
28+
boolean removeRelation(@Param("userId") Long userId);
29+
}

0 commit comments

Comments
 (0)