spring + springMVC+hibernate结合MongDB
1、创建一个mongo相关的xml文件,我使用的文件名是spring-context-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: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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.thinkgem.jeesite.mongo" />
<!-- 获取配置资源 -->
<context:property-placeholder location="classpath:mongodb.properties" />
<mongo:mongo id="mongo" replica-set="127.0.0.1:27017">
<!--
connections-per-host: 每个主机答应的连接数(每个主机的连接池大小),当连接池被用光时,会被阻塞住
max-wait-time: 被阻塞线程从连接池获取连接的最长等待时间(ms)
connect-timeout:在建立(打开)套接字连接时的超时时间(ms)
socket-timeout:套接字超时时间;该值会被传递给Socket.setSoTimeout(int)
slave-ok:指明是否答应驱动从次要节点或者奴隶节点读取数据
-->
<mongo:options
connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1000"
max-wait-time="1500"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="1500"
slave-ok="true"
write-number="1"
write-timeout="0"
write-fsync="true"/>
</mongo:mongo>
<!-- 设置使用的数据库 名-->
<mongo:db-factory dbname="info" mongo-ref="mongo"/>
<!-- mongodb的模板 -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
</beans>
2、在pom.xml中写入依赖
<!--spring-mongodb begin-->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.13.0-rc0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-cross-store</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-log4j</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<!--spring-mongodb end-->
3、写一个测试单元测试
MongoBase接口类
测试的实体类User
TestDao类
TestDaoimp
单元测试类
注意项:
1.jdk的版本要与spring和Mongodb能兼容,sping的版本要能和MongoDB的版本问兼容
2.要配一个UTF-8的编译形式