zoukankan      html  css  js  c++  java
  • spring4与mongodb的集成

    新项目的辅助系统,需要用到mongo系统,今天再次将其使用环境进行了操作搭建。还是遇到一些问题,毕竟之前使用的场景和现在的不同。版本也不一样了。

    本次使用的环境:

    mongo:3.4.4版本

    OS: redhat7

    java操作mongo库

    1. 首先,mongo数据库的安装

    直接到mongodb官网下载mongodb-linux-x86_64-rhel70-3.4.4.tgz。解压之后,运行mongod即可启动服务。当然,我这里是为了项目开发用,第一步就启用单实例。

    看看我们的mongodb的配置:

    #数据库数据存放路径
    dbpath = /home/tkrobot/mongodb/data
    #日志存放路径 logpath
    = /home/tkrobot/mongodb/logs/mongodb.log logappend = true port = 27017 fork = true
    #启动认证功能,要想连接到数据库,必须经过用户名密码认证才可以 auth = true
    #禁止http访问数据库 nohttpinterface = true
    #连接数据库时必须是这个ip bind_ip = 10.90.7.10 journal = true
    #最大连接数 maxConns = 100

    这里,需要在mongod启动前,创建好/home/tkrobot/mongodb/data以及/home/tkrobot/mongodb/logs两个目录。

    启动mongod的脚本:

    ./mongod --config /etc/mongodb.conf

    这样,mongod就算启动好了。下面有其启动日志:

    2017-05-02T11:20:03.553+0800 I CONTROL  [initandlisten] MongoDB starting : pid=98891 port=27017 dbpath=/home/tkrobot/mongodb/data 64-bit host=localhost.localdomain
    2017-05-02T11:20:03.553+0800 I CONTROL  [initandlisten] db version v3.4.4
    2017-05-02T11:20:03.553+0800 I CONTROL  [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
    2017-05-02T11:20:03.553+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
    2017-05-02T11:20:03.553+0800 I CONTROL  [initandlisten] allocator: tcmalloc
    2017-05-02T11:20:03.553+0800 I CONTROL  [initandlisten] modules: none
    2017-05-02T11:20:03.553+0800 I CONTROL  [initandlisten] build environment:
    2017-05-02T11:20:03.553+0800 I CONTROL  [initandlisten]     distmod: rhel70
    2017-05-02T11:20:03.553+0800 I CONTROL  [initandlisten]     distarch: x86_64
    2017-05-02T11:20:03.553+0800 I CONTROL  [initandlisten]     target_arch: x86_64
    2017-05-02T11:20:03.553+0800 I CONTROL  [initandlisten] options: { config: "/etc/mongodb.conf", net: { bindIp: "10.90.7.10", http: { enabled: false }, maxIncomingConnections: 100, port: 27017 }, security: { authorization: "enabled" }, st
    orage: { dbPath: "/home/tkrobot/mongodb/data", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/home/tkrobot/mongodb/logs/mongodb.log" } }
    2017-05-02T11:20:03.553+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=15438M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=jo
    urnal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
    2017-05-02T11:20:03.567+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
    2017-05-02T11:20:03.567+0800 I CONTROL  [initandlisten]
    2017-05-02T11:20:03.567+0800 I CONTROL  [initandlisten]
    2017-05-02T11:20:03.568+0800 I CONTROL  [initandlisten] ** WARNING: You are running on a NUMA machine.
    2017-05-02T11:20:03.568+0800 I CONTROL  [initandlisten] **          We suggest launching mongod like this to avoid performance problems:
    2017-05-02T11:20:03.568+0800 I CONTROL  [initandlisten] **              numactl --interleave=all mongod [other options]
    2017-05-02T11:20:03.568+0800 I CONTROL  [initandlisten]
    2017-05-02T11:20:03.568+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
    2017-05-02T11:20:03.568+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2017-05-02T11:20:03.568+0800 I CONTROL  [initandlisten]
    2017-05-02T11:20:03.568+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
    2017-05-02T11:20:03.568+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    2017-05-02T11:20:03.568+0800 I CONTROL  [initandlisten]
    2017-05-02T11:20:03.571+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/home/tkrobot/mongodb/data/diagnostic.data'
    2017-05-02T11:20:03.575+0800 I INDEX    [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
    2017-05-02T11:20:03.575+0800 I INDEX    [initandlisten]          building index using bulk method; build may temporarily use up to 500 megabytes of RAM
    2017-05-02T11:20:03.576+0800 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
    2017-05-02T11:20:03.576+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
    2017-05-02T11:20:03.576+0800 I NETWORK  [thread1] waiting for connections on port 27017

    2. 接下来,我们操作一下,验证数据库部署的情况是否ok。

    为了方便使用mongo客户端指令,将解压后的mongo配置到环境变量里面,方便操作。

    export MONGODB_PATH=/home/tkrobot/mongodb-linux-x86_64-rhel70-3.4.4

    export PATH=$JAVA_HOME/bin:$MONGODB_PATH/bin:$PATH

    用mongo登录操作数据库:

    [root@localhost bin]# mongo
    MongoDB shell version v3.4.4
    connecting to: mongodb://10.90.7.10:27017
    MongoDB server version: 3.4.4
    Welcome to the MongoDB shell.
    For interactive help, type "help".
    For more comprehensive documentation, see
            http://docs.mongodb.org/
    Questions? Try the support group
            http://groups.google.com/group/mongodb-user
    > show dbs;
    2017-05-02T11:21:08.878+0800 E QUERY    [thread1] Error: listDatabases failed:{
            "ok" : 0,
            "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
            "code" : 13,
            "codeName" : "Unauthorized"
    } :
    _getErrorWithCode@src/mongo/shell/utils.js:25:13
    Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
    shellHelper.show@src/mongo/shell/utils.js:769:19
    shellHelper@src/mongo/shell/utils.js:659:15
    @(shellhelp2):1:1

    没有权限,因为我们在mongod.conf里面指定要auth了。最简单的办法,就是将mongod.conf里面的auth注释掉,重新启动mongod,进行权限配置。

    在没有auth属性的情况下,启动mongod,然后再次mongo连接上数据库,use指定的数据库,创建一个专属的用户。

    use robotkdb

    指定数据库robotkdb后,创建一个用户robotassister。

    db.createUser({
    ... user:"robotassister",
    ... pwd:"xxyyyxxxxx",
    ... roles:[{role:"dbOwner",db:"robotkdb“}]})

    这样,就相当于给指定的数据库robotkdb创建了一个角色为dbOwener的用户robotassister。

    最后,将mongodb.conf中的auth重新放开,启用auth,再启动mongod,并进行指定数据库和用户名的方式连接到mongodb:

    mongo --host 10.90.7.10 --port 27017 -u robotassister -p xxyyyxxxx robotdb

    如此,一切都ok,单实例的mongod配置带auth的启动,客户端通过credentials的方式连接到指定的库,准备工作就绪。

    3. spring集成mongodb。

    安装spring官方的文档介绍,这个过程是比较简单的。集成过程中需要用到的支持mongodb的jar包也不多。主要有:

    mongo-java-driver-3.4.1.jar
    spring-data-commons-1.10.0.RELEASE.jar
    spring-data-commons-core-1.4.1.RELEASE.jar
    spring-data-mongodb-1.7.0.RELEASE.jar

    上述的jar是我的集成过程中用到的。

    我在自己既有的spring mvc的web项目里面,添加了mongodb的支持。

    首先看看mongodb的xml配置文件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" 
        xmlns:util="http://www.springframework.org/schema/util" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context-3.2.xsd       
              http://www.springframework.org/schema/util 
              http://www.springframework.org/schema/util/spring-util-3.2.xsd" 
            default-lazy-init="default">
        
        <!--credentials的配置形式是:用户名:密码@默认数据库-->
        <mongo:mongo-client id="mongoClient" host="${mongo.host}" port="${mongo.port}"    credentials="${mongo.username}:${mongo.password}@${mongo.dbname}">        
            <mongo:client-options  write-concern="SAFE"
                connections-per-host="${mongo.connectionsPerHost}" 
                threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" 
                connect-timeout="${mongo.connectTimeout}" 
                max-wait-time="${mongo.maxWaitTime}" 
                socket-timeout="${mongo.socketTimeout}"/>        
        </mongo:mongo-client>
        
        <mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" mongo-ref="mongoClient" />
        
        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
        </bean>    
    </beans>

    其中的参数配置文件mongo.properties:

    mongo.dbname=robotkdb
    mongo.host=10.90.7.10
    mongo.port=27017
    mongo.username=robotassister
    mongo.password=xxyyyxxxxx
    #一个线程变为可用的最大阻塞数
    mongo.connectionsPerHost=8
    #线程队列数,它以上面connectionsPerHost值相乘的结果就是线程队列最大值
    mongo.threadsAllowedToBlockForConnectionMultiplier=4
    #连接超时时间(毫秒) 
    mongo.connectTimeout=1500
    #最大等待时间
    mongo.maxWaitTime=1500
    #自动重连
    mongo.autoConnectRetry=true
    #scoket保持活动
    mongo.socketKeepAlive= true
    #scoket超时时间
    mongo.socketTimeout=1500
    #读写分离
    mongo.slaveOk=true

    接下来,看看spring的配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop"
           xmlns:cache="http://www.springframework.org/schema/cache" 
           xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
        
        <bean id="configRealm" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
             <property name="locations">
                <list>
                    <value>classpath:conf/internal.properties</value>
                    <value>classpath:conf/jdbc.properties</value>
                    <value>classpath:conf/redis.properties</value>
                    <value>classpath:conf/session.properties</value>                
                    <value>classpath:conf/mongo.properties</value>
                </list>
            </property>
        </bean>
         <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
             <property name="properties" ref="configRealm"/>
        </bean>
        
        
        <bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
            <property name="cacheManager" ref="ehcacheManager"/>
        </bean>
    
        <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            <property name="configLocation" value="classpath:conf/ehcache.xml"/>
        </bean>
    
        <cache:annotation-driven cache-manager="springCacheManager"/>
        
        <import resource="spring-servlet.xml"/>     
        <import resource="spring-cache.xml"/>     
        <import resource="spring-dao.xml"/>    
        <import resource="spring-redis.xml"/>
        <import resource="spring-mongo.xml"/>
    </beans>

    ok,到此,所有的配置工作都完毕了,我们就启动web项目,看看是否能正常启动吧!遗憾,出了下面的错误!!!!!

    五月 02, 2017 4:49:49 下午 org.apache.catalina.core.StandardContext listenerStart
    严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [spring-mongo.xml]
    Offending resource: class path resource [conf/applicationContext.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 275 in XML document from class path resource [conf/spring-mongo.xml] is invalid; nested exception is org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd; lineNumber: 275; columnNumber: 63; src-resolve: 无法将名称 'repository:auditing-attributes' 解析为 'attribute group' 组件。
        at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
        at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
        at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:274)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:199)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:184)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:147)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:101)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:495)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
        at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
        at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:542)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4811)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5251)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:744)
    Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 275 in XML document from class path resource [conf/spring-mongo.xml] is invalid; nested exception is org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd; lineNumber: 275; columnNumber: 63; src-resolve: 无法将名称 'repository:auditing-attributes' 解析为 'attribute group' 组件。
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:397)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:258)
        ... 28 more
    Caused by: org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd; lineNumber: 275; columnNumber: 63; src-resolve: 无法将名称 'repository:auditing-attributes' 解析为 'attribute group' 组件。
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:4162)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:4145)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(XSDHandler.java:1741)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDAttributeGroupTraverser.traverseLocal(XSDAttributeGroupTraverser.java:80)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDAbstractTraverser.traverseAttrsAndAttrGrps(XSDAbstractTraverser.java:643)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.processComplexContent(XSDComplexTypeTraverser.java:1122)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.traverseComplexTypeDecl(XSDComplexTypeTraverser.java:335)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.traverseLocal(XSDComplexTypeTraverser.java:164)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(XSDElementTraverser.java:392)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseGlobal(XSDElementTraverser.java:242)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseSchemas(XSDHandler.java:1433)
        at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:630)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:616)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(XMLSchemaValidator.java:2453)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1772)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:746)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:378)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2778)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
        at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:389)
        ... 31 more

    这个,还真是折腾了我一段时间,最终找到原因:

    上述这个错误,需要在spring-mongo.xml文件中添加下面的两行内容
    http://www.springframework.org/schema/data/repository
    http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd

    采用基于mongo:mongo-client标签进行安全配置,mongo要求spring-mongo.xsd必须是1.6及以上的版本。我们这里选用的是1.7.

    ok,再次运行看看吧。。。。。。结果如何呢。。。。。。。。。

    五月 02, 2017 6:03:55 下午 org.apache.catalina.core.StandardContext listenerStart
    严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [conf/spring-mongo.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/objenesis/ObjenesisStd
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:285)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1077)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:981)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:636)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:938)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4811)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5251)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:744)
    Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/objenesis/ObjenesisStd
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:121)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:277)
        ... 23 more
    Caused by: java.lang.NoClassDefFoundError: org/springframework/objenesis/ObjenesisStd
        at org.springframework.data.mongodb.core.convert.DefaultDbRefResolver.<init>(DefaultDbRefResolver.java:72)
        at org.springframework.data.mongodb.core.MongoTemplate.getDefaultMongoConverter(MongoTemplate.java:2029)
        at org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:216)
        at org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:201)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
        ... 25 more
    Caused by: java.lang.ClassNotFoundException: org.springframework.objenesis.ObjenesisStd
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139)
        ... 34 more        

    又出现了错误。。。看来不是很顺利啊,再研究看root cause是什么呢???依据

    爆出上述错误,经过查找原因,得知,sping-data-mongo要求spring-core必须是4.x的版本,也就是说spring4才行。

    源头是通过查找日志中红色部分的错误信息,得知相关的jar依赖文件,在spring-core 4.x的版本中默认含有,于是,将项目中原来3.2的配置信息,全部更新为4.2的,并相应的将工程的lib中spring的版本更新到4.2.再次启动应用。这次,一切都ok了。

    最终能正常运行的applicationContext.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:aop="http://www.springframework.org/schema/aop"
           xmlns:cache="http://www.springframework.org/schema/cache" 
           xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
        
        <bean id="configRealm" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
             <property name="locations">
                <list>
                    <value>classpath:conf/internal.properties</value>
                    <value>classpath:conf/jdbc.properties</value>
                    <value>classpath:conf/redis.properties</value>
                    <value>classpath:conf/session.properties</value>                
                    <value>classpath:conf/mongo.properties</value>
                </list>
            </property>
        </bean>
         <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
             <property name="properties" ref="configRealm"/>
        </bean>
        
        
        <bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
            <property name="cacheManager" ref="ehcacheManager"/>
        </bean>
    
        <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            <property name="configLocation" value="classpath:conf/ehcache.xml"/>
        </bean>
    
        <cache:annotation-driven cache-manager="springCacheManager"/>
        
        <import resource="spring-servlet.xml"/>     
        <import resource="spring-cache.xml"/>     
        <import resource="spring-dao.xml"/>    
        <import resource="spring-redis.xml"/>
        <import resource="spring-mongo.xml"/>
    </beans>
    View Code

    最终能正常运行的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" 
        xmlns:util="http://www.springframework.org/schema/util" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context-4.2.xsd 
              http://www.springframework.org/schema/data/mongo 
              http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd 
              http://www.springframework.org/schema/data/repository
              http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
              http://www.springframework.org/schema/util 
              http://www.springframework.org/schema/util/spring-util-4.2.xsd" 
            default-lazy-init="default">
        
        <!--credentials的配置形式是:用户名:密码@默认数据库-->
        <mongo:mongo-client id="mongoClient" host="${mongo.host}" port="${mongo.port}"    credentials="${mongo.username}:${mongo.password}@${mongo.dbname}">        
            <mongo:client-options  write-concern="SAFE"
                connections-per-host="${mongo.connectionsPerHost}" 
                threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" 
                connect-timeout="${mongo.connectTimeout}" 
                max-wait-time="${mongo.maxWaitTime}" 
                socket-timeout="${mongo.socketTimeout}"/>        
        </mongo:mongo-client>
        
        <mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" mongo-ref="mongoClient" />
        
        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
        </bean>    
    </beans>
    View Code

    4. 启用一个测试,验证一下spring和mongodb的集成是否成功吧

    先构建一个javabean:

    /**
     * @author "shihuc"
     * @date   2017年5月3日
     */
    package com.roomdis.center.mongo.model;
    
    import java.io.Serializable;
    
    import org.ietf.jgss.Oid;
    import org.springframework.data.mongodb.core.mapping.Document;
    import org.springframework.data.mongodb.core.mapping.Field;
    
    /**
     * @author chengsh05
     *
     */
    @Document(collection="buzz_element")
    public class BuzzElement implements Serializable {
        
        private static final long serialVersionUID = 1L;
        
        private Oid _id;
        
        @Field(value="item_id")
        private String itemId;
        @Field(value="item_name")
        private String itemName;
        @Field(value="price")
        private String price;
        @Field(value="desc")
        private String desc;
    
        public Oid get_id() {
            return _id;
        }
    
        public void set_id(Oid _id) {
            this._id = _id;
        }
    
        public String getItemId() {
            return itemId;
        }
    
        public void setItemId(String itemId) {
            this.itemId = itemId;
        }
    
        public String getItemName() {
            return itemName;
        }
    
        public void setItemName(String itemName) {
            this.itemName = itemName;
        }
    
        public String getPrice() {
            return price;
        }
    
        public void setPrice(String price) {
            this.price = price;
        }
    
        public String getDesc() {
            return desc;
        }
    
        public void setDesc(String desc) {
            this.desc = desc;
        }
    
        @Override
        public String toString() {
            return "ItemInfo [_id=" + _id + ", itemId=" + itemId + ", itemName="
                    + itemName + ", price=" + price + ", desc=" + desc + "]";
        }
    }

    再创建工具类BeanUtil:

    /**
     * @author "shihuc"
     * @date   2017年5月3日
     */
    package com.roomdis.center.mongo.util;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.util.Date;
    
    import org.apache.commons.beanutils.BeanUtils;
    
    import com.mongodb.BasicDBObject;
    import com.mongodb.DBObject;
    
    /**
     * @author chengsh05
     *
     */
    public class BeanUtil {
    
    
        /**
         * 把实体bean对象转换成DBObject
         * @param bean
         * @return
         * @throws IllegalArgumentException
         * @throws IllegalAccessException
         */
        public static <T> DBObject bean2DBObject(T bean) throws IllegalArgumentException,
            IllegalAccessException {
          if (bean == null) {
            return null;
          }
          DBObject dbObject = new BasicDBObject();
          // 获取对象对应类中的所有属性域
          Field[] fields = bean.getClass().getDeclaredFields();
          
          // 获取所有注解
          for (Field field : fields) {
              org.springframework.data.mongodb.core.mapping.Field anno = field.getAnnotation(org.springframework.data.mongodb.core.mapping.Field.class);
            // 获取属性名
            String varName = field.getName();
            
            if("serialVersionUID".equals(varName) || "_id".equals(varName)){
                continue;
            }
            
            // 获取注解的值
            varName = anno.value();
            // 修改访问控制权限
            boolean accessFlag = field.isAccessible();
            if (!accessFlag) {
              field.setAccessible(true);
            }
            Object param = field.get(bean);
            if (param == null) {
              continue;
            } else if (param instanceof Integer) {//判断变量的类型
              int value = ((Integer) param).intValue();
              dbObject.put(varName, value);
            } else if (param instanceof String) {
              String value = (String) param;
              dbObject.put(varName, value);
            } else if (param instanceof Double) {
              double value = ((Double) param).doubleValue();
              dbObject.put(varName, value);
            } else if (param instanceof Float) {
              float value = ((Float) param).floatValue();
              dbObject.put(varName, value);
            } else if (param instanceof Long) {
              long value = ((Long) param).longValue();
              dbObject.put(varName, value);
            } else if (param instanceof Boolean) {
              boolean value = ((Boolean) param).booleanValue();
              dbObject.put(varName, value);
            } else if (param instanceof Date) {
              Date value = (Date) param;
              dbObject.put(varName, value);
            }
            // 恢复访问控制权限
            field.setAccessible(accessFlag);
          }
          
          return dbObject;
        }
    
        /**
         * 把DBObject转换成bean对象
         * @param dbObject
         * @param bean
         * @return
         * @throws IllegalAccessException
         * @throws InvocationTargetException
         * @throws NoSuchMethodException
         */
        public static <T> T dbObject2Bean(DBObject dbObject, T bean) throws IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {
          if (bean == null) {
            return null;
          }
          Field[] fields = bean.getClass().getDeclaredFields();
          for (Field field : fields) {
            String varName = field.getName();
            org.springframework.data.mongodb.core.mapping.Field anno = field.getAnnotation(org.springframework.data.mongodb.core.mapping.Field.class);
            if("serialVersionUID".equals(varName) || "_id".equals(varName)){
                continue;
            }
            
            String fieldName = anno.value();
            Object object = dbObject.get(fieldName);
            if (object != null) {
              BeanUtils.setProperty(bean, varName, object);
            }
          }
          return bean;
        }
    }

    再次,创建服务,进行操作数据库,相当于dao层:

    /**
     * @author "shihuc"
     * @date   2017年5月3日
     */
    package com.roomdis.center.mongo.service;
    
    import java.util.List;
    
    import org.json.JSONObject;
    
    import com.roomdis.center.mongo.model.BuzzElement;
    
    /**
     * @author chengsh05
     *
     */
    public interface IBuzzElementService {
        // 查询
        public List<BuzzElement> getItemInfo(JSONObject json) throws Exception;
        
        // 保存
        public int save(BuzzElement itemInfo) throws Exception;
        
        // 更新
        public void update(BuzzElement intemInfo) throws Exception;
    }
    /**
     * @author "shihuc"
     * @date   2017年5月3日
     */
    package com.roomdis.center.mongo.service.impl;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    import org.json.JSONObject;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.mongodb.core.MongoTemplate;
    import org.springframework.stereotype.Service;
    
    import com.mongodb.BasicDBObject;
    import com.mongodb.DBCollection;
    import com.mongodb.DBCursor;
    import com.mongodb.DBObject;
    import com.mongodb.WriteResult;
    import com.roomdis.center.mongo.model.BuzzElement;
    import com.roomdis.center.mongo.service.IBuzzElementService;
    import com.roomdis.center.mongo.util.BeanUtil;
    
    /**
     * @author chengsh05
     *
     */
    @Service("buzzElementService")
    public class BuzzElementServiceImpl implements IBuzzElementService {
        
        @Autowired
        private MongoTemplate mongoTemplate;
        private final static String COLLECTION_NAME = "buzz_element"; 
        
        /* (non-Javadoc)
         * @see com.roomdis.center.mongo.service.IBuzzElementService#getItemInfo(org.json.JSONObject)
         */
        @Override
        public List<BuzzElement> getItemInfo(JSONObject json) throws Exception {
            List<BuzzElement> list = new ArrayList<BuzzElement>();
            // 判断查询的json中传递过来的参数
            DBObject query = new BasicDBObject();
    
            if(json.has("item_id")){            
                query.put("item_id", json.getString("item_id"));
            }else if(json.has("item_name")){
                query.put("item_name", json.getString("item_name"));
            }
            
            DBCursor results = mongoTemplate.getCollection(COLLECTION_NAME).find(query);
            if(null != results){
                Iterator<DBObject> iterator = results.iterator();
                while(iterator.hasNext()){
                    BasicDBObject obj = (BasicDBObject) iterator.next();
                    BuzzElement itemInfo = new BuzzElement();
                    itemInfo = BeanUtil.dbObject2Bean(obj, itemInfo);
                    list.add(itemInfo);
                }
            }
            
            
            return list;
        }
    
        /* (non-Javadoc)
         * @see com.roomdis.center.mongo.service.IBuzzElementService#save(com.roomdis.center.mongo.model.BuzzElement)
         */
        @Override
        public int save(BuzzElement itemInfo) throws Exception {      
            DBCollection collection = this.mongoTemplate.getCollection(COLLECTION_NAME);
            int result = 0;
            DBObject iteminfoObj = BeanUtil.bean2DBObject(itemInfo);
            
            //iteminfoObj.removeField("serialVersionUID");
            //result = collection.insert(iteminfoObj).getN();
            WriteResult writeResult = collection.save(iteminfoObj);
            result = writeResult.getN();
            return result;
        }
    
        /* (non-Javadoc)
         * @see com.roomdis.center.mongo.service.IBuzzElementService#update(com.roomdis.center.mongo.model.BuzzElement)
         */
        @Override
        public void update(BuzzElement intemInfo) throws Exception {
            DBCollection collection = this.mongoTemplate.getCollection(COLLECTION_NAME);
            BuzzElement queryItemInfo = new BuzzElement();
            queryItemInfo.setItemId(intemInfo.getItemId());
            DBObject itemInfoObj = BeanUtil.bean2DBObject(intemInfo);
            DBObject query =  BeanUtil.bean2DBObject(queryItemInfo);
            collection.update(query, itemInfoObj);        
        }
    }

    郑重说明,上述的javabean以及BeanUtil还有服务程序类,是参照网络资源,考虑到这些东西不是本博的重点,也不是关键,所以没有花时间在这个上面。

    最后,我们写一个controller来模拟一下添加数据,查询数据以及更新数据,看看工作流程是否顺畅!

    /**
     * @author "shihuc"
     * @date   2017年5月3日
     */
    package com.roomdis.center.mongo.controller;
    
    import java.util.List;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    
    import org.json.JSONObject;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.roomdis.center.mongo.model.BuzzElement;
    import com.roomdis.center.mongo.service.IBuzzElementService;
    
    /**
     * @author chengsh05
     *
     */
    @Controller
    @RequestMapping("/mongo")
    public class MongoOpController {
    
        @Resource(name="buzzElementService")
        private IBuzzElementService bes;
        
        @RequestMapping("/save")
        @ResponseBody
        public String doSave(HttpServletRequest req){
            
            String itemName = req.getParameter("itemName");
            String price = req.getParameter("price");
            String desc = req.getParameter("desc");
            BuzzElement be = new BuzzElement();
            be.setItemName(itemName);
            be.setDesc(desc);
            be.setPrice(price);
            try {
                bes.save(be);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "Save OK";
        }
        
        @RequestMapping("/find")
        @ResponseBody
        public String doFind(HttpServletRequest req){
            
            String itemName = req.getParameter("itemName");        
            JSONObject json = new JSONObject();
            json.putOnce("item_name", itemName);
            List<BuzzElement> res = null;
            try {
                res = bes.getItemInfo(json);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return "Find OK: " + res.size() ;
        }
        
        @RequestMapping("/update")
        @ResponseBody
        public String doUpdate(HttpServletRequest req){
            
            String itemName = req.getParameter("itemName");
            String price = req.getParameter("price");
            String desc = req.getParameter("desc");
            
            BuzzElement be = new BuzzElement();
            be.setItemName(itemName);
            be.setDesc(desc);
            be.setPrice(price);
            
            try {
                bes.update(be);
            } catch (Exception e) {        
                e.printStackTrace();
            }
            
            return "Update OK";
        }
    }

    好了,代码都准备ok了,下面进行测试!

    A.在浏览器地址栏执行:http://10.90.9.20:8080/RDcenter/mongo/save

    数据库得到的数据:

    > show collections
    buzz_element
    user
    > db.buzz_element.find()
    { "_id" : ObjectId("59092f3e0ef2eb286cd7410a") }

    B. 添加一条数据。在浏览器地址栏里面输入: http://10.90.9.20:8080/RDcenter/mongo/save?itemName=iphone7&price=7000&desc=good product, everyone loves it

    数据库得到的数据:

    > db.buzz_element.find()
    { "_id" : ObjectId("59092f3e0ef2eb286cd7410a") }
    { "_id" : ObjectId("590930480ef2eb286cd7410b"), "item_name" : "iphone7", "price" : "7000", "desc" : "good product, everyone loves it" }

    再次添加一条数据,在浏览器地址栏里面输入: http://10.90.9.20:8080/RDcenter/mongo/save?itemName=S8&price=8000&desc=samsung new product

    数据库得到的数据:

    > db.buzz_element.find()
    { "_id" : ObjectId("59092f3e0ef2eb286cd7410a") }
    { "_id" : ObjectId("590930480ef2eb286cd7410b"), "item_name" : "iphone7", "price" : "7000", "desc" : "good product, everyone loves it" }
    { "_id" : ObjectId("590933870ef2eb1bf8d04d70"), "item_name" : "S8", "price" : "8000", "desc" : "samsung new product" }

    C.查询数据,在浏览器地址栏输入:http://10.90.9.20:8080/RDcenter/mongo/find?itemName=S8

    最终,浏览器上得到的返回结果是:

    D. 更新数据,在浏览器地址栏输入:http://10.90.9.20:8080/RDcenter/mongo/update?itemName=S8&price=9000&desc=hope it is nice

    更新前的数据库数据:

    > db.buzz_element.find()
    { "_id" : ObjectId("59092f3e0ef2eb286cd7410a"), "item_name" : "iphone7", "desc" : "not everyone will buy it" }
    { "_id" : ObjectId("590930480ef2eb286cd7410b"), "item_name" : "iphone7", "price" : "7000", "desc" : "good product, everyone loves it" }
    { "_id" : ObjectId("590933870ef2eb1bf8d04d70"), "item_name" : "S8", "price" : "8000", "desc" : "samsung new product" }

    更新后的数据库数据:

    > db.buzz_element.find()
    { "_id" : ObjectId("59092f3e0ef2eb286cd7410a"), "item_name" : "S8", "price" : "9000", "desc" : "hope it is nice" }
    { "_id" : ObjectId("590930480ef2eb286cd7410b"), "item_name" : "iphone7", "price" : "7000", "desc" : "good product, everyone loves it" }
    { "_id" : ObjectId("590933870ef2eb1bf8d04d70"), "item_name" : "S8", "price" : "8000", "desc" : "samsung new product" }

    从上面看,数据库是更新了,但是貌似有点逻辑问题,将第一个更新了,我们希望是将既有的item_name为S8产品数据更新的,这个是业务逻辑问题,暂且不做修改了。。。

    从上面的全部过程来看,基于spring+mongodb的集成工作,完美结束,也很简单。

    剩下来的,重点是mongodb的增删改查相关的接口研究,是个熟悉的过程,各位朋友,建议去看官网的资料。

  • 相关阅读:
    HttpModule学习总结实例应用读书笔记
    SEO入门教程之入门相关
    HttpHandler学习总结实例应用读书笔记
    服务器安全设置总结(Win2003)
    网站建设合同书
    HTML标签解释大全
    敏捷之痒
    一个google浏览器很意思的东东
    C#访问非托管DLL
    随着DzNT的开源,我将投入到.NET的开发当中
  • 原文地址:https://www.cnblogs.com/shihuc/p/6800761.html
Copyright © 2011-2022 走看看