日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring boot -mongodb

發布時間:2025/3/21 javascript 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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-optionsconnections-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);// Findp = mongoOps.findById(p.getId(), Person.class);log.info("Found: " + p);// UpdatemongoOps.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 workedList<Person> people = mongoOps.findAll(Person.class);log.info("Number of people = : " + people.size());mongoOps.dropCollection(Person.class);} }

?

  

轉載于:https://www.cnblogs.com/dibinbin/p/9444764.html

總結

以上是生活随笔為你收集整理的Spring boot -mongodb的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。