对于mongodb的引用,spring有对应的封装包。
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.10.2.RELEASE</version>
</dependency>
在引用对应jar包时,注意spring的版本问题。
在引入jar包完毕后。写spring-mongodb.xml文件。
<?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">
<!-- 加载mongodb的属性配置文件 --> <!-- 如果是将此xml引入到一个中心的xml进行初始化加载。若时中心的xml有读取properties文件的配置的话,这里再进行读取就会报错。所以这里注释掉 -->
<!-- <context:property-placeholder location="classpath:db-mongodb.properties"/> -->
<!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 -->
<mongo:mongo id="mongo" replica-set="${mongo.hostport}">
<!-- 一些连接属性的设置 -->
<mongo:options
connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1000"
max-wait-time="1500"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="1500"
slave-ok="true"
write-number="1"
write-timeout="0"
write-fsync="true"/>
</mongo:mongo>
<!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
<mongo:db-factory dbname="test" mongo-ref="mongo"/> <!-- 当mongodb设置了用户名密码时,就将此注释放开,若是无,就不要放开。会报错 -->
<!-- <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials"> -->
<!-- <constructor-arg name="username" value="${mongo.username}" /> -->
<!-- <constructor-arg name="password" value="${mongo.psw}" /> -->
<!-- </bean> -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="test"/>
<!-- <constructor-arg ref="userCredentials" /> -->
</bean>
<!-- 配置第二个mongodb数据库连接。若是需要多个库,就以此类推 -->
<mongo:mongo id="mongo2" replica-set="${mongo.hostport}">
<!-- 一些连接属性的设置 -->
<mongo:options
connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1000"
max-wait-time="1500"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="1500"
slave-ok="true"
write-number="1"
write-timeout="0"
write-fsync="true"/>
</mongo:mongo>
<!-- 第二个mongo的工厂,指向第二个数据库。通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
<mongo:db-factory dbname="test2" mongo-ref="mongo2"/>
<!-- <bean id="userCredentials2" class="org.springframework.data.authentication.UserCredentials"> -->
<!-- <constructor-arg name="username" value="${mongo.username}" /> -->
<!-- <constructor-arg name="password" value="${mongo.psw}" /> -->
<!-- </bean> -->
<bean id="mongoTemplate2" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo2"/>
<constructor-arg name="databaseName" value="test2"/>
<!-- <constructor-arg ref="userCredentials2" /> -->
</bean>
<!-- 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 -->
<mongo:mapping-converter base-package="com.wyn.moduls.mongodbmuduls"/>
<!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 -->
<mongo:repositories base-package="com.wyn.moduls.mongodbdao"/>
</beans>
对于dao层注入mongodb。
@Autowired
@Qualifier(value="mongoTemplate") //根据spring容器中的bean的id指定注入mongodb操作模板
private MongoTemplate mongoTemplate;
@Autowired
@Qualifier(value="mongoTemplate2")
private MongoTemplate mongoTemplate2;