這篇文章主要是簡(jiǎn)單集成下mongodb,測(cè)試一下,使用java簡(jiǎn)單操作一下mongodb
先看需要的依賴(除了spring的核心包之外另外需要兩個(gè)依賴)
<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>
一個(gè)是spring對(duì)mongodb的集成,一個(gè)是mongodb對(duì)java的支持
然后不用多說肯定是一大堆的配置,不過這里不會(huì)有太多
配置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">
<!-- 自動(dòng)掃描 -->
<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>
<!--<!–實(shí)體映射自動(dòng)掃描注入的包–>-->
<!--<mongo:mapping-converter>-->
<!--<mongo:custom-converters base-package="com.shunova.core.entity.mongo" />-->
<!--</mongo:mapping-converter>-->
</beans>
mongo的配置文件

其他的tomcat和springmvc的配置沒什么區(qū)別所以就不貼出來了
然后是簡(jiǎn)單的插入和查詢的一個(gè)mongo 做的dao層 不過這里我只是為了簡(jiǎn)單測(cè)試 所以寫在了service方便控制器調(diào)用
先看項(xiàng)目結(jié)構(gòu)

mongo的簡(jiǎn)單dao層

控制層


為了方便存儲(chǔ),做的一個(gè)簡(jiǎn)單的用戶bean對(duì)象

然后下面看測(cè)試流程
測(cè)試使用接口的方式

看mongodb里面是否有這條剛插入的數(shù)據(jù)(這里使用的默認(rèn)的test庫)

這好像就是我們剛插入的數(shù)據(jù)
下面來通過姓名來獲取

比對(duì)下里面的數(shù)據(jù),和剛插入進(jìn)mongo的數(shù)據(jù)一摸一樣,證明我們的測(cè)試成功了
簡(jiǎn)單測(cè)試就到這里了,方便自己復(fù)習(xí)