vivion32 发表于 2016-12-2 08:13:20

spring data for mongo

一、 运行环境:
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

05connections-per-host="${${mongo.connectionsPerHost}}"

06threads-allowed-to-block-for-connection-multiplier="${${mongo.threadsAllowedToBlockForConnectionMultiplier}}"

07connect-timeout="${${mongo.connectTimeout}}"

08max-wait-time="${${mongo.maxWaitTime}}"

09auto-connect-retry="${${mongo.autoConnectRetry}}"

10socket-keep-alive="${${mongo.socketKeepAlive}}"

11socket-timeout="${${mongo.socketTimeout}}"

12slave-ok="${${mongo.slaveOk}}"

13write-number="1"

14write-timeout="0"

15write-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

06private String id;

07private String business;

08private String adCenter;

09private int deductNum;

10private int requestNum;

11public String getBusiness() {

12return business;

13}

14public void setBusiness(String business) {

15this.business = business;

16}

17public String getAdCenter() {

18return adCenter;

19}

20public void setAdCenter(String adCenter) {

21this.adCenter = adCenter;

22}

23public int getDeductNum() {

24return deductNum;

25}

26public void setDeductNum(int deductNum) {

27this.deductNum = deductNum;

28}

29public int getRequestNum() {

30return requestNum;

31}

32public void setRequestNum(int requestNum) {

33this.requestNum = requestNum;

34}

35public String getId() {

36return id;

37}

38public void setId(String id) {

39this.id = id;

40}

41public AdCenter() {

42}

43public AdCenter(String business, String adCenter, int deductNum, int requestNum) {

44this.business = business;

45this.adCenter = adCenter;

46this.deductNum = deductNum;

47this.requestNum = requestNum;

48}

49@Override

50public String toString() {

51return "AdCenter [id=" + id + "business=" + business + ", adCenter=" + adCenter

52+ ", deductNum=" + deductNum + ", requestNum=" + requestNum

53+ "]";

54}

55 }

六、测试
view sourceprint?01 @Before

02public void setUp() throws Exception {

03System.setProperty("NetQinServerType", "REL");

04System.setProperty("NetQinServerLocation", "C");

05}

06@Test

07public void testAdCenter() throws UnknownHostException {

08ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml文件");

09MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

10AdCenter adCenter = new AdCenter("ms","test",99,98);

11mongoOperation.insert(adCenter);

12assert adCenter.getId()!=null;

13System.out.println("newAdCenter:"+adCenter.toString());

14Query query = new Query(Criteria.where("business").is(adCenter.getBusiness()));

15query.addCriteria(Criteria.where("adCenter").is(adCenter.getAdCenter()));

16//查找

17AdCenter exsitAdCenter = mongoOperation.findOne(query,AdCenter.class);

18assert exsitAdCenter!=null;

19System.out.println("exsitAdCenter:"+exsitAdCenter.toString());

20//修改

21Update update = new Update().set("deductNum", 0);

22AdCenter modifyAd = mongoOperation.findAndModify(query, update, AdCenter.class);

23assert modifyAd.getDeductNum()==exsitAdCenter.getDeductNum();

24System.out.println("beforeModifyAdCenter:"+exsitAdCenter.toString());

25exsitAdCenter = mongoOperation.findOne(query,AdCenter.class);

26assert exsitAdCenter!=null&&exsitAdCenter.getDeductNum()==0;

27System.out.println("AfterModifyAdCenter:"+exsitAdCenter.toString());

28//自增

29Update incUpdate = new Update().inc("deductNum", 1);

30mongoOperation.findAndModify(query, incUpdate, AdCenter.class);

31exsitAdCenter = mongoOperation.findOne(query,AdCenter.class);

32assert exsitAdCenter!=null&&exsitAdCenter.getDeductNum()==1;

33System.out.println("AfterIncAdCenter:"+exsitAdCenter.toString());

34//删除

35mongoOperation.findAndRemove(query,AdCenter.class);

36}

七、测试输出
view sourceprint?1 newAdCenter:AdCenter

2 exsitAdCenter:AdCenter

3 beforeModifyAdCenter:AdCenter

4 AfterModifyAdCenter:AdCenter

5 AfterIncAdCenter:AdCenter

八、参考文档
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
页: [1]
查看完整版本: spring data for mongo