像我这样优雅地进行Spring整合MongoDB

本文重点是要将 mongodb 与 spring 整合到项目中去,在实践中发现问题,追踪问题,然后解决问题。

一、准备

  • Maven、Spring(spring-data-mongodb)
  • spring Data for MongoDB 是 Spring Data 的一个子模块。 目标是为 mongodb 提供一个相近的一致的基于 Spring 的编程模型。
  • Spring Data for MongoDB 核心功能是映射 POJO 到 Mongo 的 DBCollection 中的文档,并且提供 Repository 风格数据访问层。

 

二、特性

  • MongoDB 的提供了一个面向文档存储,操作起来比较简单和容易。
  • 你可以在 MongoDB 记录中设置任何属性的索引 (如:FirstName="Ning",Address="Beijing") 来实现更快的排序。
  • 你可以通过本地或者网络创建数据镜像,这使得 MongoDB 有更强的扩展性。
  • 如果负载的增加(需要更多的存储空间和更强的处理能力) ,它可以分布在计算机网络中的其他节点上这就是所谓的分片。
  • Mongo 支持丰富的查询表达式。查询指令使用 JSON 形式的标记,可轻易查询文档中内嵌的对象及数组。
  • MongoDb 使用 update() 命令可以实现替换完成的文档(数据)或者一些指定的数据字段 。
  • Mongodb 中的 Map/reduce 主要是用来对数据进行批量处理和聚合操作。
  • Map 和 Reduce。Map 函数调用 emit(key,value) 遍历集合中所有的记录,将 key 与 value 传给 Reduce 函数进行处理。
  • Map 函数和 Reduce 函数是使用 Javascript 编写的,并可以通过 db.runCommand 或 mapreduce 命令来执行 MapReduce 操作。
  • GridFS 是 MongoDB 中的一个内置功能,可以用于存放大量小文件。
  • MongoDB 允许在服务端执行脚本,可以用 Javascript 编写某个函数,直接在服务端执行,也可以把函数的定义存储在服务端,下次直接调用即可。
  • MongoDB 支持各种编程语言:RUBY,PYTHON,JAVA,C++,PHP,C# 等多种语言。

三、依赖包

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.5.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.5.10</version>
</dependency>

spring 相关依赖

<!-- spring web 相关依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <!-- spring test 依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.1.RELEASE</version>
        </dependency>

 

四、集成 MongoDB

【注:MongoDB 添加权限管理请参见我的这篇文章:MongDB 开启权限认证

mongodb.properties

mongo.hostport=172.16.4.166:27017
mongo.dbname=ad_api_count
mongo.username=hehaitao
mongo.password=hehaitao
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#\u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4
mongo.connectTimeout=1000
#\u7B49\u5F85\u65F6\u95F4
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
#Socket\u8D85\u65F6\u65F6\u95F4
mongo.socketTimeout=1500
mongo.slaveOk=true

mongoDB.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: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.1.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.1.xsd">
    <!-- 加载 mongodb 的属性配置文件 -->
    <context:property-placeholder location="classpath:mongodb.properties" ignore-unresolvable="true"/>
