请求地址:localhost:8989/SmartPush/Users

请求参数:{“department_id”:[41,42]}

1.先定义实体类

  1. @Data
  2. public class PushUser {
  3. private Integer id;
  4. private String name;
  5. private Integer department_id;
  6. private String phone;
  7. private Integer height;
  8. private Integer weight;
  9. @JsonIgnore //此注解用于隐藏该字段,不向前端返回
  10. private Date birthday;
  11. private Integer age;
  12. private Integer sex;
  13. }

2.再定义一个实体类,用于存放传入的数组型的id

  1. @Data
  2. public class RequestDepartmentID {
  3. private ArrayList<Integer> department_id;
  4. }

3.Controller

  1. @Slf4j
  2. @RestController
  3. @RequestMapping("/SmartPush")
  4. public class SmartPushController {
  5. @Autowired
  6. private SmartPushService smartPushService;
  7. @PostMapping("/Users")
  8. public ResponseData SmartPushUsers(@RequestBody RequestDepartmentID ReqID) {
  9. ResponseData responseData = new ResponseData();
  10. ArrayList<Integer> deptid = ReqID.getDepartment_id();
  11. Collections.sort(deptid);
  12. if (deptid.size()<=0) {
  13. responseData.setCode(-1);
  14. responseData.setMessage("该部门信息有误");
  15. responseData.setData(null);
  16. return responseData;
  17. } else {
  18. responseData.setData(smartPushService.getUsersByDepartmentid(deptid));
  19. responseData.setCode(200);
  20. return responseData;
  21. }
  22. }
  23. }

4.Service

  1. @Service
  2. public class SmartPushService {
  3. @Autowired
  4. private TaUserDao taUserDao;
  5. public List<PushUser> getUsersByDepartmentid(ArrayList<Integer> department_id){
  6. List<PushUser> list = new ArrayList<>();
  7. if (department_id != null) {
  8. List<PushUser> users = taUserDao.getUsersByDepartment_id(department_id);
  9. //lamda表达式,将users集合按人员身高从高到低排序,关键方法:.reversed
  10. list = users.stream()
  11. .sorted(Comparator.comparing(PushUser::getHeight)
  12. .reversed()).collect(Collectors.toList());
  13. }
  14. return list;
  15. }
  16. }

5.Dao

  1. @Mapper
  2. public interface TaUserDao {
  3. List<PushUser> getUsersByDepartment_id(ArrayList<Integer> department_id);
  4. }

6.XML

  1. <!--关键方法:<foreach> 里面的东西不用改,直接复用-->
  2. <select id="getUsersByDepartment_id" resultType="com.jfsystem.manage.dwpc.model.PushUser">
  3. select id,name,height,department_id,weight,TIMESTAMPDIFF(YEAR, birthday, CURDATE()) as age,phone,sex FROM ta_user where department_id in
  4. <foreach collection="list" open="(" close=")" separator="," item="item">
  5. #{item}
  6. </foreach>
  7. ORDER BY RAND() LIMIT 6;
  8. </select>

7.返回的数据

  1. {
  2. "code": 0,
  3. "message": null,
  4. "data": [
  5. {
  6. "id": 48,
  7. "name": "李伟",
  8. "department_id": 42,
  9. "phone": "18747205204",
  10. "height": 175,
  11. "weight": 80,
  12. "age": 30,
  13. "sex": 1
  14. },
  15. {
  16. "id": 99,
  17. "name": "常威",
  18. "department_id": 41,
  19. "phone": "13039586119",
  20. "height": 173,
  21. "weight": 146,
  22. "age": 25,
  23. "sex": 1
  24. },
  25. {
  26. "id": 87,
  27. "name": "曹操",
  28. "department_id": 42,
  29. "phone": "18648467050",
  30. "height": 171,
  31. "weight": 74,
  32. "age": 23,
  33. "sex": 1
  34. },
  35. {
  36. "id": 69,
  37. "name": "王凯",
  38. "department_id": 42,
  39. "phone": "17604875674",
  40. "height": 169,
  41. "weight": 140,
  42. "age": 24,
  43. "sex": 1
  44. },
  45. {
  46. "id": 133,
  47. "name": "刘磊",
  48. "department_id": 41,
  49. "phone": "17604854980",
  50. "height": 166,
  51. "weight": 72,
  52. "age": 20,
  53. "sex": 1
  54. },
  55. {
  56. "id": 123,
  57. "name": "张讯",
  58. "department_id": 42,
  59. "phone": "18586185119",
  60. "height": 165,
  61. "weight": 66,
  62. "age": 24,
  63. "sex": 1
  64. }
  65. ]
  66. }