工作CRUD模板(个人笔记)

1.分页列表查询list

@PostMapping(value = "/list")
public BaseResponse list(@RequestBody ResourcesSearchRequest request){
   try {
      //当前登录人
      request.setLoginUserId(this.getLoginUserId().toString());
      searchCondition.setSearchBean(request);
      PageInfo<ResourcesModel> rows = resourcesService.selectListPage(searchCondition);
      List<ResourcesVO> list = new ArrayList<>();
      for(ResourcesModel model : rows.getList()){
         ResourcesVO vo = new ResourcesVO();
         BeanUtils.copyProperties(model, vo);
         //状态
         if (StringUtils.isNotEmpty(model.getResourcesStatus())) {
            vo.setResourcesStatusName(codeUtils.getCodeNameByParentId(1038L, model.getResourcesStatus()));
         }
         list.add(vo);
      }
      pageResponse.setData(list, rows);
   } catch (SystemException e) {
      logger.error(e.getMessage());
      pageResponse.error(e.getMessage());
   } catch (Exception e) {
      pageResponse.error();
      logger.error(e.getMessage(), e);
   }
   return pageResponse;
}

2.查询单个detail

@GetMapping(value = "/detail/{id}")
public BaseResponse detail(@PathVariable(value = "id") String id){
   try {
      ResourcesModel model = resourcesService.selectByPrimaryKey(id);
      ResourcesVO vo = new ResourcesVO();
      BeanUtils.copyProperties(model, vo);

      //状态
      if (StringUtils.isNotEmpty(model.getResourcesStatus())) {
         vo.setResourcesStatusName(codeUtils.getCodeNameByParentId(1038L, model.getResourcesStatus()));
      }
      dataResponse.setData(vo);
   } catch (SystemException e) {
      logger.error(e.getMessage());
      dataResponse.error(e.getMessage());
   } catch (Exception e) {
      dataResponse.error();
      logger.error(e.getMessage(), e);
   }
   return dataResponse;
}

3.新增add

@PostMapping(value = "/add")
public BaseResponse add(@RequestBody ResourcesCreateRequest request) {
   try {
      LoginUserBean user = this.getLoginUser();
      baseResponse = resourcesService.add(request, user);
   } catch (SystemException e) {
      logger.error(e.getMessage());
      baseResponse.error(e.getMessage());
   } catch (Exception e) {
      baseResponse.error(e.getMessage());
      logger.error(e.getMessage(), e);
   }
   return baseResponse;
}

4.更新update

@PostMapping(value = "/update")
public BaseResponse update(@RequestBody ResourcesUpdateRequest request) {
   try {
      LoginUserBean user = this.getLoginUser();
      baseResponse = resourcesService.update(request, user);
   } catch (SystemException e) {
      logger.error(e.getMessage());
      baseResponse.error(e.getMessage());
   } catch (Exception e) {
      baseResponse.error(e.getMessage());
      logger.error(e.getMessage(), e);
   }
   return baseResponse;
}

5.删除delete

@PostMapping(value = "/delete")
public BaseResponse delete(@RequestParam("id") String id) {
   try {
      baseResponse = resourcesService.deleteByPrimaryKey(id);
   } catch (SystemException e) {
      logger.error(e.getMessage());
      baseResponse.error(e.getMessage());
   } catch (Exception e) {
      baseResponse.error();
      logger.error(e.getMessage(), e);
   }
   return baseResponse;
}

你可能感兴趣的:(工作CRUD模板(个人笔记))