|
@@ -0,0 +1,142 @@
|
|
|
+package com.elab.mongodb;
|
|
|
+
|
|
|
+import com.elab.core.componts.chains.ComponentConsumerHandlerChain;
|
|
|
+import com.elab.core.componts.chains.ComponentSupplierHandlerChain;
|
|
|
+import com.elab.mongodb.chain.MongoInterceptInvoke;
|
|
|
+import com.elab.mongodb.chain.model.MongoExecutorRequest;
|
|
|
+import com.elab.mongodb.chain.model.MongoInsertRequest;
|
|
|
+import com.elab.mongodb.chain.model.MongoQueryRequest;
|
|
|
+import com.elab.mongodb.chain.model.MongoUpdateRequest;
|
|
|
+import com.mongodb.DBObject;
|
|
|
+import com.mongodb.Mongo;
|
|
|
+import com.mongodb.WriteResult;
|
|
|
+import org.springframework.data.authentication.UserCredentials;
|
|
|
+import org.springframework.data.mongodb.MongoDbFactory;
|
|
|
+import org.springframework.data.mongodb.core.CollectionCallback;
|
|
|
+import org.springframework.data.mongodb.core.DocumentCallbackHandler;
|
|
|
+import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
+import org.springframework.data.mongodb.core.convert.MongoConverter;
|
|
|
+import org.springframework.data.mongodb.core.convert.MongoWriter;
|
|
|
+import org.springframework.data.mongodb.core.query.Query;
|
|
|
+import org.springframework.data.mongodb.core.query.Update;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.function.Supplier;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 简单的mongodb操作类
|
|
|
+ *
|
|
|
+ * @author liukaixiong
|
|
|
+ * @date 2022/8/13 - 11:18
|
|
|
+ */
|
|
|
+public class SimpleMongodbTemplate extends MongoTemplate {
|
|
|
+
|
|
|
+ private List<MongoInterceptInvoke> mongoInterceptInvokeList = new ArrayList<>();
|
|
|
+
|
|
|
+ public SimpleMongodbTemplate(Mongo mongo, String databaseName) {
|
|
|
+ super(mongo, databaseName);
|
|
|
+ }
|
|
|
+
|
|
|
+ public SimpleMongodbTemplate(Mongo mongo, String databaseName, UserCredentials userCredentials) {
|
|
|
+ super(mongo, databaseName, userCredentials);
|
|
|
+ }
|
|
|
+
|
|
|
+ public SimpleMongodbTemplate(MongoDbFactory mongoDbFactory) {
|
|
|
+ super(mongoDbFactory);
|
|
|
+ }
|
|
|
+
|
|
|
+ public SimpleMongodbTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter) {
|
|
|
+ super(mongoDbFactory, mongoConverter);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setMongoInterceptInvokeList(List<MongoInterceptInvoke> mongoInterceptInvokeList) {
|
|
|
+ this.mongoInterceptInvokeList = mongoInterceptInvokeList;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public <T> T execute(String collectionName, CollectionCallback<T> callback) {
|
|
|
+ MongoExecutorRequest req = new MongoExecutorRequest(collectionName, callback);
|
|
|
+ ComponentSupplierHandlerChain<MongoInterceptInvoke, MongoExecutorRequest, T> handlerChain =
|
|
|
+ new ComponentSupplierHandlerChain(this.mongoInterceptInvokeList,
|
|
|
+ () -> super.execute(collectionName, callback));
|
|
|
+ return handlerChain.executor(req, MongoInterceptInvoke::execute);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T> writer) {
|
|
|
+
|
|
|
+ MongoInsertRequest request = new MongoInsertRequest(collectionName, objectToSave);
|
|
|
+ ComponentConsumerHandlerChain<MongoInterceptInvoke, MongoInsertRequest> chain =
|
|
|
+ new ComponentConsumerHandlerChain<>(this.mongoInterceptInvokeList,
|
|
|
+ (r) -> super.doSave(collectionName, objectToSave, writer));
|
|
|
+ chain.run(request, MongoInterceptInvoke::insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected <T> void doInsert(String collectionName, T objectToSave, MongoWriter<T> writer) {
|
|
|
+ MongoInsertRequest request = new MongoInsertRequest(collectionName, objectToSave);
|
|
|
+ ComponentConsumerHandlerChain<MongoInterceptInvoke, MongoInsertRequest> chain =
|
|
|
+ new ComponentConsumerHandlerChain<>(this.mongoInterceptInvokeList,
|
|
|
+ (r) -> super.doInsert(collectionName, objectToSave, writer));
|
|
|
+ chain.run(request, MongoInterceptInvoke::insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected WriteResult doUpdate(String collectionName, Query query, Update update, Class<?> entityClass,
|
|
|
+ boolean upsert, boolean multi) {
|
|
|
+ MongoUpdateRequest request = new MongoUpdateRequest(collectionName, query, update);
|
|
|
+ ComponentSupplierHandlerChain<MongoInterceptInvoke, MongoUpdateRequest, WriteResult> chain =
|
|
|
+ new ComponentSupplierHandlerChain<>(this.mongoInterceptInvokeList,
|
|
|
+ () -> super.doUpdate(collectionName, query, update, entityClass, upsert, multi));
|
|
|
+ return chain.executor(request, MongoInterceptInvoke::update);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public <T> List<T> find(Query query, Class<T> entityClass, String collectionName) {
|
|
|
+ return findListExecutor(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass,
|
|
|
+ () -> super.find(query, entityClass, collectionName));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected <T> List<T> doFind(String collectionName, DBObject query, DBObject fields, Class<T> entityClass) {
|
|
|
+ return findListExecutor(collectionName, query, fields, entityClass,
|
|
|
+ () -> super.doFind(collectionName, query, fields, entityClass));
|
|
|
+ }
|
|
|
+
|
|
|
+ private <T> List<T> findListExecutor(String collectionName, DBObject query, DBObject fields, Class<T> entityClass,
|
|
|
+ Supplier<List<T>> supplier) {
|
|
|
+ MongoQueryRequest request = new MongoQueryRequest();
|
|
|
+ request.setCollectionName(collectionName);
|
|
|
+ request.setQuery(query);
|
|
|
+ request.setFields(fields);
|
|
|
+ request.setClazz(entityClass);
|
|
|
+ ComponentSupplierHandlerChain<MongoInterceptInvoke, MongoQueryRequest, List<T>> chain =
|
|
|
+ new ComponentSupplierHandlerChain<>(this.mongoInterceptInvokeList, supplier);
|
|
|
+ return chain.executor(request, MongoInterceptInvoke::queryList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public <T> List<T> findAll(Class<T> entityClass, String collectionName) {
|
|
|
+ return super.findAll(entityClass, collectionName);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields, Class<T> entityClass) {
|
|
|
+ MongoQueryRequest request = new MongoQueryRequest();
|
|
|
+ request.setCollectionName(collectionName);
|
|
|
+ request.setQuery(query);
|
|
|
+ request.setFields(fields);
|
|
|
+ request.setClazz(entityClass);
|
|
|
+ ComponentSupplierHandlerChain<MongoInterceptInvoke, MongoQueryRequest, T> chain =
|
|
|
+ new ComponentSupplierHandlerChain<>(this.mongoInterceptInvokeList,
|
|
|
+ () -> super.doFindOne(collectionName, query, fields, entityClass));
|
|
|
+ return chain.executor(request, MongoInterceptInvoke::queryOne);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void executeQuery(Query query, String collectionName, DocumentCallbackHandler dch) {
|
|
|
+ super.executeQuery(query, collectionName, dch);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|