博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring data for mongo
阅读量:6589 次
发布时间:2019-06-24

本文共 4391 字,大约阅读时间需要 14 分钟。

hot3.png

一、 运行环境:

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

二 、MAVEN配置

org.springframework.data
spring-data-mongodb
1.1.0.RELEASE

三、spring 配置

认证口令

四、properties配置文件

mongo.host=192.168.0.251mongo.port=27017mongo.connectionsPerHost=8mongo.threadsAllowedToBlockForConnectionMultiplier=4mongo.connectTimeout=1000mongo.maxWaitTime=1500mongo.autoConnectRetry=truemongo.socketKeepAlive=truemongo.socketTimeout=1500mongo.slaveOk=truemongo.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文件"); 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]

八、参考文档

转载于:https://my.oschina.net/damihui/blog/173897

你可能感兴趣的文章
【Scala篇】--Scala初始与基础
查看>>
RecyclerView实例-实现可下拉刷新上拉加载更多并可切换线性流和瀑布流模式(1)...
查看>>
Java基础之Switch语句
查看>>
小泉的难题 结构体
查看>>
CentOS 7 安装Hadoop 2.7.1
查看>>
地图地位
查看>>
新iPad未到 老iPad价格反弹
查看>>
why add \n to http response.responseText
查看>>
Codeforces Round #316 (Div. 2B) 570B Simple Game 贪心
查看>>
RabbitMQ headers Exchange
查看>>
memset,memcpy,strcpy的使用与区别
查看>>
angukarjs的优势
查看>>
NewLIne 超过的字符 自动换行
查看>>
JVM 内存分析(转)
查看>>
求余数 http://acm.nyist.net/JudgeOnline/problem.php?pid=205
查看>>
LINUX服务器--所有用户登陆操作命令审计
查看>>
2012关闭ECN
查看>>
kylin构建cube优化
查看>>
团队管理中的代码评审
查看>>
利用html5canvas给图片增加文字水印
查看>>