-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
郑家骜[ào]
committed
Sep 27, 2024
1 parent
6c7c88a
commit 58060b5
Showing
6 changed files
with
361 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...ta/starter-data-mongodb/src/main/java/com/zja/controller/NonStructuredJsonController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.zja.controller; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
import com.zja.model.BasePageRequest; | ||
import com.zja.model.PageData; | ||
import com.zja.service.NonStructuredJsonService; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.validation.Valid; | ||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.NotEmpty; | ||
|
||
/** | ||
* 非结构化数据 接口层(一般与页面、功能对应) | ||
* | ||
* @author: zhengja | ||
* @since: 2024/09/26 9:15 | ||
*/ | ||
@Validated | ||
@RestController | ||
@RequestMapping("/rest/non/structured/json") | ||
@Api(tags = {"BASE-非结构化JSON数据管理页面"}) | ||
public class NonStructuredJsonController { | ||
|
||
@Autowired | ||
NonStructuredJsonService service; | ||
|
||
@GetMapping("/query/{id}") | ||
@ApiOperation("查询单个非结构化数据详情") | ||
public JSONObject queryById(@NotBlank @PathVariable("id") String id) { | ||
return service.queryById(id); | ||
} | ||
|
||
@GetMapping("/page/list") | ||
@ApiOperation("分页查询非结构化数据列表") | ||
public PageData<JSONObject> pageList(@Valid BasePageRequest pageRequest) { | ||
return service.pageList(pageRequest); | ||
} | ||
|
||
@PostMapping("/add") | ||
@ApiOperation("添加非结构化数据") | ||
public String add(@Valid @NotEmpty @RequestBody JSONObject jsonData) { | ||
return service.add(jsonData); | ||
} | ||
|
||
@PutMapping("/update/{id}") | ||
@ApiOperation("更新非结构化数据") | ||
public JSONObject update(@NotBlank @PathVariable("id") String id, | ||
@Valid @NotEmpty @RequestBody JSONObject jsonData) { | ||
return service.update(id, jsonData); | ||
} | ||
|
||
@GetMapping("/exists/{id}") | ||
@ApiOperation("存在非结构化数据") | ||
public boolean exists(@NotBlank @PathVariable("id") String id) { | ||
return service.exists(id); | ||
} | ||
|
||
@DeleteMapping("/delete/{id}") | ||
@ApiOperation("删除非结构化数据") | ||
public boolean deleteById(@NotBlank @PathVariable("id") String id) { | ||
return service.deleteById(id); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
starter-data/starter-data-mongodb/src/main/java/com/zja/model/BasePageRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.zja.model; | ||
|
||
import io.swagger.annotations.ApiModel; | ||
import io.swagger.annotations.ApiModelProperty; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* 分页请求(通用的) | ||
* | ||
* @author: zhengja | ||
* @since: 2024/09/27 9:05 | ||
*/ | ||
@ApiModel("BasePageRequest") | ||
public class BasePageRequest implements Serializable { | ||
@ApiModelProperty("页码 从第1页开始") | ||
private Integer page = 1; | ||
@ApiModelProperty("每页数量 默认 10") | ||
private Integer size = 10; | ||
|
||
public Integer getPage() { | ||
return page - 1; // 注:jpa page 从 0 开始 | ||
} | ||
|
||
public void setPage(Integer page) { | ||
this.page = page; | ||
} | ||
|
||
public Integer getSize() { | ||
return size; | ||
} | ||
|
||
public void setSize(Integer size) { | ||
this.size = size; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
starter-data/starter-data-mongodb/src/main/java/com/zja/model/PageData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.zja.model; | ||
|
||
import io.swagger.annotations.ApiModel; | ||
import io.swagger.annotations.ApiModelProperty; | ||
import lombok.Data; | ||
import org.springframework.data.domain.Page; | ||
|
||
import java.io.Serializable; | ||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* a 数据传输 | ||
* | ||
* @author: zhengja | ||
* @since: 2024/09/27 9:05 | ||
*/ | ||
@Data | ||
@ApiModel("PageData") | ||
public final class PageData<T> implements Serializable { | ||
|
||
@ApiModelProperty("数据") | ||
private List<T> data; | ||
@ApiModelProperty("当前页号") | ||
private int index; | ||
@ApiModelProperty("页大小") | ||
private int size; | ||
@ApiModelProperty("查询到的数据量") | ||
private int length; | ||
@ApiModelProperty("总数据量") | ||
private long count; | ||
@ApiModelProperty("总页数") | ||
private int pages; | ||
@ApiModelProperty("是否第一页") | ||
private boolean first; | ||
@ApiModelProperty("是否最后一页") | ||
private boolean last; | ||
@ApiModelProperty("是否有下一页") | ||
private boolean hasNext; | ||
@ApiModelProperty("是否有上一页") | ||
private boolean hasPrev; | ||
|
||
public static <T> PageData<T> of(Page<T> page) { | ||
return of(page.getContent(), page.getNumber(), page.getSize(), page.getTotalElements()); | ||
} | ||
|
||
public static <T> PageData<T> of(List<T> data, int pageIndex, int pageSize, long totalSize) { | ||
PageData<T> dp = new PageData<>(); | ||
dp.data = data; | ||
dp.index = pageIndex; | ||
dp.size = pageSize; | ||
dp.length = data.size(); | ||
dp.count = totalSize; | ||
dp.pages = BigDecimal.valueOf(totalSize).divide(BigDecimal.valueOf(pageSize), RoundingMode.UP).intValue(); | ||
dp.first = pageIndex == 0; | ||
dp.hasPrev = !dp.first; | ||
dp.hasNext = (dp.pages - dp.index) > 1; | ||
dp.last = !dp.hasNext; | ||
return dp; | ||
} | ||
|
||
public void convert(Function<T, T> mapper) { | ||
this.data = data.stream().map(mapper).collect(Collectors.toList()); | ||
} | ||
|
||
public <R extends Serializable> PageData<R> map(Function<List<T>, List<R>> mapper) { | ||
return of(mapper.apply(this.getData()), this.getIndex(), this.getSize(), this.getCount()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ter-data/starter-data-mongodb/src/main/java/com/zja/service/NonStructuredJsonService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.zja.service; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
import com.zja.model.BasePageRequest; | ||
import com.zja.model.PageData; | ||
|
||
/** | ||
* mongo非结构化json数据管理 | ||
* | ||
* @Author: zhengja | ||
* @Date: 2024-09-25 15:39 | ||
*/ | ||
public interface NonStructuredJsonService { | ||
|
||
String add(JSONObject jsonData); | ||
|
||
JSONObject update(String id, JSONObject jsonData); | ||
|
||
JSONObject queryById(String id); | ||
|
||
PageData<JSONObject> pageList(BasePageRequest pageRequest); | ||
|
||
boolean exists(String id); | ||
|
||
boolean deleteById(String id); | ||
} |
149 changes: 149 additions & 0 deletions
149
...data/starter-data-mongodb/src/main/java/com/zja/service/NonStructuredJsonServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package com.zja.service; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
import com.mongodb.MongoException; | ||
import com.mongodb.client.result.DeleteResult; | ||
import com.zja.model.BasePageRequest; | ||
import com.zja.model.PageData; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.query.Criteria; | ||
import org.springframework.data.mongodb.core.query.Query; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* mongo非结构化json数据管理实现类 | ||
* | ||
* @Author: zhengja | ||
* @Date: 2024-09-25 15:44 | ||
*/ | ||
@Slf4j | ||
@Component | ||
public class NonStructuredJsonServiceImpl implements NonStructuredJsonService { | ||
|
||
@Autowired | ||
private MongoTemplate mongoTemplate; | ||
|
||
// 集合名称 | ||
private static final String COLLECTION_NAME = "non_structured_json"; | ||
|
||
private void checkJsonData(JSONObject jsonData) { | ||
if (jsonData == null) { | ||
throw new IllegalArgumentException("jsonData cannot be null"); | ||
} | ||
} | ||
|
||
@Override | ||
public String add(JSONObject jsonData) { | ||
checkJsonData(jsonData); | ||
|
||
// 添加数据 | ||
JSONObject addData = new JSONObject(); | ||
addData.put("data", jsonData); | ||
addData.put("createdAt", LocalDateTime.now()); | ||
addData.put("sort", System.currentTimeMillis()); | ||
// 保存添加的数据 | ||
JSONObject entity = mongoTemplate.save(addData, COLLECTION_NAME); | ||
|
||
return entity.getString("_id"); | ||
} | ||
|
||
@Override | ||
public JSONObject update(String id, JSONObject jsonData) { | ||
checkJsonData(jsonData); | ||
|
||
if (!exists(id)) { | ||
throw new RuntimeException("不存在的记录,更新失败."); | ||
} | ||
|
||
// 查询原有数据 | ||
Query query = new Query(Criteria.where("_id").is(id)); | ||
JSONObject updatedData = mongoTemplate.findOne(query, JSONObject.class, COLLECTION_NAME); | ||
if (updatedData == null) { | ||
throw new RuntimeException("找不到对应的记录,更新失败."); | ||
} | ||
|
||
// 更新数据 | ||
updatedData.put("data", jsonData); | ||
updatedData.put("updatedAt", LocalDateTime.now()); | ||
// 保存更新后的数据 | ||
JSONObject entity = mongoTemplate.save(updatedData, COLLECTION_NAME); | ||
|
||
return entity.getJSONObject("data"); | ||
} | ||
|
||
@Override | ||
public JSONObject queryById(String id) { | ||
Query query = new Query(Criteria.where("_id").is(id)); | ||
List<JSONObject> jsonDocuments = mongoTemplate.find(query, JSONObject.class, COLLECTION_NAME); | ||
if (!jsonDocuments.isEmpty()) { | ||
return jsonDocuments.get(0).getJSONObject("data"); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public PageData<JSONObject> pageList(BasePageRequest pageRequest) { | ||
Pageable pageable = PageRequest.of(pageRequest.getPage(), pageRequest.getSize(), Sort.by(Sort.Direction.DESC, "sort")); | ||
Query query = new Query().with(pageable); | ||
|
||
List<JSONObject> result = mongoTemplate.find(query, JSONObject.class, COLLECTION_NAME); | ||
long totalSize = mongoTemplate.count(new Query(), COLLECTION_NAME); | ||
|
||
List<JSONObject> resultData = result.stream() | ||
.map(this::convertId) | ||
.collect(Collectors.toList()); | ||
|
||
return PageData.of(resultData, pageRequest.getPage(), pageRequest.getSize(), totalSize); | ||
} | ||
|
||
private JSONObject convertId(JSONObject jsonObject) { | ||
JSONObject convertedJson = new JSONObject(); | ||
convertedJson.putAll(jsonObject); | ||
String id = convertedJson.getString("_id"); | ||
convertedJson.put("id", id); | ||
convertedJson.remove("_id"); | ||
return convertedJson; | ||
} | ||
|
||
@Override | ||
public boolean exists(String id) { | ||
Query query = new Query(Criteria.where("_id").is(id)); | ||
return mongoTemplate.exists(query, JSONObject.class, COLLECTION_NAME); | ||
} | ||
|
||
@Override | ||
public boolean deleteById(String id) { | ||
if (!exists(id)) { | ||
return true; | ||
} | ||
|
||
try { | ||
Query query = new Query(Criteria.where("_id").is(id)); | ||
DeleteResult deleteResult = mongoTemplate.remove(query, COLLECTION_NAME); | ||
|
||
if (deleteResult.wasAcknowledged() && deleteResult.getDeletedCount() > 0) { | ||
log.debug("删除成功,id={}", id); | ||
return true; | ||
} else { | ||
log.warn("删除失败,可能是由于数据不存在或数据库问题,id={}", id); | ||
return false; | ||
} | ||
} catch (MongoException me) { | ||
log.error("MongoDB异常:删除失败,请排查id={},异常信息:{}", id, me.getMessage(), me); | ||
return false; | ||
} catch (Exception e) { | ||
log.error("未知异常:删除失败,请排查id={},异常信息:{}", id, e.getMessage(), e); | ||
return false; | ||
} | ||
} | ||
} |