1、项目采用Maven 的方式。所需要的jar包只需要配置即可。
2、与Mongodb,spring整合相关的两个jar包:
<dependency>
<groupId>org.mongodbgroupId>
<artifactId>mongo-java-driverartifactId>
<version>${mongo-java-driver.version}version>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-mongodbartifactId>
<version>${spring-data-mongodb.version}version>
dependency>
4、pom.xml配置如下所示:
<dependencies>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jmsartifactId>
<version>${spring.version}version>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.apache.xbeangroupId>
<artifactId>xbean-springartifactId>
<version>${xbean-spring.version}version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>${httpclient.version}version>
dependency>
<dependency>
<groupId>org.mongodbgroupId>
<artifactId>mongo-java-driverartifactId>
<version>${mongo-java-driver.version}version>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-mongodbartifactId>
<version>${spring-data-mongodb.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
<scope>testscope>
dependency>
5、添加spring相关的配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:config.propertiesvalue>
<value>classpath:common.propertiesvalue>
list>
property>
bean>
<context:component-scan base-package="com.newsoft">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
context:component-scan>
beans>
6、添加Mongodb相关的配置文件。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
">
<mongo:mongo-client id="mongoClient" host="${mongo.host}" port="${mongo.port}">
<mongo:client-options
connections-per-host="${mongo.connectionsPerHost}"
min-connections-per-host="${mongo.minConnectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}"
max-wait-time="${mongo.maxWaitTime}"
socket-keep-alive="${mongo.socketKeepAlive}"
socket-timeout="${mongo.socketTimeout}"
description="${mongo.description}"
max-connection-idle-time="${mongo.maxConnectionIdleTime}"
max-connection-life-time="${mongo.maxConnectionLifeTime}"
heartbeat-socket-timeout="${mongo.heartbeatSocketTimeout}"
heartbeat-connect-timeout="${mongo.heartbeatConnectTimeout}"
min-heartbeat-frequency="${mongo.minHeartbeatFrequency}"
heartbeat-frequency="${mongo.heartbeatFrequency}"
/>
mongo:mongo-client>
<mongo:db-factory id="mongoDbFactory"
dbname="${mongo.defaultDbName}"
mongo-ref="mongoClient"/>
<mongo:template id="mongoTemplate" db-factory-ref="mongoDbFactory" write-concern="NORMAL"/>
<mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory"
base-package="cn.idongjia.web.dao"/>
<mongo:gridFsTemplate id="gridFsTemplcate" db-factory-ref="mongoDbFactory" converter-ref="converter" />
<mongo:repositories base-package="com.newsoft.model" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
beans>
#mongodb
mongo.host=127.0.0.1
mongo.port=27017
mongo.defaultDbName=log
mongo.user=liuc
mongo.pwd=199116
mongo.connectionsPerHost=100
mongo.threadsAllowedToBlockForConnectionMultiplier=5
mongo.minConnectionsPerHost=5
#连接超时时间
mongo.connectTimeout=10000
#等待时间
mongo.maxWaitTime=120000
#Socket超时时间
mongo.socketTimeout=0
mongo.socketKeepAlive=true
mongo.description= test mongodb database
mongo.maxConnectionIdleTime=1500
mongo.maxConnectionLifeTime=0
#mongo slave
mongo.heartbeatSocketTimeout=1000
mongo.heartbeatConnectTimeout=1500
mongo.minHeartbeatFrequency=5
mongo.heartbeatFrequency=10
本项目将Mongodb相关的增删查改操作进行了一层封装。只需传入所需参数调用即可。
public class ObjectDaoImpl<T> implements ObjectDao<T>{
@Autowired
protected MongoOperations mongoOperations;
@Override
public void save(T t) {
mongoOperations.insert(t);
}
@Override
public T saveReturnT( T t){
mongoOperations.insert(t);
//如果t对象中包含id主键,则插入数据后,返回对象t,包含自增的主键id内容
return t;
}
@Override
public int update(Query query, Update update,Class<T> tclass) {
WriteResult result = mongoOperations.updateFirst(query, update, tclass);
return result.getN();
}
@Override
public Pager page(Pager pager, Query query,Class<T> tclass) {
int skipCount = (pager.getPageNumber() > 0 ? pager.getPageNumber() - 1 : 0) * pager.getPageSize();
query.skip(skipCount);
query.limit(pager.getPageSize());
List list = mongoOperations.find(query, tclass);
pager.setResult(list);
pager.setTotalCount(list.size());
return pager;
}
@Override
public T get(Query query, Class<T> tclass) {
T t = mongoOperations.findOne(query,tclass);
return t;
}
@Override
public void remove(Query query, Class<T> tclass) {
mongoOperations.remove(query, tclass);
}
}
实体类中的注释详解:
@Id - 文档的唯一标识,在mongodb中为ObjectId,它是唯一的,通过时间戳+机器标识+进程ID+自增计数器(确保同一秒内产生的Id不会冲突)构成。
@Document - 把一个java类声明为mongodb的文档,可以通过collection参数指定这个类对应的文档。@Document(collection=”mongodb”) mongodb对应表
@DBRef - 声明类似于关系数据库的关联关系。ps:暂不支持级联的保存功能,当你在本实例中修改了DERef对象里面的值时,单独保存本实例并不能保存DERef引用的对象,它要另外保存,如下面例子的Person和Account。
@Indexed - 声明该字段需要索引,建索引可以大大的提高查询效率。
@CompoundIndex - 复合索引的声明,建复合索引可以有效地提高多字段的查询效率。
@GeoSpatialIndexed - 声明该字段为地理信息的索引。
@Transient - 映射忽略的字段,该字段不会保存到mongodb。
@PersistenceConstructor - 声明构造函数,作用是把从数据库取出的数据实例化为对象。该构造函数传入的值为从DBObject中取出的数据
具体详情可以下载本项目进行查看。http://download.csdn.net/download/u012151597/9999239