<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">mongo:mongo </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="mongo"</span><span style="color: rgba(255, 0, 0, 1)"> replica-set</span><span style="color: rgba(0, 0, 255, 1)">="${mongo.hostport}"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">mongo:options
         </span><span style="color: rgba(255, 0, 0, 1)">connections-per-host</span><span style="color: rgba(0, 0, 255, 1)">="${mongo.connectionsPerHost}"</span><span style="color: rgba(255, 0, 0, 1)">
         threads-allowed-to-block-for-connection-multiplier</span><span style="color: rgba(0, 0, 255, 1)">="${mongo.threadsAllowedToBlockForConnectionMultiplier}"</span><span style="color: rgba(255, 0, 0, 1)">
         connect-timeout</span><span style="color: rgba(0, 0, 255, 1)">="${mongo.connectTimeout}"</span><span style="color: rgba(255, 0, 0, 1)">
         max-wait-time</span><span style="color: rgba(0, 0, 255, 1)">="${mongo.maxWaitTime}"</span><span style="color: rgba(255, 0, 0, 1)">
         auto-connect-retry</span><span style="color: rgba(0, 0, 255, 1)">="${mongo.autoConnectRetry}"</span><span style="color: rgba(255, 0, 0, 1)">
         socket-keep-alive</span><span style="color: rgba(0, 0, 255, 1)">="${mongo.socketKeepAlive}"</span><span style="color: rgba(255, 0, 0, 1)">
         socket-timeout</span><span style="color: rgba(0, 0, 255, 1)">="${mongo.socketTimeout}"</span><span style="color: rgba(255, 0, 0, 1)">
         slave-ok</span><span style="color: rgba(0, 0, 255, 1)">="${mongo.slaveOk}"</span><span style="color: rgba(255, 0, 0, 1)">
         write-number</span><span style="color: rgba(0, 0, 255, 1)">="1"</span><span style="color: rgba(255, 0, 0, 1)">
         write-timeout</span><span style="color: rgba(0, 0, 255, 1)">="0"</span><span style="color: rgba(255, 0, 0, 1)">
         write-fsync</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>        
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">mongo:mongo</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">mongo:db-factory </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="mgFactory"</span><span style="color: rgba(255, 0, 0, 1)"> 
    dbname</span><span style="color: rgba(0, 0, 255, 1)">="${mongo.dbname}"</span><span style="color: rgba(255, 0, 0, 1)">  
    username</span><span style="color: rgba(0, 0, 255, 1)">="${mongo.username}"</span><span style="color: rgba(255, 0, 0, 1)"> 
    password</span><span style="color: rgba(0, 0, 255, 1)">="${mongo.password}"</span><span style="color: rgba(255, 0, 0, 1)">
     mongo-ref</span><span style="color: rgba(0, 0, 255, 1)">="mongo"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">bean </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="mongoTemplate"</span><span style="color: rgba(255, 0, 0, 1)"> class</span><span style="color: rgba(0, 0, 255, 1)">="org.springframework.data.mongodb.core.MongoTemplate"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">constructor-arg </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="mongoDbFactory"</span><span style="color: rgba(255, 0, 0, 1)"> ref</span><span style="color: rgba(0, 0, 255, 1)">="mgFactory"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">bean</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

</beans>

spring-contex.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"
    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-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">aop:aspectj-autoproxy </span><span style="color: rgba(255, 0, 0, 1)">proxy-target-class</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">使用注解管理bean  </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">context:annotation-config</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 扫描com.lutongnet下的所有类 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">context:component-scan </span><span style="color: rgba(255, 0, 0, 1)">base-package</span><span style="color: rgba(0, 0, 255, 1)">="com.lutong.cps"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">context:exclude-filter </span><span style="color: rgba(255, 0, 0, 1)">type </span><span style="color: rgba(0, 0, 255, 1)">= "annotation"</span><span style="color: rgba(255, 0, 0, 1)"> expression </span><span style="color: rgba(0, 0, 255, 1)">= "org.springframework.stereotype.Controller"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">context:component-scan</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<import resource="mongoDB.xml"/>
</beans>

 

五、代码实现

基础实现 MongoDBService

/**
 * File Name   : MongoDBService.java
 * Package     : com.lutongnet.ad.service
 * Description : TODO
 * Author      : zhangfj
 * Date        : 2012-11-29
 * Version     : V1.0 
 */
package com.lutong.cps.schedule.service.fj;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
/**

  • @author zhangfj

/
@Service(
"mongoDBService")
public class MongoDBService
{
/
@Resource(name = "mongoTemplate")
protected MongoTemplate mongoTemplate;
*/

<span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> query 
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> entityClass 
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> T 
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> &lt;T&gt; T findOne(Query query, Class&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)"> entityClass)
{
    ApplicationContext context</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> ClassPathXmlApplicationContext("mongoDB.xml"<span style="color: rgba(0, 0, 0, 1)">);
    MongoTemplate mongoTemplate</span>= (MongoTemplate) context.getBean("mongoTemplate"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 可以直接调用</span>
    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> mongoTemplate.findOne(query, entityClass);
}

}

继承类 UserAdCountService 

