spring集成mongodb 小试牛刀

这篇文章主要是简单集成下mongodb,测试一下,使用java简单操作一下mongodb

先看需要的依赖(除了spring的核心包之外另外需要两个依赖)


<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongo-java-driver</artifactId>
  <version>2.14.2</version>
</dependency>

一个是spring对mongodb的集成,一个是mongodb对java的支持

然后不用多说肯定是一大堆的配置,不过这里不会有太多

配置mongodb的连接池,工厂等

<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-3.0.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-3.0.xsd">

<!-- 自动扫描 -->
<context:component-scan base-package="com.quwei" />

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:mongo.properties</value>
        </list>
    </property>
</bean>
<!--连接池配置-->
<mongo:mongo host="${mongo.host}" port="${mongo.port}">
    <mongo:options connections-per-host="${mongo.options.connections-per-host}"
                   threads-allowed-to-block-for-connection-multiplier="${mongo.options.threads-allowed-to-block-for-connection-multiplier}"
                   connect-timeout="${mongo.options.connect-timeout}"
                   max-wait-time="${mongo.options.max-wait-time}"
                   auto-connect-retry="${mongo.options.auto-connect-retry}"
                   socket-keep-alive="${mongo.options.socket-keep-alive}"
                   socket-timeout="${mongo.options.socket-timeout}"
                   slave-ok="${mongo.options.slave-ok}"
                   write-number="${mongo.options.write-number}"
                   write-timeout="${mongo.options.write-timeout}"
                   write-fsync="${mongo.options.write-fsync}"/>
</mongo:mongo>
<!--连接池工厂配置-->
<mongo:db-factory dbname="${mongo.dbname}" mongo-ref="mongo"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
<!--&lt;!&ndash;实体映射自动扫描注入的包&ndash;&gt;-->
<!--<mongo:mapping-converter>-->
    <!--<mongo:custom-converters base-package="com.shunova.core.entity.mongo" />-->
<!--</mongo:mapping-converter>-->

</beans>

mongo的配置文件

image.png

其他的tomcat和springmvc的配置没什么区别所以就不贴出来了

然后是简单的插入和查询的一个mongo 做的dao层 不过这里我只是为了简单测试 所以写在了service方便控制器调用

先看项目结构

image.png

mongo的简单dao层

image.png

控制层

image.png
image.png

为了方便存储,做的一个简单的用户bean对象

image.png

然后下面看测试流程

测试使用接口的方式

image.png

看mongodb里面是否有这条刚插入的数据(这里使用的默认的test库)

image.png

这好像就是我们刚插入的数据

下面来通过姓名来获取

image.png

比对下里面的数据,和刚插入进mongo的数据一摸一样,证明我们的测试成功了

简单测试就到这里了,方便自己复习

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容