Spring boot -mongodb

1.更接近原生mongodb
public class MongoApp { 
private static final Log log = LogFactory.getLog(MongoApp.class); 
public static void main(String[] args) throws Exception {
 MongoOperations mongoOps = new MongoTemplate(new MongoClient(), "database"); 
mongoOps.insert(new Person("Joe", 34)); 
log.info(mongoOps.findOne(new Query(where("name").is("Joe")), Person.class)); mongoOps.dropCollection("person"); 
} } 
2.@Configuration public class AppConfig { 
/* * Use the standard Mongo driver API to create a com.mongodb.MongoClient instance. */
public @Bean MongoClient mongoClient() {
return new MongoClient("localhost");
} } 3.@Configuration
public class AppConfig {
/* * Factory bean that creates the com.mongodb.MongoClient instance */
public @Bean MongoClientFactoryBean mongo() {
MongoClientFactoryBean mongo = new MongoClientFactoryBean();
mongo.setHost("localhost"); return mongo; } } 4. http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd

  

5.spring与mongodb的整合
<context:property-placeholder location="classpath:/com/myapp/mongodb/config/mongo.properties"/>

<mongo:mongo-client host="${mongo.host}" port="${mongo.port}">
  <mongo:client-options
     connections-per-host="${mongo.connectionsPerHost}"
     threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
     connect-timeout="${mongo.connectTimeout}"
     max-wait-time="${mongo.maxWaitTime}"
     auto-connect-retry="${mongo.autoConnectRetry}"
     socket-keep-alive="${mongo.socketKeepAlive}"
     socket-timeout="${mongo.socketTimeout}"
     slave-ok="${mongo.slaveOk}"
     write-number="1"
     write-timeout="0"
     write-fsync="true"/>
</mongo:mongo-client>

<mongo:db-factory dbname="database" mongo-ref="mongoClient"/>

<bean id="anotherMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
  <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
MongodbTemplate一旦配置好,是线程安全的的,可以跨多个实例使用
@Configuration
public class AppConfig {

  public @Bean MongoClient mongoClient() {
      return new MongoClient("localhost");
  }

  public @Bean MongoTemplate mongoTemplate() {
      return new MongoTemplate(mongoClient(), "mydatabase");
  }
}

mongodb的构造函数:
MongoTemplate(MongoClient mongo, String databaseName): 
MongoTemplate(MongoDbFactory mongoDbFactory): 
MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter): 

<mongo:mongo-client host="localhost" port="27017"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
  <constructor-arg ref="mongoClient"/>
  <constructor-arg name="databaseName" value="geospatial"/>
</bean>

 官方推荐使用MongoOperations.对mongodb进行操作..

public class MongoApp {
  private static final Log log = LogFactory.getLog(MongoApp.class);
  public static void main(String[] args) {
    MongoOperations mongoOps = new MongoTemplate(new SimpleMongoDbFactory(new MongoClient(), "database"));
   Person p = new Person("Joe", 34);
    // Insert is used to initially store the object into the database.
    mongoOps.insert(p);
    log.info("Insert: " + p);
    // Find
    p = mongoOps.findById(p.getId(), Person.class);
    log.info("Found: " + p);
    // Update
    mongoOps.updateFirst(query(where("name").is("Joe")), update("age", 35), Person.class);
    p = mongoOps.findOne(query(where("name").is("Joe")), Person.class);
    log.info("Updated: " + p);
    // Delete
    mongoOps.remove(p);
    // Check that deletion worked
    List<Person> people =  mongoOps.findAll(Person.class);
    log.info("Number of people = : " + people.size());
    mongoOps.dropCollection(Person.class);
  }
}

  

猜你喜欢

转载自www.cnblogs.com/dibinbin/p/9444764.html