/**
 * File Name   : UserAdCountService.java
 * Package     : com.lutongnet.ad.service
 * Description : TODO
 * Author      : zhangfj
 * Date        : 2012-11-29
 * Version     : V1.0 
 */
package com.lutong.cps.schedule.service.fj;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.lutong.cps.schedule.entity.UserAdCount;
/**

  • @author zhangfj

/
@Service(
"userAdCountService")
public class UserAdCountService extends MongoDBService
{
/**
* 获取单个广告的观看次数,查询不到则返回 0
*
*
@param adCode
*
@return int
/
public int getUserAdCount(UserAdCount adCode)
{
Criteria criteria
= new Criteria();
criteria.andOperator(Criteria.where(
"userAd").is(adCode.getUserAd()),
Criteria.where(
"adCode").is(adCode.getAdCode()),
Criteria.where(
"countDate").is(adCode.getCountDate()));
Query query
= new Query(criteria);
UserAdCount result
= findOne(query, UserAdCount.class);
if (null != result)
{
return result.getTimesCount();
}
return 0;
}
}

实体类 UserAdCount

package com.lutong.cps.schedule.entity;

import java.util.Date;

import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.mapping.Document;

/**

  • mongo 专用统计单个用户的单个广告观看次数
  • @author cancer

*/
@Document(collection
="userAdCount")
public class UserAdCount
{
private int timesCount;

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 用户账号
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String userAd;

</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String adCode;

</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String countDate;

</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Date expireAt;

@PersistenceConstructor
public UserAdCount(int timesCount, String userAd,String adCode,String countDate,Date expireAt)
{
this.timesCount = timesCount;
this.userAd = userAd;
this.adCode = adCode;
this.countDate = countDate;
this.expireAt = expireAt;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> UserAdCount(String userAd,String adCode,String countDate)
{
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.userAd =<span style="color: rgba(0, 0, 0, 1)"> userAd;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.adCode =<span style="color: rgba(0, 0, 0, 1)"> adCode;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.countDate =<span style="color: rgba(0, 0, 0, 1)"> countDate;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> UserAdCount(String userAd,String adCode,String countDate,Date expireAt)
{
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.userAd =<span style="color: rgba(0, 0, 0, 1)"> userAd;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.adCode =<span style="color: rgba(0, 0, 0, 1)"> adCode;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.countDate =<span style="color: rgba(0, 0, 0, 1)"> countDate;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.expireAt =<span style="color: rgba(0, 0, 0, 1)"> expireAt;
}

public UserAdCount(String countDate)
{
this.countDate = countDate;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getTimesCount()
{
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> timesCount;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setTimesCount(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> timesCount)
{
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.timesCount =<span style="color: rgba(0, 0, 0, 1)"> timesCount;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getUserAd()
{
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> userAd;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setUserAd(String userAd)
{
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.userAd =<span style="color: rgba(0, 0, 0, 1)"> userAd;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getAdCode()
{
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> adCode;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setAdCode(String adCode)
{
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.adCode =<span style="color: rgba(0, 0, 0, 1)"> adCode;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getCountDate() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> countDate;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setCountDate(String countDate) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.countDate =<span style="color: rgba(0, 0, 0, 1)"> countDate;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Date getExpireAt() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> expireAt;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setExpireAt(Date expireAt) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.expireAt =<span style="color: rgba(0, 0, 0, 1)"> expireAt;
}

}

最后写一个测试类来测试下

import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.acts.web.modules.mark.model.Users;
import com.lutong.cps.schedule.service.fj.UserAdCountService;
@ContextConfiguration({ "classpath:spring-context.xml",
        "classpath:mongoDB.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class UserTest {
   @Resource(name = "userAdCountService")
    private UserAdCountService userAdCountService;
    @Test
    public void testDao() {
        try {
            UserAdCount userAdCount = new UserAdCount("hehaitao", "pos001",
                            DateTime.now().toString("yyyy-MM-dd"));
                int count = userAdCountService
                            .getUserAdCount(userAdCount);
                System.out.println(count);
        } catch (Exception e) {e.printStackTrace();
        }
    }
}