Mongodb与spring集成(1)------配置 (转)

这里我们用到的是spring-data中一个集成mongodb的项目,首先在maven中添加对它的依赖,这里我用的是1.0.0.M5版本

 

   
   
   
   
<!-- mongodb spring --> < dependency > < groupId > org.springframework.data </ groupId > < artifactId > spring-data-mongodb </ artifactId > < version > 1.0.0.M5 </ version > </ dependency >

 

然后是配置文件

   
   
   
   
<? xml version="1.0" encoding="UTF-8" ?> < beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:context ="http://www.springframework.org/schema/context" xmlns:mongo ="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation ="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" > < context:property-placeholder location ="classpath*:META-INF/mongodb/mongodb.properties" /> <!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 --> < mongo:mongo id ="mongo" replica-set ="localhost:27017" > <!-- 一些连接属性的设置 --> < mongo: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 > <!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 --> < mongo:db-factory dbname ="test" mongo-ref ="mongo" /> <!-- mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成 --> < bean id ="mongoTemplate" class ="org.springframework.data.mongodb.core.MongoTemplate" > < constructor-arg name ="mongoDbFactory" ref ="mongoDbFactory" /> </ bean > <!-- 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 --> < mongo:mapping-converter base-package ="com.xxx.xxx.domain" /> <!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 --> < mongo:repositories base-package ="com.xxx.xxx.persist.mongodb" /> <!-- To translate any MongoExceptions thrown in @Repository annotated classes --> < context:annotation-config /> </ beans >

 

这样基本配置就完成了,其它深入操作将在后续文章中说明。

你可能感兴趣的:(Mongodb与spring集成(1)------配置 (转))