Package core

Source Code of core.MongoPersonRepository

package core;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.SimpleMongoConverter;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.mongodb.Mongo;

@Repository
public class MongoPersonRepository implements PersonRepository {

  private final MongoTemplate mongoDbTemplate;

  @Autowired
  private MongoPersonRepository(Mongo mongo) {
    Assert.notNull(mongo, "Mongo is required");
    mongoDbTemplate = new MongoTemplate(mongo, "test", new SimpleMongoConverter());
  }

  public int count() {
    return (int) this.mongoDbTemplate.getCollection("people").count();
  }

  public List<Person> getAll() {
    return this.mongoDbTemplate.getCollection("people", Person.class);
  }

  public void add(Person person) {
    this.mongoDbTemplate.insert("people", person);
  }

}
TOP

Related Classes of core.MongoPersonRepository

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.