当前位置: 首页 > news >正文

房山 网站建设东莞市品牌网站建设报价

房山 网站建设,东莞市品牌网站建设报价,网站开发流程书籍,wordpress内容函数先说说MongoDB是什么。MongoDB官网也有各语言所用的API#xff0c;当然也有Java的了。但是平日写的东西大部分是基于Spring框架的#xff0c;自然希望对于数据库的操作也在Spring的掌控之下。前几日才Spring的官网看到了SPRING DATA MONGODB#xff0c;对此很感兴趣稍微试了…先说说MongoDB是什么。MongoDB官网也有各语言所用的API当然也有Java的了。但是平日写的东西大部分是基于Spring框架的自然希望对于数据库的操作也在Spring的掌控之下。前几日才Spring的官网看到了SPRING DATA MONGODB对此很感兴趣稍微试了一下。首先引用相关的包主要是以下两个spring-data-mongodb-1.0.0.M4.jarspring-data-commons-1.2.0.M1.jar其他诸如mongo-java-driver.jar的还是需要的看自己的需要。下面开始我们的操作了。1.首先注册一个Mongo的实例(1)方法一Configurationpublic class AppConfig {public Bean Mongo mongo() throws UnknownHostException {return new Mongo(localhost); //如果是远程的就填IP}}(2)方法二xml配置文件xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:mongohttp://www.springframework.org/schema/data/mongoxsi:schemaLocationhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/data/mongohttp://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd还有一些细节可以设置threads-allowed-to-block-for-connection-multiplier4connect-timeout1000 //连接超时时间max-wait-time1500} //等待时间auto-connect-retrytruesocket-keep-alivetruesocket-timeout1500 //Socket超时时间slave-oktruewrite-number1write-timeout0write-fsynctrue/说实在话我对于其中的几个timeout分的不是很清楚希望了解的朋友指教。2.MongoDbFactory这个是位于org.springframework.data.mongodb.core下的主要有两个方法DB getDb() throws DataAccessException;DB getDb(String dbName) throws DataAccessException;现在注册一个工厂实例(1)方法一Configurationpublic class MongoConfiguration {public Bean MongoDbFactory mongoDbFactory() throws Exception {return new SimpleMongoDbFactory(new Mongo(), databaseName);}}(2)方法二Xml配置还有usernamepassword的属性可选详细参考文档。3.MongoTemplate的使用还是先注册一下public Bean MongoTemplate mongoTemplate() throws Exception {return new MongoTemplate(mongo(), databaseName);}同样支持Xml方式配置到这里就差不多了。具体的使用倒是没有什么可说的参考API就可以搞定了。最常用的
http://www.sadfv.cn/news/3116/

相关文章: