org.springframework.data.mongodb.core.MongoTemplate类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(232)

本文整理了Java中org.springframework.data.mongodb.core.MongoTemplate类的一些代码示例,展示了MongoTemplate类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MongoTemplate类的具体详情如下:
包路径:org.springframework.data.mongodb.core.MongoTemplate
类名称:MongoTemplate

MongoTemplate介绍

[英]Primary implementation of MongoOperations.
[中]MongoOperations的主要实现。

代码示例

代码示例来源:origin: spring-projects/spring-data-mongodb

/**
 * Creates a new {@link DefaultIndexOperations}.
 *
 * @param mongoDbFactory must not be {@literal null}.
 * @param collectionName must not be {@literal null}.
 * @param queryMapper must not be {@literal null}.
 * @param type Type used for mapping potential partial index filter expression. Can be {@literal null}.
 * @since 1.10
 * @deprecated since 2.1. Please use
 *             {@link DefaultIndexOperations#DefaultIndexOperations(MongoOperations, String, Class)}.
 */
@Deprecated
public DefaultIndexOperations(MongoDbFactory mongoDbFactory, String collectionName, QueryMapper queryMapper,
    @Nullable Class<?> type) {
  Assert.notNull(mongoDbFactory, "MongoDbFactory must not be null!");
  Assert.notNull(collectionName, "Collection name can not be null!");
  Assert.notNull(queryMapper, "QueryMapper must not be null!");
  this.collectionName = collectionName;
  this.mapper = queryMapper;
  this.type = type;
  this.mongoOperations = new MongoTemplate(mongoDbFactory);
}

代码示例来源:origin: roncoo/spring-boot-demo

