美文网首页
ssh2框架整合 mongodb

ssh2框架整合 mongodb

作者: 游泳的星尘龙 | 来源:发表于2018-07-27 19:16 被阅读0次

struts2+ spring +hibernate+ mongodb

业务升级需要用到mongodb存储一些信息。但是网上找不到说明,最近弄好了写个帖子记录一下配置的过程。免得以后忘了。
其实就是简单的spring+mongodb而已。不过网上全是maven,但是这个工程很久又没升级一开始配置各种问题。
好了下面开始说明:

首先要准备好几个jar包:

spring-core-4.3.0.RELEASE.jar
spring-data-commons-1.10.0.RELEASE.jar
spring-data-mongodb-1.5.2.RELEASE.jar
mongo-java-driver-2.13.0.jar(这个不确定是不是必要的,但上面的少一个都会启动失败)

加到工程lib文件夹里,如果工具不会自动加载那就需要自己去工程属性里配置一下啦

新建mongo.properties(mongodb属性配置文件)

#mongodb的地址和端口
mongo.hostport=192.168.1.107:27017
#库的名称
mongo.dbname=db_test
#用户
mongo.username=cas-config
#密码
mongo.password=123456
mongo.connectionsPerHost=20
mongo.threadsAllowedToBlockForConnectionMultiplier=4
mongo.connectTimeout=20000
mongo.maxWaitTime=15000
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.socketTimeout=15000
mongo.slaveOk=true

新建spring-mongo.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.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.xsd">
          
    <!-- 加载mongodb的属性配置文件 -->
    <context:property-placeholder location="classpath:mongo.properties" />

    <!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 -->
    <mongo:mongo id="mongo" replica-set="${mongo.hostport}">
        <!-- 一些连接属性的设置 -->
        <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:db-factory dbname="${mongo.dbname}" mongo-ref="mongo" username="${mongo.username}"
                      password="${mongo.password}"/>

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="mongo"/>
        <constructor-arg name="databaseName" value="${mongo.dbname}"/>
        <constructor-arg ref="userCredentials"/>
    </bean>
    <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
        <constructor-arg name="username" value="${mongo.username}"/>
        <constructor-arg name="password" value="${mongo.password}"/>
    </bean>

</beans>

最后在applicationContext.xml导入mongodb的配置文件

<import resource="spring-mongo.xml" />

配置好自动检测扫描,详细的配置网上很多自行搜索。

<!-- 使用 annotation -->
<context:annotation-config />
<!-- 使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入 -->
<context:component-scan base-package="com.demo" />

配置到这里就完成了。其他新建对象,Dao层什么的和一般的没差。我这里就不多讲,只是记录一下配置的过程。写的有点粗糙,不过大概就是这样子。

相关文章

网友评论

      本文标题:ssh2框架整合 mongodb

      本文链接:https://www.haomeiwen.com/subject/kebgmftx.html