赞
踩
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-mongodb</artifactId>
- </dependency>
- package com.gnss.logserver.utils;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.mongodb.core.MongoTemplate;
- import org.springframework.data.mongodb.core.query.Query;
- import org.springframework.stereotype.Component;
-
- import java.util.Collection;
- import java.util.List;
-
- /**
- * MongoDB 操作类
- * @author Mr.
- * @date 20211129
- */
- @Component
- public class MongoDbHelper {
-
- @Autowired
- private MongoTemplate mongoTemplate;
-
- /**
- * 保存对象
- *
- * @param t
- * @param <T>
- * @return
- */
- public <T> T save(T t) {
- return mongoTemplate.insert(t);
- }
-
- /**
- * 保存对象到指定的collection
- *
- * @param list
- * @param collectionName
- * @return
- */
- public Collection<T> save(Collection<T> list, String collectionName) {
- return mongoTemplate.insert(list,collectionName);
- }
-
- /**
- * 查询数据
- *
- * @param query
- * @param tClass
- * @param <T>
- * @return
- */
- public <T> List<T> find(Query query, Class<T> tClass) {
- return mongoTemplate.find(query, tClass);
- }
-
- /**
- * 查询数据指定的collection
- *
- * @param query
- * @param tClass
- * @param <T>
- * @return
- */
- public <T> List<T> find(Query query, Class<T> tClass,String collectionName) {
- return mongoTemplate.find(query, tClass,collectionName);
- }
-
- /**
- * 查询所有
- *
- * @param tClass
- * @param <T>
- * @return
- */
- public <T> List<T> findAll(Class<T> tClass) {
- return mongoTemplate.findAll(tClass);
- }
-
- /**
- * 查询所有指定的collection
- *
- * @param tClass
- * @param collectionName
- * @param <T>
- * @return
- */
- public <T> List<T> findAll(Class<T> tClass,String collectionName) {
- return mongoTemplate.findAll(tClass,collectionName);
- }
- }

MongoTemplate里面封装了MongoDB操作方法,我们可以根据自己的需要对方法进行封装,比如:
我们存储一个对象到MongoDB的时候可以采用save()方法
public <T> T save(T objectToSave)方法是将一个实体对象存储到其对应的Collection里面
public <T> T save(T objectToSave, String collectionName)方法是将一个实体对象存储到指定的Collection里面
- @Override
- public <T> T save(T objectToSave) {
-
- Assert.notNull(objectToSave, "Object to save must not be null!");
- return save(objectToSave, getCollectionName(ClassUtils.getUserClass(objectToSave)));
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public <T> T save(T objectToSave, String collectionName) {
-
- Assert.notNull(objectToSave, "Object to save must not be null!");
- Assert.hasText(collectionName, "Collection name must not be null or empty!");
-
- AdaptibleEntity<T> source = operations.forEntity(objectToSave, mongoConverter.getConversionService());
-
- return source.isVersionedEntity() //
- ? doSaveVersioned(source, collectionName) //
- : (T) doSave(collectionName, objectToSave, this.mongoConverter);
-
- }

如果需要进行批量存储提示性能,则可以采用insert方法
public <T> Collection<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass)以实体类对应的Collection进行批量存储
public <T> Collection<T> insert(Collection<? extends T> batchToSave, String collectionName)批量存储到以collectionName命名的Collection
- @Override
- @SuppressWarnings("unchecked")
- public <T> Collection<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass) {
-
- Assert.notNull(batchToSave, "BatchToSave must not be null!");
-
- return (Collection<T>) doInsertBatch(getCollectionName(entityClass), batchToSave, this.mongoConverter);
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public <T> Collection<T> insert(Collection<? extends T> batchToSave, String collectionName) {
-
- Assert.notNull(batchToSave, "BatchToSave must not be null!");
- Assert.notNull(collectionName, "CollectionName must not be null!");
-
- return (Collection<T>) doInsertBatch(collectionName, batchToSave, this.mongoConverter);
- }

可以根据自己的需要对MongoTemplate里的方法进行封装,我这里不做过多的说明.
目前测试结果,单机可达9000/s
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。