SSM中加入MongoDB配置

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 服务测试

 

后记:

学而不是则罔,思而不学则殆

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值