当前位置:   article > 正文

SpringBoot 集成MongoDB实现高并发存储_spring boot mongodb封装 helper

spring boot mongodb封装 helper

1.导入包

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-mongodb</artifactId>
  4. </dependency>

2.封装DBHelper类

  1. package com.gnss.logserver.utils;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.data.mongodb.core.MongoTemplate;
  4. import org.springframework.data.mongodb.core.query.Query;
  5. import org.springframework.stereotype.Component;
  6. import java.util.Collection;
  7. import java.util.List;
  8. /**
  9. * MongoDB 操作类
  10. * @author Mr.
  11. * @date 20211129
  12. */
  13. @Component
  14. public class MongoDbHelper {
  15. @Autowired
  16. private MongoTemplate mongoTemplate;
  17. /**
  18. * 保存对象
  19. *
  20. * @param t
  21. * @param <T>
  22. * @return
  23. */
  24. public <T> T save(T t) {
  25. return mongoTemplate.insert(t);
  26. }
  27. /**
  28. * 保存对象到指定的collection
  29. *
  30. * @param list
  31. * @param collectionName
  32. * @return
  33. */
  34. public Collection<T> save(Collection<T> list, String collectionName) {
  35. return mongoTemplate.insert(list,collectionName);
  36. }
  37. /**
  38. * 查询数据
  39. *
  40. * @param query
  41. * @param tClass
  42. * @param <T>
  43. * @return
  44. */
  45. public <T> List<T> find(Query query, Class<T> tClass) {
  46. return mongoTemplate.find(query, tClass);
  47. }
  48. /**
  49. * 查询数据指定的collection
  50. *
  51. * @param query
  52. * @param tClass
  53. * @param <T>
  54. * @return
  55. */
  56. public <T> List<T> find(Query query, Class<T> tClass,String collectionName) {
  57. return mongoTemplate.find(query, tClass,collectionName);
  58. }
  59. /**
  60. * 查询所有
  61. *
  62. * @param tClass
  63. * @param <T>
  64. * @return
  65. */
  66. public <T> List<T> findAll(Class<T> tClass) {
  67. return mongoTemplate.findAll(tClass);
  68. }
  69. /**
  70. * 查询所有指定的collection
  71. *
  72. * @param tClass
  73. * @param collectionName
  74. * @param <T>
  75. * @return
  76. */
  77. public <T> List<T> findAll(Class<T> tClass,String collectionName) {
  78. return mongoTemplate.findAll(tClass,collectionName);
  79. }
  80. }

3.MongoTemplate 源码(批量存储)

MongoTemplate里面封装了MongoDB操作方法,我们可以根据自己的需要对方法进行封装,比如:

我们存储一个对象到MongoDB的时候可以采用save()方法

public <T> T save(T objectToSave)方法是将一个实体对象存储到其对应的Collection里面

public <T> T save(T objectToSave, String collectionName)方法是将一个实体对象存储到指定的Collection里面

  1. @Override
  2. public <T> T save(T objectToSave) {
  3. Assert.notNull(objectToSave, "Object to save must not be null!");
  4. return save(objectToSave, getCollectionName(ClassUtils.getUserClass(objectToSave)));
  5. }
  6. @Override
  7. @SuppressWarnings("unchecked")
  8. public <T> T save(T objectToSave, String collectionName) {
  9. Assert.notNull(objectToSave, "Object to save must not be null!");
  10. Assert.hasText(collectionName, "Collection name must not be null or empty!");
  11. AdaptibleEntity<T> source = operations.forEntity(objectToSave, mongoConverter.getConversionService());
  12. return source.isVersionedEntity() //
  13. ? doSaveVersioned(source, collectionName) //
  14. : (T) doSave(collectionName, objectToSave, this.mongoConverter);
  15. }

如果需要进行批量存储提示性能,则可以采用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

  1. @Override
  2. @SuppressWarnings("unchecked")
  3. public <T> Collection<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass) {
  4. Assert.notNull(batchToSave, "BatchToSave must not be null!");
  5. return (Collection<T>) doInsertBatch(getCollectionName(entityClass), batchToSave, this.mongoConverter);
  6. }
  7. @Override
  8. @SuppressWarnings("unchecked")
  9. public <T> Collection<T> insert(Collection<? extends T> batchToSave, String collectionName) {
  10. Assert.notNull(batchToSave, "BatchToSave must not be null!");
  11. Assert.notNull(collectionName, "CollectionName must not be null!");
  12. return (Collection<T>) doInsertBatch(collectionName, batchToSave, this.mongoConverter);
  13. }

可以根据自己的需要对MongoTemplate里的方法进行封装,我这里不做过多的说明.

4.并发效果截图

 目前测试结果,单机可达9000/s

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/201265
推荐阅读
相关标签
  

闽ICP备14008679号