1spring和mong集成需要的jar包
spring-data-commons-core-1.2.1.RELEASE.jar
spring-data-mongodb-1.0.4.RELEASE.jar
mongo-2.10.1.jar
2 web.xml配置,classpath*:会自动读取ClassPath路径下面所有文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>cms_service</display-name>
<!-- ContextParam Config -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
</web-app>
3 applicationContext-mongodb.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
</beans>
4 由于都是${}的形式,是因为applicationContext.xml中配置了
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:sysconfig.properties</value>
</property>
</bean>
该properties文件里面是
mongo.db.name=tycms_m
mongo.db.username=
mongo.db.password=
mongo.db.host=127.0.30.26:30000
#mongo.db.host=127.0.212.41:22000
mongo.connectionsPerHost=10
mongo.threadsAllowedToBlockForConnectionMultiplier=5
mongo.connectTimeout=10000
mongo.maxWaitTime=120000
mongo.autoConnectRetry=true
mongo.socketKeepAlive=false
mongo.socketTimeout=0
5 java操作mongo,使用如下
public class MongodbDao {
public MongoOperations mongoOperations;
public <T extends BaseDomain> void insert(T entity) {
Date date = new Date(); // 创建时间
entity.setCreateTime(date);
entity.setUpdateTime(date);
entity.setCreateTimeStr(DateUtils.dateToStrLong(date));
entity.setUpdateTimeStr(DateUtils.dateToStrLong(date));
mongoOperations.insert(entity);
}
.......
}