【7.0.3】整理mongodb模块代码

pull/10/head
fengshuonan 2021-04-13 21:57:41 +08:00
parent e2f1b04ce3
commit d34881696a
12 changed files with 49 additions and 51 deletions

View File

@ -26,6 +26,7 @@ package cn.stylefeng.roses.kernel.mongodb.api;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import org.springframework.web.multipart.MultipartFile;
import java.util.Optional;
/**
@ -34,7 +35,7 @@ import java.util.Optional;
* @author huziyang
* @date 2021/03/30 11:06
*/
public interface MongoFileApi<T,ID> {
public interface MongoFileApi<T, ID> {
/**
*
@ -46,7 +47,6 @@ public interface MongoFileApi<T,ID> {
*/
T saveFile(MultipartFile file);
/**
* id
*
@ -66,7 +66,6 @@ public interface MongoFileApi<T,ID> {
*/
Optional<T> getFileById(ID id);
/**
*
*
@ -77,5 +76,4 @@ public interface MongoFileApi<T,ID> {
*/
PageResult<T> getFilesByPage(T fileDocument);
}

View File

@ -33,7 +33,7 @@ import java.util.Optional;
* @author huziyang
* @date 2021/03/20 16:24
*/
public interface MongodbApi<T,ID> {
public interface MongodbApi<T, ID> {
/**
*

View File

@ -32,7 +32,6 @@ package cn.stylefeng.roses.kernel.mongodb.api.constants;
*/
public interface MongodbConstants {
/**
* mongodb
*/
@ -42,4 +41,5 @@ public interface MongodbConstants {
*
*/
String MONGODB_EXCEPTION_STEP_CODE = "70";
}

View File

@ -29,7 +29,7 @@ import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
/**
*
* Mongodb
*
* @author fengshuonan
* @date 2021/13/17 23:59

View File

@ -38,7 +38,6 @@ import org.springframework.stereotype.Controller;
@ApiResource(name = "MongoDB文件管理界面渲染控制器")
public class ModelViewController {
/**
*
*

View File

@ -38,6 +38,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@ -54,7 +55,7 @@ import java.util.Optional;
public class MongoFileController {
@Resource
private MongoFileApi<MongoFileEntity,String> mongoFileApi;
private MongoFileApi<MongoFileEntity, String> mongoFileApi;
/**
*
@ -63,7 +64,7 @@ public class MongoFileController {
* @date 2021/03/31 17:28
*/
@PostResource(name = "Mongodb文件新增", path = "/view/mongodb/file/add")
public ResponseData mongodbFileAdd(@RequestPart("file") MultipartFile file) {
public ResponseData mongodbFileAdd(@RequestPart("file") MultipartFile file) {
return new SuccessResponseData(mongoFileApi.saveFile(file));
}
@ -99,12 +100,12 @@ public class MongoFileController {
@GetResource(name = "Mongodb文件下载", path = "/view/mongodb/file/down")
public ResponseEntity mongodbFileDown(@RequestParam String id) throws UnsupportedEncodingException {
Optional<MongoFileEntity> file = mongoFileApi.getFileById(id);
if(file.isPresent()){
if (file.isPresent()) {
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; fileName=" + URLEncoder.encode(file.get().getName() , "utf-8"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; fileName=" + URLEncoder.encode(file.get().getName(), "utf-8"))
.header(HttpHeaders.CONTENT_TYPE, "application/octet-stream")
.body(file.get().getContent());
}else {
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("not found");
}
}

View File

@ -25,10 +25,7 @@
package cn.stylefeng.roses.kernel.mongodb.file.entity;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@ -40,12 +37,14 @@ import java.util.Date;
* @author huziyang
* @date 2021/03/26 17:23
*/
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Document("mongo_file")
public class MongoFileEntity extends BaseRequest {
@Id
private String id;
private String name;
@ -59,4 +58,5 @@ public class MongoFileEntity extends BaseRequest {
*
*/
private byte[] content;
}

View File

@ -27,6 +27,7 @@ package cn.stylefeng.roses.kernel.mongodb.file.service;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.mongodb.file.entity.MongoFileEntity;
import org.springframework.web.multipart.MultipartFile;
import java.util.Optional;
/**
@ -47,7 +48,6 @@ public interface MongoFileService {
*/
MongoFileEntity saveFile(MultipartFile file);
/**
* id
*
@ -67,7 +67,6 @@ public interface MongoFileService {
*/
Optional<MongoFileEntity> getFileById(String id);
/**
*
*
@ -78,5 +77,4 @@ public interface MongoFileService {
*/
PageResult<MongoFileEntity> getFilesByPage(MongoFileEntity fileDocument);
}

View File

@ -39,7 +39,6 @@ import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
@ -47,6 +46,8 @@ import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.Optional;
@ -59,14 +60,16 @@ import java.util.Optional;
*/
@Slf4j
@Service
public class MongoFileServiceImpl implements MongoFileService, MongoFileApi<MongoFileEntity,String> {
@Autowired
private MongoFileMapper mongoFileMapper;
@Autowired
private GridFsTemplate gridFsTemplate;
@Autowired
private GridFSBucket gridFSBucket;
public class MongoFileServiceImpl implements MongoFileService, MongoFileApi<MongoFileEntity, String> {
@Resource
private MongoFileMapper mongoFileMapper;
@Resource
private GridFsTemplate gridFsTemplate;
@Resource
private GridFSBucket gridFSBucket;
@Override
public MongoFileEntity saveFile(MultipartFile file) {
@ -77,7 +80,7 @@ public class MongoFileServiceImpl implements MongoFileService, MongoFileApi<Mong
// 填充登录用户的userId
LoginUser loginUser = LoginContext.me().getLoginUser();
fileDocument.setUploadUserId(loginUser.getUserId());
}catch (Exception e){
} catch (Exception e) {
// 获取不到用户登录信息,就不填充
}
fileDocument.setSuffix(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")));
@ -85,7 +88,7 @@ public class MongoFileServiceImpl implements MongoFileService, MongoFileApi<Mong
ObjectId store = gridFsTemplate.store(file.getInputStream(), IdUtil.simpleUUID(), file.getContentType());
fileDocument.setGridfsId(String.valueOf(store));
return mongoFileMapper.save(fileDocument);
}catch (IOException ex){
} catch (IOException ex) {
log.error(ex.getMessage());
}
return fileDocument;
@ -94,7 +97,7 @@ public class MongoFileServiceImpl implements MongoFileService, MongoFileApi<Mong
@Override
public void removeFile(String id) {
Optional<MongoFileEntity> fileDocumentOptional = mongoFileMapper.findById(id);
if(fileDocumentOptional.isPresent()){
if (fileDocumentOptional.isPresent()) {
mongoFileMapper.deleteById(id);
gridFsTemplate.delete(new Query().addCriteria(Criteria.where("_id").is(fileDocumentOptional.get().getGridfsId())));
}
@ -103,20 +106,20 @@ public class MongoFileServiceImpl implements MongoFileService, MongoFileApi<Mong
@Override
public Optional<MongoFileEntity> getFileById(String id) {
Optional<MongoFileEntity> fileDocumentOptional = mongoFileMapper.findById(id);
if(fileDocumentOptional.isPresent()){
if (fileDocumentOptional.isPresent()) {
MongoFileEntity fileDocument = fileDocumentOptional.get();
Query gridQuery = new Query().addCriteria(Criteria.where("_id").is(fileDocument.getGridfsId()));
GridFSFile fsFile = gridFsTemplate.findOne(gridQuery);
GridFSDownloadStream in = gridFSBucket.openDownloadStream(fsFile.getObjectId());
try {
if(in.getGridFSFile().getLength() > 0){
if (in.getGridFSFile().getLength() > 0) {
GridFsResource resource = new GridFsResource(fsFile, in);
fileDocument.setContent(IoUtil.readBytes(resource.getInputStream()));
return Optional.of(fileDocument);
}else {
} else {
return Optional.empty();
}
}catch (IOException e){
} catch (IOException e) {
log.error(e.getMessage());
}
}
@ -128,10 +131,10 @@ public class MongoFileServiceImpl implements MongoFileService, MongoFileApi<Mong
Integer pageIndex = fileDocument.getPageNo();
Integer pageSize = fileDocument.getPageSize();
Sort sort = Sort.by(Sort.Direction.DESC, "uploadDate");
PageRequest pageRequest = PageRequest.of(pageIndex-1, pageSize, sort);
PageRequest pageRequest = PageRequest.of(pageIndex - 1, pageSize, sort);
Example<MongoFileEntity> example = Example.of(fileDocument, ExampleMatcher.matching()
.withIgnoreCase(true)
.withIgnorePaths("_class","pageSize","pageNo","content")
.withIgnorePaths("_class", "pageSize", "pageNo", "content")
);
Page<MongoFileEntity> all = mongoFileMapper.findAll(example, pageRequest);
return PageResultFactory.createPageResult(all.getContent(), mongoFileMapper.count(example), pageSize, pageIndex);

View File

@ -29,6 +29,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.HashMap;
import java.util.Map;
@ -47,7 +48,6 @@ public class GunsMapEntity {
@Id
private String _id;
private Map<String,Object> data = new HashMap<>();
private Map<String, Object> data = new HashMap<>();
}

View File

@ -29,6 +29,7 @@ import cn.stylefeng.roses.kernel.mongodb.entity.GunsMapEntity;
import cn.stylefeng.roses.kernel.mongodb.mapper.GunsMapRepository;
import cn.stylefeng.roses.kernel.mongodb.service.GunsMapService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
@ -40,35 +41,33 @@ import java.util.Optional;
* @date 2021/03/20 16:24
*/
@Service
public class GunsMapServiceImpl implements GunsMapService, MongodbApi<GunsMapEntity,String> {
public class GunsMapServiceImpl implements GunsMapService, MongodbApi<GunsMapEntity, String> {
@Resource
private GunsMapRepository gunsMapRepository;
@Override
public GunsMapEntity insert(GunsMapEntity gunsMapEntity){
public GunsMapEntity insert(GunsMapEntity gunsMapEntity) {
return gunsMapRepository.insert(gunsMapEntity);
}
@Override
public GunsMapEntity update(GunsMapEntity gunsMapEntity){
public GunsMapEntity update(GunsMapEntity gunsMapEntity) {
return gunsMapRepository.save(gunsMapEntity);
}
@Override
public void deleteById(String id){
public void deleteById(String id) {
gunsMapRepository.deleteById(id);
}
@Override
public Optional<GunsMapEntity> findById(String id){
public Optional<GunsMapEntity> findById(String id) {
return gunsMapRepository.findById(id);
}
@Override
public List<GunsMapEntity> findAll(){
public List<GunsMapEntity> findAll() {
return gunsMapRepository.findAll();
}

View File

@ -26,6 +26,8 @@ package cn.stylefeng.roses.kernel.mongodb.starter;
import cn.stylefeng.roses.kernel.mongodb.api.MongoFileApi;
import cn.stylefeng.roses.kernel.mongodb.api.MongodbApi;
import cn.stylefeng.roses.kernel.mongodb.entity.GunsMapEntity;
import cn.stylefeng.roses.kernel.mongodb.file.entity.MongoFileEntity;
import cn.stylefeng.roses.kernel.mongodb.file.service.impl.MongoFileServiceImpl;
import cn.stylefeng.roses.kernel.mongodb.service.impl.GunsMapServiceImpl;
import org.springframework.context.annotation.Bean;
@ -40,7 +42,6 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class GunsMongodbAutoConfiguration {
/**
* Mongodb
*
@ -48,7 +49,7 @@ public class GunsMongodbAutoConfiguration {
* @date 2021/03/20 16:24
*/
@Bean
public MongodbApi mongodbApi() {
public MongodbApi<GunsMapEntity, String> mongodbApi() {
return new GunsMapServiceImpl();
}
@ -59,10 +60,9 @@ public class GunsMongodbAutoConfiguration {
* @date 2021/03/20 16:24
*/
@Bean
public MongoFileApi mongoFileApi() {
public MongoFileApi<MongoFileEntity, String> mongoFileApi() {
return new MongoFileServiceImpl();
}
}