#1.回顧
SpringBoot整合MongoDB數據庫方式(一)
上一篇文章寫的接口繼承的接口,而繼承的接口也繼承着上級接口,一級一級繼承上去,但最終由SpringBoot的容器框架幫助我們實現接口的具體實現,框架是如何實現的我們這裏暫時不做說明.有興趣的可以自己去研究一下.
即接口之間的繼承–>注入–>使用
#2.MongoTemplate.class
#2.1體系結構
#2.2實現接口
#.2.3注入對象
#2.4相關方法
save相關方法
insert相關方法
update相關方法
get相關方法:
upsert相關方法:
remove相關方法:
就不一一贅述了,裏面這個MongoTemplate類裏面3000多行代碼,幾十上百個方法,幾乎涵蓋了操作MongoDB數據庫的所有方式.就目前來說不會出現你想用而他沒有的方法,更多的情況是這個方法已經存在,而你並不知道他的存在.
#2.5當然,這也是框架給我們提供好的,那麼如何擴展自己特定的方法呢.
#3.擴展
#3.1編寫接口
#3.2自己實現自己的接口(達到定製化)
#3.3 複雜實現
上面這個接口開起來很簡單,當然我們還有複雜一點的實現方式,比如下面的具體實現:
/**
* 複製數據
*
* @param tenantId 企業id
* @param dataId 數據id
* @param formId 表單id
* @param copySource 數據複製源
* @return 複製的結果
* @author ???
*/
@Override
public BaseVO<TableDataVO> copyData(Long tenantId, Long dataId, Long formId, String copySource) {
List<CopySource> copySources = new ArrayList<>();
JSONArray jsonArray = JSON.parseArray(copySource);
for (Object o : jsonArray) {
JSONObject jsonObject = (JSONObject) o;
CopySource source = JSON.parseObject(JSON.toJSONString(jsonObject), CopySource.class);
source.setRowId(jsonObject.getLong("row_id"));
List<TableRow> tableRowlist = new ArrayList<>();
JSONArray rowsJson = jsonObject.getJSONArray("table_rows");
for (Object obj : rowsJson) {
JSONObject json = (JSONObject) obj;
TableRow tableRow = new TableRow();
tableRow.setValue(jsonTransfer.transData(json.getJSONObject(Const.VALUE)));
tableRowlist.add(tableRow);
}
source.setTableRows(tableRowlist);
copySources.add(source);
}
List<Long> copyIds = new ArrayList<>();
List<TableRow> copyResultRows = new ArrayList<>();
for (CopySource source : copySources) {
List<TableRow> tableRows = source.getTableRows();
for (TableRow tableRow : tableRows) {
tableRow.setId(snowflakeClient.uniqueId());
tableRow.setCopySourceId(source.getRowId());
tableRow.setStatus(DataState.ENABLE);
}
copyIds.add(source.getRowId());
copyResultRows.addAll(source.getTableRows());
}
Map.Entry<String, DataBase> sourceDataEntry = null;
//查詢數據庫中需要被拆分的數據
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(dataId).and("form_id").is(formId).and("tenant_id").is(tenantId));
FormData formData = mongoTemplate.findOne(query, FormData.class);
for (Map.Entry<String, DataBase> entry : formData.getData().entrySet()) {
if (Const.LIST.equals(entry.getValue().getType())) {
TableData tableData = (TableData) entry.getValue();
List<Long> allRows = new ArrayList<>();
for (TableRow tableRow : tableData.getValue()) {
allRows.add(tableRow.getId());
}
if (allRows.containsAll(copyIds)) {
sourceDataEntry = entry;
}
}
}
if (sourceDataEntry == null) {
return null;
}
if (!((TableVO) metadataClient.getField(tenantId, formId, sourceDataEntry.getKey())).getCopy()) {
return null;
}
TableData sourceTable = (TableData) sourceDataEntry.getValue();
for (TableRow tableRow : sourceTable.getValue()) {
if (copyIds.contains(tableRow.getId())) {
tableRow.setStatus(DataState.HIDDEN);
}
}
sourceTable.getValue().addAll(copyResultRows);
formData.getData().put(sourceDataEntry.getKey(), sourceTable);
formData.setSelectedRows(new ArrayList<>());
mongoTemplate.save(formData);
Map<String, DataBaseVO> dataVO = jsonTransfer.transDataVO(tenantId, formData.getCreator(), formId,
JSON.parseObject(JSON.toJSONString(formData.getData())), formData, null);
return new BaseVO<>(BaseVO.SUCCESS_CODE, "操作成功", (TableDataVO) dataVO.get(sourceDataEntry.getKey()));
}
而他的接口如下;
這和你各自具體的業務相關,mongoDB能幹啥,無非就是數據的操作,你把數據拿出來按照一定業務邏輯過濾一遍,然後再存回數據庫去,這是一種,業務邏輯各種各樣,而基礎操作就那麼一些
#4其他我想告訴你的
比如下面這個接口:
具體實現;
mongoDB中的高級查詢
@Override
public List<FormData> getHistoryVersionInfo(Long tenantId, Long originId, Long grantId, Long formId) {
//創建查詢對象
Query query = new Query();
// 添加查詢條件
query.addCriteria(Criteria.where("tenant_id").is(tenantId).and("origin_id").is(originId).and("form_id").is(formId).and("status").ne("DELETE"));
// 將查詢對象,參數傳遞進去,利用mongoTemplate的find()方法拿到你想要的數據,
return mongoTemplate.find(query, FormData.class, Const.COLLECTION_FORM_DATA);
}
這些需要你對MongoDB數據庫有一定的瞭解,比如ne關鍵字啊,find()findOne等等起數據庫自身的相關內容
#5.兩種方式總結
#1. 寫自己的接口去繼承別人的接口(MongoRepository) 由框架幫我們實現,當然也可以在自己的接口寫擴展.
#2. 利用框架已經寫好的具體實現類(MongoTemplate),通過注入該類對象,調用該類幾乎涵蓋了所有操作內容的方法
方法有很多,看你自己喜歡,個人推薦第二種 #2,即注入MongoTemplate,調用其豐富的方法,實現SpringBoot對MongoDB的操作…