[size=medium]一、 运行环境:
MongoDB 1.4或更高版本
JDK1.5或更高版本
Sping3X或更高版本
二 、MAVEN配置
view sourceprint?1 <dependencies>
2 <!-- other dependency elements omitted -->
3 <dependency>
4 <groupId>org.springframework.data</groupId>
5 <artifactId>spring-data-mongodb</artifactId>
6 <version>1.1.0.RELEASE</version>
7 </dependency>
8 </dependencies>
三、spring 配置
view sourceprint?01 <!--指定配置文件地址-->
02 <context:property-placeholder location="classpath:/mongo.properties"/>
03 <mongo:mongo id="mongo" host="${${mongo.host}}" port="${${mongo.port}}">
04 <mongo:options
05 connections-per-host="${${mongo.connectionsPerHost}}"
06 threads-allowed-to-block-for-connection-multiplier="${${mongo.threadsAllowedToBlockForConnectionMultiplier}}"
07 connect-timeout="${${mongo.connectTimeout}}"
08 max-wait-time="${${mongo.maxWaitTime}}"
09 auto-connect-retry="${${mongo.autoConnectRetry}}"
10 socket-keep-alive="${${mongo.socketKeepAlive}}"
11 socket-timeout="${${mongo.socketTimeout}}"
12 slave-ok="${${mongo.slaveOk}}"
13 write-number="1"
14 write-timeout="0"
15 write-fsync="true"
16 />
17 </mongo:mongo>
18 <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
19 <description>认证口令</description>
20 <constructor-arg name="username" value="${${mongo.username}}"/>
21 <constructor-arg name="password" value="${${mongo.password}}"/>
22 </bean>
23 <bean name="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
24 <constructor-arg name="mongo" ref="mongo"/>
25 <constructor-arg name="userCredentials" ref="userCredentials"/>
26 <constructor-arg name="databaseName" value="nq-s2s"/>
27 </bean>
四、properties配置文件
view sourceprint?01 mongo.host=192.168.0.251
02 mongo.port=27017
03 mongo.connectionsPerHost=8
04 mongo.threadsAllowedToBlockForConnectionMultiplier=4
05 mongo.connectTimeout=1000
06 mongo.maxWaitTime=1500
07 mongo.autoConnectRetry=true
08 mongo.socketKeepAlive=true
09 mongo.socketTimeout=1500
10 mongo.slaveOk=true
11 mongo.username=***
12 mongo.password=***
13 mongo.dbname=***
五、JAVABEAN映射
AdCenter.java是一个标准的javaBean,采用注解的方式实现ORM映射。@Document、@Id是必要的两个注解,@Document映射Mongodb中的Collection,@Id映射指定Collection中的标识_id。
其他注解标签有:@Indexed索引、@Transient不映射此属性
view sourceprint?01 import org.springframework.data.annotation.Id;
02 import org.springframework.data.mongodb.core.mapping.Document;
03 @Document(collection = "adCenterDeduction")
04 public class AdCenter {
05 @Id
06 private String id;
07 private String business;
08 private String adCenter;
09 private int deductNum;
10 private int requestNum;
11 public String getBusiness() {
12 return business;
13 }
14 public void setBusiness(String business) {
15 this.business = business;
16 }
17 public String getAdCenter() {
18 return adCenter;
19 }
20 public void setAdCenter(String adCenter) {
21 this.adCenter = adCenter;
22 }
23 public int getDeductNum() {
24 return deductNum;
25 }
26 public void setDeductNum(int deductNum) {
27 this.deductNum = deductNum;
28 }
29 public int getRequestNum() {
30 return requestNum;
31 }
32 public void setRequestNum(int requestNum) {
33 this.requestNum = requestNum;
34 }
35 public String getId() {
36 return id;
37 }
38 public void setId(String id) {
39 this.id = id;
40 }
41 public AdCenter() {
42 }
43 public AdCenter(String business, String adCenter, int deductNum, int requestNum) {
44 this.business = business;
45 this.adCenter = adCenter;
46 this.deductNum = deductNum;
47 this.requestNum = requestNum;
48 }
49 @Override
50 public String toString() {
51 return "AdCenter [id=" + id + "business=" + business + ", adCenter=" + adCenter
52 + ", deductNum=" + deductNum + ", requestNum=" + requestNum
53 + "]";
54 }
55 }
六、测试
view sourceprint?01 @Before
02 public void setUp() throws Exception {
03 System.setProperty("NetQinServerType", "REL");
04 System.setProperty("NetQinServerLocation", "C");
05 }
06 @Test
07 public void testAdCenter() throws UnknownHostException {
08 ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml文件");
09 MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
10 AdCenter adCenter = new AdCenter("ms","test",99,98);
11 mongoOperation.insert(adCenter);
12 assert adCenter.getId()!=null;
13 System.out.println("newAdCenter:"+adCenter.toString());
14 Query query = new Query(Criteria.where("business").is(adCenter.getBusiness()));
15 query.addCriteria(Criteria.where("adCenter").is(adCenter.getAdCenter()));
16 //查找
17 AdCenter exsitAdCenter = mongoOperation.findOne(query,AdCenter.class);
18 assert exsitAdCenter!=null;
19 System.out.println("exsitAdCenter:"+exsitAdCenter.toString());
20 //修改
21 Update update = new Update().set("deductNum", 0);
22 AdCenter modifyAd = mongoOperation.findAndModify(query, update, AdCenter.class);
23 assert modifyAd.getDeductNum()==exsitAdCenter.getDeductNum();
24 System.out.println("beforeModifyAdCenter:"+exsitAdCenter.toString());
25 exsitAdCenter = mongoOperation.findOne(query,AdCenter.class);
26 assert exsitAdCenter!=null&&exsitAdCenter.getDeductNum()==0;
27 System.out.println("AfterModifyAdCenter:"+exsitAdCenter.toString());
28 //自增
29 Update incUpdate = new Update().inc("deductNum", 1);
30 mongoOperation.findAndModify(query, incUpdate, AdCenter.class);
31 exsitAdCenter = mongoOperation.findOne(query,AdCenter.class);
32 assert exsitAdCenter!=null&&exsitAdCenter.getDeductNum()==1;
33 System.out.println("AfterIncAdCenter:"+exsitAdCenter.toString());
34 //删除
35 mongoOperation.findAndRemove(query,AdCenter.class);
36 }
七、测试输出
view sourceprint?1 newAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=99, requestNum=98]
2 exsitAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=99, requestNum=98]
3 beforeModifyAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=99, requestNum=98]
4 AfterModifyAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=0, requestNum=98]
5 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 [/size]
MongoDB 1.4或更高版本
JDK1.5或更高版本
Sping3X或更高版本
二 、MAVEN配置
view sourceprint?1 <dependencies>
2 <!-- other dependency elements omitted -->
3 <dependency>
4 <groupId>org.springframework.data</groupId>
5 <artifactId>spring-data-mongodb</artifactId>
6 <version>1.1.0.RELEASE</version>
7 </dependency>
8 </dependencies>
三、spring 配置
view sourceprint?01 <!--指定配置文件地址-->
02 <context:property-placeholder location="classpath:/mongo.properties"/>
03 <mongo:mongo id="mongo" host="${${mongo.host}}" port="${${mongo.port}}">
04 <mongo:options
05 connections-per-host="${${mongo.connectionsPerHost}}"
06 threads-allowed-to-block-for-connection-multiplier="${${mongo.threadsAllowedToBlockForConnectionMultiplier}}"
07 connect-timeout="${${mongo.connectTimeout}}"
08 max-wait-time="${${mongo.maxWaitTime}}"
09 auto-connect-retry="${${mongo.autoConnectRetry}}"
10 socket-keep-alive="${${mongo.socketKeepAlive}}"
11 socket-timeout="${${mongo.socketTimeout}}"
12 slave-ok="${${mongo.slaveOk}}"
13 write-number="1"
14 write-timeout="0"
15 write-fsync="true"
16 />
17 </mongo:mongo>
18 <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
19 <description>认证口令</description>
20 <constructor-arg name="username" value="${${mongo.username}}"/>
21 <constructor-arg name="password" value="${${mongo.password}}"/>
22 </bean>
23 <bean name="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
24 <constructor-arg name="mongo" ref="mongo"/>
25 <constructor-arg name="userCredentials" ref="userCredentials"/>
26 <constructor-arg name="databaseName" value="nq-s2s"/>
27 </bean>
四、properties配置文件
view sourceprint?01 mongo.host=192.168.0.251
02 mongo.port=27017
03 mongo.connectionsPerHost=8
04 mongo.threadsAllowedToBlockForConnectionMultiplier=4
05 mongo.connectTimeout=1000
06 mongo.maxWaitTime=1500
07 mongo.autoConnectRetry=true
08 mongo.socketKeepAlive=true
09 mongo.socketTimeout=1500
10 mongo.slaveOk=true
11 mongo.username=***
12 mongo.password=***
13 mongo.dbname=***
五、JAVABEAN映射
AdCenter.java是一个标准的javaBean,采用注解的方式实现ORM映射。@Document、@Id是必要的两个注解,@Document映射Mongodb中的Collection,@Id映射指定Collection中的标识_id。
其他注解标签有:@Indexed索引、@Transient不映射此属性
view sourceprint?01 import org.springframework.data.annotation.Id;
02 import org.springframework.data.mongodb.core.mapping.Document;
03 @Document(collection = "adCenterDeduction")
04 public class AdCenter {
05 @Id
06 private String id;
07 private String business;
08 private String adCenter;
09 private int deductNum;
10 private int requestNum;
11 public String getBusiness() {
12 return business;
13 }
14 public void setBusiness(String business) {
15 this.business = business;
16 }
17 public String getAdCenter() {
18 return adCenter;
19 }
20 public void setAdCenter(String adCenter) {
21 this.adCenter = adCenter;
22 }
23 public int getDeductNum() {
24 return deductNum;
25 }
26 public void setDeductNum(int deductNum) {
27 this.deductNum = deductNum;
28 }
29 public int getRequestNum() {
30 return requestNum;
31 }
32 public void setRequestNum(int requestNum) {
33 this.requestNum = requestNum;
34 }
35 public String getId() {
36 return id;
37 }
38 public void setId(String id) {
39 this.id = id;
40 }
41 public AdCenter() {
42 }
43 public AdCenter(String business, String adCenter, int deductNum, int requestNum) {
44 this.business = business;
45 this.adCenter = adCenter;
46 this.deductNum = deductNum;
47 this.requestNum = requestNum;
48 }
49 @Override
50 public String toString() {
51 return "AdCenter [id=" + id + "business=" + business + ", adCenter=" + adCenter
52 + ", deductNum=" + deductNum + ", requestNum=" + requestNum
53 + "]";
54 }
55 }
六、测试
view sourceprint?01 @Before
02 public void setUp() throws Exception {
03 System.setProperty("NetQinServerType", "REL");
04 System.setProperty("NetQinServerLocation", "C");
05 }
06 @Test
07 public void testAdCenter() throws UnknownHostException {
08 ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml文件");
09 MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
10 AdCenter adCenter = new AdCenter("ms","test",99,98);
11 mongoOperation.insert(adCenter);
12 assert adCenter.getId()!=null;
13 System.out.println("newAdCenter:"+adCenter.toString());
14 Query query = new Query(Criteria.where("business").is(adCenter.getBusiness()));
15 query.addCriteria(Criteria.where("adCenter").is(adCenter.getAdCenter()));
16 //查找
17 AdCenter exsitAdCenter = mongoOperation.findOne(query,AdCenter.class);
18 assert exsitAdCenter!=null;
19 System.out.println("exsitAdCenter:"+exsitAdCenter.toString());
20 //修改
21 Update update = new Update().set("deductNum", 0);
22 AdCenter modifyAd = mongoOperation.findAndModify(query, update, AdCenter.class);
23 assert modifyAd.getDeductNum()==exsitAdCenter.getDeductNum();
24 System.out.println("beforeModifyAdCenter:"+exsitAdCenter.toString());
25 exsitAdCenter = mongoOperation.findOne(query,AdCenter.class);
26 assert exsitAdCenter!=null&&exsitAdCenter.getDeductNum()==0;
27 System.out.println("AfterModifyAdCenter:"+exsitAdCenter.toString());
28 //自增
29 Update incUpdate = new Update().inc("deductNum", 1);
30 mongoOperation.findAndModify(query, incUpdate, AdCenter.class);
31 exsitAdCenter = mongoOperation.findOne(query,AdCenter.class);
32 assert exsitAdCenter!=null&&exsitAdCenter.getDeductNum()==1;
33 System.out.println("AfterIncAdCenter:"+exsitAdCenter.toString());
34 //删除
35 mongoOperation.findAndRemove(query,AdCenter.class);
36 }
七、测试输出
view sourceprint?1 newAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=99, requestNum=98]
2 exsitAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=99, requestNum=98]
3 beforeModifyAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=99, requestNum=98]
4 AfterModifyAdCenter:AdCenter [id=51b4581bb2e9ac8f62684366business=ms, adCenter=test, deductNum=0, requestNum=98]
5 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 [/size]