diff --git a/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/MongoFileApi.java b/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/MongoFileApi.java index 0af3915f4..8d5b8d924 100644 --- a/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/MongoFileApi.java +++ b/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/MongoFileApi.java @@ -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 { +public interface MongoFileApi { /** * 保存文件 @@ -46,7 +47,6 @@ public interface MongoFileApi { */ T saveFile(MultipartFile file); - /** * 根据id删除文件 * @@ -66,7 +66,6 @@ public interface MongoFileApi { */ Optional getFileById(ID id); - /** * 分页获取文件列表 * @@ -77,5 +76,4 @@ public interface MongoFileApi { */ PageResult getFilesByPage(T fileDocument); - } diff --git a/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/MongodbApi.java b/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/MongodbApi.java index b52554cfd..fdc7b29db 100644 --- a/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/MongodbApi.java +++ b/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/MongodbApi.java @@ -33,7 +33,7 @@ import java.util.Optional; * @author huziyang * @date 2021/03/20 16:24 */ -public interface MongodbApi { +public interface MongodbApi { /** * 新增数据 diff --git a/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/constants/MongodbConstants.java b/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/constants/MongodbConstants.java index 5b919d4f4..b88ee4c0a 100644 --- a/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/constants/MongodbConstants.java +++ b/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/constants/MongodbConstants.java @@ -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"; + } diff --git a/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/exception/MongodbException.java b/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/exception/MongodbException.java index 089c03143..65bff9277 100644 --- a/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/exception/MongodbException.java +++ b/kernel-d-mongodb/mongodb-api/src/main/java/cn/stylefeng/roses/kernel/mongodb/api/exception/MongodbException.java @@ -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 diff --git a/kernel-d-mongodb/mongodb-integration-beetl/src/main/java/cn/stylefeng/roses/kernel/mongodb/integration/controller/ModelViewController.java b/kernel-d-mongodb/mongodb-integration-beetl/src/main/java/cn/stylefeng/roses/kernel/mongodb/integration/controller/ModelViewController.java index 91046c818..1762872ce 100644 --- a/kernel-d-mongodb/mongodb-integration-beetl/src/main/java/cn/stylefeng/roses/kernel/mongodb/integration/controller/ModelViewController.java +++ b/kernel-d-mongodb/mongodb-integration-beetl/src/main/java/cn/stylefeng/roses/kernel/mongodb/integration/controller/ModelViewController.java @@ -38,7 +38,6 @@ import org.springframework.stereotype.Controller; @ApiResource(name = "MongoDB文件管理界面渲染控制器") public class ModelViewController { - /** * 文件管理视图 * diff --git a/kernel-d-mongodb/mongodb-integration-beetl/src/main/java/cn/stylefeng/roses/kernel/mongodb/integration/controller/MongoFileController.java b/kernel-d-mongodb/mongodb-integration-beetl/src/main/java/cn/stylefeng/roses/kernel/mongodb/integration/controller/MongoFileController.java index 551d4842a..9dfc58707 100644 --- a/kernel-d-mongodb/mongodb-integration-beetl/src/main/java/cn/stylefeng/roses/kernel/mongodb/integration/controller/MongoFileController.java +++ b/kernel-d-mongodb/mongodb-integration-beetl/src/main/java/cn/stylefeng/roses/kernel/mongodb/integration/controller/MongoFileController.java @@ -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 mongoFileApi; + private MongoFileApi 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 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"); } } diff --git a/kernel-d-mongodb/mongodb-sdk-file/src/main/java/cn/stylefeng/roses/kernel/mongodb/file/entity/MongoFileEntity.java b/kernel-d-mongodb/mongodb-sdk-file/src/main/java/cn/stylefeng/roses/kernel/mongodb/file/entity/MongoFileEntity.java index 20b6d980e..bbd3e693c 100644 --- a/kernel-d-mongodb/mongodb-sdk-file/src/main/java/cn/stylefeng/roses/kernel/mongodb/file/entity/MongoFileEntity.java +++ b/kernel-d-mongodb/mongodb-sdk-file/src/main/java/cn/stylefeng/roses/kernel/mongodb/file/entity/MongoFileEntity.java @@ -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; + } diff --git a/kernel-d-mongodb/mongodb-sdk-file/src/main/java/cn/stylefeng/roses/kernel/mongodb/file/service/MongoFileService.java b/kernel-d-mongodb/mongodb-sdk-file/src/main/java/cn/stylefeng/roses/kernel/mongodb/file/service/MongoFileService.java index 1c6515b50..cf834bb2c 100644 --- a/kernel-d-mongodb/mongodb-sdk-file/src/main/java/cn/stylefeng/roses/kernel/mongodb/file/service/MongoFileService.java +++ b/kernel-d-mongodb/mongodb-sdk-file/src/main/java/cn/stylefeng/roses/kernel/mongodb/file/service/MongoFileService.java @@ -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 getFileById(String id); - /** * 分页获取文件列表 * @@ -78,5 +77,4 @@ public interface MongoFileService { */ PageResult getFilesByPage(MongoFileEntity fileDocument); - } diff --git a/kernel-d-mongodb/mongodb-sdk-file/src/main/java/cn/stylefeng/roses/kernel/mongodb/file/service/impl/MongoFileServiceImpl.java b/kernel-d-mongodb/mongodb-sdk-file/src/main/java/cn/stylefeng/roses/kernel/mongodb/file/service/impl/MongoFileServiceImpl.java index ab9891e98..56e3225b4 100644 --- a/kernel-d-mongodb/mongodb-sdk-file/src/main/java/cn/stylefeng/roses/kernel/mongodb/file/service/impl/MongoFileServiceImpl.java +++ b/kernel-d-mongodb/mongodb-sdk-file/src/main/java/cn/stylefeng/roses/kernel/mongodb/file/service/impl/MongoFileServiceImpl.java @@ -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 { - @Autowired - private MongoFileMapper mongoFileMapper; - @Autowired - private GridFsTemplate gridFsTemplate; - @Autowired - private GridFSBucket gridFSBucket; +public class MongoFileServiceImpl implements MongoFileService, MongoFileApi { + @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 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 getFileById(String id) { Optional 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 example = Example.of(fileDocument, ExampleMatcher.matching() .withIgnoreCase(true) - .withIgnorePaths("_class","pageSize","pageNo","content") + .withIgnorePaths("_class", "pageSize", "pageNo", "content") ); Page all = mongoFileMapper.findAll(example, pageRequest); return PageResultFactory.createPageResult(all.getContent(), mongoFileMapper.count(example), pageSize, pageIndex); diff --git a/kernel-d-mongodb/mongodb-sdk-springboot/src/main/java/cn/stylefeng/roses/kernel/mongodb/entity/GunsMapEntity.java b/kernel-d-mongodb/mongodb-sdk-springboot/src/main/java/cn/stylefeng/roses/kernel/mongodb/entity/GunsMapEntity.java index 14fb085be..7252c4149 100644 --- a/kernel-d-mongodb/mongodb-sdk-springboot/src/main/java/cn/stylefeng/roses/kernel/mongodb/entity/GunsMapEntity.java +++ b/kernel-d-mongodb/mongodb-sdk-springboot/src/main/java/cn/stylefeng/roses/kernel/mongodb/entity/GunsMapEntity.java @@ -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 data = new HashMap<>(); - + private Map data = new HashMap<>(); } diff --git a/kernel-d-mongodb/mongodb-sdk-springboot/src/main/java/cn/stylefeng/roses/kernel/mongodb/service/impl/GunsMapServiceImpl.java b/kernel-d-mongodb/mongodb-sdk-springboot/src/main/java/cn/stylefeng/roses/kernel/mongodb/service/impl/GunsMapServiceImpl.java index 7dd511baa..d59c6e3cd 100644 --- a/kernel-d-mongodb/mongodb-sdk-springboot/src/main/java/cn/stylefeng/roses/kernel/mongodb/service/impl/GunsMapServiceImpl.java +++ b/kernel-d-mongodb/mongodb-sdk-springboot/src/main/java/cn/stylefeng/roses/kernel/mongodb/service/impl/GunsMapServiceImpl.java @@ -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 { - +public class GunsMapServiceImpl implements GunsMapService, MongodbApi { @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 findById(String id){ + public Optional findById(String id) { return gunsMapRepository.findById(id); } @Override - public List findAll(){ + public List findAll() { return gunsMapRepository.findAll(); } diff --git a/kernel-d-mongodb/mongodb-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/mongodb/starter/GunsMongodbAutoConfiguration.java b/kernel-d-mongodb/mongodb-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/mongodb/starter/GunsMongodbAutoConfiguration.java index c83d5ee52..b17e5f769 100644 --- a/kernel-d-mongodb/mongodb-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/mongodb/starter/GunsMongodbAutoConfiguration.java +++ b/kernel-d-mongodb/mongodb-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/mongodb/starter/GunsMongodbAutoConfiguration.java @@ -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 mongodbApi() { return new GunsMapServiceImpl(); } @@ -59,10 +60,9 @@ public class GunsMongodbAutoConfiguration { * @date 2021/03/20 16:24 */ @Bean - public MongoFileApi mongoFileApi() { + public MongoFileApi mongoFileApi() { return new MongoFileServiceImpl(); } - }