public void deleteById(int id) {
  Criteria criteria = Criteria.where("id").in(id);
  Query query = new Query(criteria);
  mongoTemplate.remove(query, RoncooUser.class);
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Nullable
@Override
public <T> T findOne(Query query, Class<T> entityClass, String collectionName) {
  Assert.notNull(query, "Query must not be null!");
  Assert.notNull(entityClass, "EntityClass must not be null!");
  Assert.notNull(collectionName, "CollectionName must not be null!");
  if (ObjectUtils.isEmpty(query.getSortObject()) && !query.getCollation().isPresent()) {
    return doFindOne(collectionName, query.getQueryObject(), query.getFieldsObject(), entityClass);
  } else {
    query.limit(1);
    List<T> results = find(query, entityClass, collectionName);
    return results.isEmpty() ? null : results.get(0);
  }
}

代码示例来源:origin: spring-projects/spring-integration-samples

public static MongoDbFactory prepareMongoFactory(String... additionalCollectionToDrop) throws Exception{
    MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(new MongoClient(), "test");
    MongoTemplate template = new MongoTemplate(mongoDbFactory);
    template.dropCollection("messages");
    template.dropCollection("data");
    for (String additionalCollection : additionalCollectionToDrop) {
      template.dropCollection(additionalCollection);
    }
    return mongoDbFactory;
  }
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
public <T> T insert(T objectToSave) {
  Assert.notNull(objectToSave, "ObjectToSave must not be null!");
  ensureNotIterable(objectToSave);
  return insert(objectToSave, operations.determineEntityCollectionName(objectToSave));
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
public Collection<T> all(Collection<? extends T> objects) {
  Assert.notNull(objects, "Objects must not be null!");
  return template.insert(objects, getCollectionName());
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
public <T> T save(T objectToSave) {
  Assert.notNull(objectToSave, "Object to save must not be null!");
  return save(objectToSave, operations.determineEntityCollectionName(objectToSave));
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
public long count(Query query, Class<?> entityClass) {
  Assert.notNull(entityClass, "Entity class must not be null!");
  return count(query, entityClass, operations.determineCollectionName(entityClass));
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Override
public DeleteResult remove(Object object) {
  Assert.notNull(object, "Object must not be null!");
  Query query = operations.forEntity(object).getByIdQuery();
  return remove(query, object.getClass());
}

代码示例来源:origin: spring-projects/spring-integration

@Override
@ManagedAttribute
public int getMessageCountForAllMessageGroups() {
  Query query = Query.query(Criteria.where(MessageDocumentFields.MESSAGE_ID).exists(true)
      .and(MessageDocumentFields.GROUP_ID).exists(true));
  long count = this.mongoTemplate.count(query, this.collectionName);
  Assert.isTrue(count <= Integer.MAX_VALUE, "Message count is out of Integer's range");
  return (int) count;
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public int messageGroupSize(Object groupId) {
  long lCount = this.template.count(new Query(Criteria.where(GROUP_ID_KEY).is(groupId)), this.collectionName);
  Assert.isTrue(lCount <= Integer.MAX_VALUE, "Message count is out of Integer's range");
  return (int) lCount;
}

代码示例来源:origin: yu199195/hmily

@Override
public int updateStatus(final String id, final Integer status) {
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(id));
  Update update = new Update();
  update.set("status", status);
  final UpdateResult updateResult = template.updateFirst(query, update, MongoAdapter.class, collectionName);
  if (updateResult.getModifiedCount() <= 0) {
    throw new HmilyRuntimeException("update data exception!");
  }
  return ROWS;
}

代码示例来源:origin: roncoo/spring-boot-demo

public RoncooUser selectById(int id) {
    Criteria criteria = Criteria.where("id").in(id);
    Query query = new Query(criteria);
    return mongoTemplate.findOne(query, RoncooUser.class);
  }
}

代码示例来源:origin: yu199195/hmily

final PageParameter pageParameter = query.getPageParameter();
final int pageSize = pageParameter.getPageSize();
Query baseQuery = new Query();
if (StringUtils.isNoneBlank(query.getTransId())) {
  baseQuery.addCriteria(new Criteria("transId").is(query.getTransId()));
  baseQuery.addCriteria(new Criteria("retriedCount").lt(query.getRetry()));
final long totalCount = mongoTemplate.count(baseQuery, mongoTableName);
if (totalCount <= 0) {
  return voCommonPager;
baseQuery.skip(start).limit(pageSize);
final List<MongoAdapter> mongoAdapters =
    mongoTemplate.find(baseQuery, MongoAdapter.class, mongoTableName);
if (CollectionUtils.isNotEmpty(mongoAdapters)) {
  final List<HmilyCompensationVO> recoverVOS =

代码示例来源:origin: yu199195/hmily

@Override
public List<HmilyTransaction> listAllByDelay(final Date date) {
  Query query = new Query();
  query.addCriteria(Criteria.where("lastTime").lt(date));
  final List<MongoAdapter> mongoBeans =
      template.find(query, MongoAdapter.class, collectionName);
  if (CollectionUtils.isNotEmpty(mongoBeans)) {
    return mongoBeans.stream().map(this::buildByCache).collect(Collectors.toList());
  }
  return Collections.emptyList();
}

代码示例来源:origin: ityouknow/spring-boot-examples

/**
 * 根据用户名查询对象
 * @param userName
 * @return
 */
@Override
public UserEntity findUserByUserName(String userName) {
  Query query=new Query(Criteria.where("userName").is(userName));
  UserEntity user =  mongoTemplate.findOne(query , UserEntity.class);
  return user;
}

代码示例来源:origin: ityouknow/spring-boot-examples

/**
   * 删除对象
   * @param id
   */
  @Override
  public void deleteUserById(Long id) {
    Query query=new Query(Criteria.where("id").is(id));
    mongoTemplate.remove(query,UserEntity.class);
  }
}

代码示例来源:origin: yu199195/Raincat

@Override
public List<TransactionRecover> listAllByDelay(final Date date) {
  Query query = new Query();
  query.addCriteria(new Criteria("status")
      .in(TransactionStatusEnum.BEGIN.getCode(),
          TransactionStatusEnum.FAILURE.getCode(),
          TransactionStatusEnum.ROLLBACK.getCode()))
      .addCriteria(Criteria.where("lastTime").lt(date));
  final List<MongoAdapter> mongoBeans =
      template.find(query, MongoAdapter.class, collectionName);
  if (CollectionUtils.isNotEmpty(mongoBeans)) {
    return mongoBeans.stream().map(this::buildByCache).collect(Collectors.toList());
  }
  return null;
}

代码示例来源:origin: yu199195/hmily

@Override
public int remove(final String id) {
  AssertUtils.notNull(id);
  Query query = new Query();
  query.addCriteria(new Criteria("transId").is(id));
  template.remove(query, collectionName);
  return ROWS;
}

代码示例来源:origin: spring-projects/spring-data-mongodb

@Nullable
@Override
public <T> T findOne(Query query, Class<T> entityClass) {
  return findOne(query, entityClass, operations.determineCollectionName(entityClass));
}

相关文章