1.新建xml文件
新建mongodb-config.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>
2.新建properties 文件
mongodb-config.properties文件
# MongoDB
mongo.host=localhost
mongo.dbname=exmcp
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 如果你的mogodb没有设置其他电脑访问,最好用localhost,如果用ip,需要在 mongodb中设置其他user
可以访问,否则连接会被拒绝;
当然,你也可以在启动时候设置:
在cmd中,切换到目录下 ,mongod.exe 是启动的程序 --bind_ip:0.0.0.0 表示允许任何机器连接
3.引入文件
在applicationContext.xml文件中配置如下:
<context:property-placeholder location="classpath:common.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="classpath:mongodb-config.properties" ignore-unresolvable="true"/>
<!--mongodb 配置 -->
<import resource="classpath:mongodb-config.xml"/>
注意:ignore-unresolvable="true" 这个得有,properties默认引入一次,所以用这个多次引入
4.jar包
由于这是ssm项目,需要手动加包
根据自己的版本选择
5.结构
测试:
打开C:\Program Files\MongoDB\Server\3.6\bin mongod.exe 服务测试
后记:
学而不是则罔,思而不学则殆