1.新建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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd">
<!-- 加载mongodb的属性配置文件 -->
<!-- <context:property-placeholder location="mongodb-config.properties" ignore-unresolvable="true" /> -->
<mongo:mongo-client id="mongo" 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}"
socket-keep-alive="${mongo.socketKeepAlive}"
socket-timeout="${mongo.socketTimeout}"/>
</mongo:mongo-client>
<mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" mongo-ref="mongo" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" lazy-init="false">
<!-- <constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="${mongo.dbname}" /> -->
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
<!-- 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 -->
<!--<mongo:mapping-converter base-package="jaky.web.*.dao" />-->
<!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 -->
<!--<mongo:repositories base-package="jaky.web.*" />-->
</beans>
mongodb-config.xml文件新建成功。
2.新建MongoDB-config.properties
# MongoDB
# 做测试 可以先用下面这个数据库
mongo.host=localhost
mongo.dbname=test
#mongo.host=192.168.20.208
#mongo.dbname=unknown
mongo.port=27017
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#连接超时时间
mongo.connectTimeout=1000
#等待时间
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
#Socket超时时间
mongo.socketTimeout=1500
mongo.slaveOk=true
mongo.host=localhost 连接的数据库地址ip
mongo.dbname=test 要连接的数据库
3.映入到配置文件中
在applicationContext.xml文件中映入刚才两个文件
<!--对bean中的属性值进行外在化管理 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:common.properties</value>
<value>classpath:mongodb-config.properties</value>
</list>
</property>
</bean>
测试:
打开C:\Program Files\MongoDB\Server\3.6\bin mongod.exe 服务测试
后记:
学而不是则罔,思而不学则殆