一、项目一
一、文件上传
CommonsMultipartResolver的作用参考:https://blog.csdn.net/AlbenXie/article/details/101281161
MultipartResolver 用于处理文件上传,当收到请求时 DispatcherServlet 的 checkMultipart() 方法会调用 MultipartResolver 的 isMultipart() 方法判断请求中是否包含文件。如果请求数据中包含文件,则调用 MultipartResolver 的 resolveMultipart() 方法对请求的数据进行解析,然后将文件数据解析成 MultipartFile 并封装在 MultipartHttpServletRequest (继承了 HttpServletRequest) 对象中,最后传递给 Controller。
@RestController
@RequestMapping("/ad/asd")
public class UploadFileController {
@RequestMapping(value = "/upload",method = {POST})
@ResponseBody
@AnnotationAboutFile //自定义拦截器
public Response<IvsUploadResponse> upload(HttpServletRequest request, HttpServletResponse response){
response.setCharacterEncoding("UTF-8");
CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext());
if (!multipartResolver.isMultipart(request)){
throw new Exception("上传文件出错!");
}
MultipartHttpServletRequest multiRequest = multipartResolver.resolveMultipart(request);
List<MultipartFile> files = multiRequest.getFiles("files");
if (CollectionUtils.isEmpty(files)){
throw new Exception("请选择文件上传!");
}
files = files.stream().filter(file -> file.getSize() != 0).collect(Collectors.toList());
String fileTypeStr = multiRequest.getParameter("fileType");
List<EnumFileType> fileTypes = EnumFileType.getByGroup(fileTypeStr);
if (CollectionUtils.isEmpty(fileTypes)){
throw new Exception("文件类型错误!");
}
for (MultipartFile file : files) {
EnumFileType subFileType = EnumFileType.getByFileName(file.getOriginalFilename());
if (subFileType==null){
throw new Exception("文件名称和文件类型不匹配!");
}
if (!subFileType.getGroup().equals(fileTypeStr)){
throw new Exception("文件名称和文件类型不匹配!");
}
if (Objects.requireNonNull(file.getOriginalFilename()).toLowerCase().endsWith(subFileType.getSuffix())){
throw new Exception("文件格式错误,需要格式:"+subFileType.getSuffix());
}
}
List<FileUploadRecord> FileUploadRecordList=fileUploadRecordService.addFiles(files);
List<FileUploadResult> uploadResults=FileUploadRecordList.stream()
.map(this::convertToFileUploadRecord)
.collect(Collectors.toList());
IvsUploadResponse ivsUploadResponse = new IvsUploadResponse();
ivsUploadResponse.setFileUploadResultList(uploadResults);
return SuccessResponse.build(ivsUploadResponse);
}
}
二、分页查询(参考MybatisPlus中分页查询)
1. Controller:
@RestController
@RequestMapping("/ad/asd")
public class CoalController {
@Autowired
private CoalService coalService;
@RequestMapping
public Response<PagingQueryResultWrap<QueryCoalOutVo>> queryTransList(@Validated @RequestBody QueryCoalInVo coalInvo){
PagingQueryResultWrap<QueryCoalOutVo> coalList=coalService.queryCoalList(coalInvo);
return SuccessResponse.build(coalList);
}
}
2. Service:
@Service
public class CoalService {
/**
* 分页查询
*/
public PagingQueryResultWrap<QueryCoalOutVo> queryCoalList(QueryCoalInVo coalInVo){
QueryWrapper<Coal> queryWrapper= Wrappers.query();
queryWrapper.eq("IS_DEL",0);
if(StringUtils.isNotEmpty(coalInVo.getName())){
queryWrapper.like("NAME",coalInVo.getName());
}
IPage<Coal> page=new Page<>(coalInVo.getPageIndex(),coalInVo.getPageRowCount());
IPage<Coal> coalIPage=coalMapper.selectPage(page,queryWrapper);
PagingQueryResultWrap<Coal> queryResultWrap=new PagingQueryResultWrap<>();
queryResultWrap.setDbcount(Math.toIntExact(coalIPage.getTotal()));
queryResultWrap.setRecords(coalIPage.getRecords());
return SqlHelper.copy(queryResultWrap,QueryCoalOutVo.class);//.copy()自定义方法
}
}