spring data for mongo

一、 运行环境:

  1. MongoDB 1.4或更高版本
  2. JDK1.5或更高版本
  3. Sping3X或更高版本

二 、MAVEN配置


<dependencies> 
<!-- other dependency elements omitted -->
<dependency> 
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.1.0.RELEASE</version> 
</dependency> 
</dependencies>

三、spring 配置



<!--指定配置文件地址-->
 <context:property-placeholder location="classpath:/mongo.properties"/>
 <mongo:mongo id="mongo" host="${${mongo.host}}" port="${${mongo.port}}">
 <mongo:options
 connections-per-host="${${mongo.connectionsPerHost}}"
 threads-allowed-to-block-for-connection-multiplier="${${mongo.threadsAllowedToBlockForConnectionMultiplier}}"
 connect-timeout="${${mongo.connectTimeout}}"
 max-wait-time="${${mongo.maxWaitTime}}"
 auto-connect-retry="${${mongo.autoConnectRetry}}"
 socket-keep-alive="${${mongo.socketKeepAlive}}"
 socket-timeout="${${mongo.socketTimeout}}"
 slave-ok="${${mongo.slaveOk}}"
 write-number="1"
 write-timeout="0"
 write-fsync="true"
 />
 </mongo:mongo>
 <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
 <description>认证口令</description>
 <constructor-arg name="username" value="${${mongo.username}}"/>
 <constructor-arg name="password" value="${${mongo.password}}"/>
 </bean>
 <bean name="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
 <constructor-arg name="mongo" ref="mongo"/>
 <constructor-arg name="userCredentials" ref="userCredentials"/>
 <constructor-arg name="databaseName" value="nq-s2s"/>
 </bean>

四、properties配置文件



mongo.host=192.168.0.251
mongo.port=27017
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
mongo.connectTimeout=1000
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.socketTimeout=1500
mongo.slaveOk=true
mongo.username=***
mongo.password=***
mongo.dbname=***

五、JAVABEAN映射

AdCenter.java是一个标准的javaBean,采用注解的方式实现ORM映射。@Document、@Id是必要的两个注解,@Document映射Mongodb中的Collection,@Id映射指定Collection中的标识_id。

其他注解标签有:@Indexed索引、@Transient不映射此属性

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "adCenterDeduction")
public class AdCenter {
 @Id
 private String id;
 private String business;
 private String adCenter;
 private int deductNum;
 private int requestNum;
 public String getBusiness() {
 return business;
 }
 public void setBusiness(String business) {
 this.business = business;
 }
 public String getAdCenter() {
 return adCenter;
 }
 public void setAdCenter(String adCenter) {
 this.adCenter = adCenter;
 }
 public int getDeductNum() {
 return deductNum;
 }
 public void setDeductNum(int deductNum) {
 this.deductNum = deductNum;
 }
 public int getRequestNum() {
 return requestNum;
 }
 public void setRequestNum(int requestNum) {
 this.requestNum = requestNum;
 }
 public String getId() {
 return id;
 }
 public void setId(String id) {
 this.id = id;
 }
 public AdCenter() {
 }
 public AdCenter(String business, String adCenter, int deductNum, int requestNum) {
 this.business = business;
 this.adCenter = adCenter;
 this.deductNum = deductNum;
 this.requestNum = requestNum;
 }
 @Override
 public String toString() {
 return "AdCenter [id=" + id + "business=" + business + ", adCenter=" + adCenter
 + ", deductNum=" + deductNum + ", requestNum=" + requestNum
 + "]";
 }
}

六、测试

@Before
 public void setUp() throws Exception {
 System.setProperty("NetQinServerType", "REL");
 System.setProperty("NetQinServerLocation", "C");
 }
 @Test
 public void testAdCenter() throws UnknownHostException {
 ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml&#25991;&#20214;");
 MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
 AdCenter adCenter = new AdCenter("ms","test",99,98);
 mongoOperation.insert(adCenter);
 assert adCenter.getId()!=null;
 System.out.println("newAdCenter:"+adCenter.toString());
 Query query = new Query(Criteria.where("business").is(adCenter.getBusiness()));
 query.addCriteria(Criteria.where("adCenter").is(adCenter.getAdCenter()));
 //查找
 AdCenter exsitAdCenter = mongoOperation.findOne(query,AdCenter.class);
 assert exsitAdCenter!=null;
 System.out.println("exsitAdCenter:"+exsitAdCenter.toString());
 //修改
 Update update = new Update().set("deductNum", 0);
 AdCenter modifyAd = mongoOperation.findAndModify(query, update, AdCenter.class);
 assert modifyAd.getDeductNum()==exsitAdCenter.getDeductNum();
 System.out.println("beforeModifyAdCenter:"+exsitAdCenter.toString());
 exsitAdCenter = mongoOperation.findOne(query,AdCenter.class);
 assert exsitAdCenter!=null&&exsitAdCenter.getDeductNum()==0;
 System.out.println("AfterModifyAdCenter:"+exsitAdCenter.toString());
 //自增
 Update incUpdate = new Update().inc("deductNum", 1);
 mongoOperation.findAndModify(query, incUpdate, AdCenter.class);
 exsitAdCenter = mongoOperation.findOne(query,AdCenter.class);
 assert exsitAdCenter!=null&&exsitAdCenter.getDeductNum()==1;
 System.out.println("AfterIncAdCenter:"+exsitAdCenter.toString());
 //删除
 mongoOperation.findAndRemove(query,AdCenter.class);
 }

七、测试输出

newAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=99, requestNum=98]
exsitAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=99, requestNum=98]
beforeModifyAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=99, requestNum=98]
AfterModifyAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=0, requestNum=98]
AfterIncAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=1, requestNum=98]

八、参考文档

http://www.springsource.org/spring-data/mongodb
http://static.springsource.org/spring-data/mongodb/docs/current/reference/html/mongo.core.html#mongodb-connectors
http://static.springsource.org/spring-data/mongodb/docs/current/reference/html/
https://github.com/jreijn/spring-mongo-demo





你可能感兴趣的:(spring data for mongo)