SpringDataMongoDB介绍(二)-MongoOperations介绍
2024-10-16 18:24:04
MongoOperations是一个很强大的接口,有了这个接口,基本上什么都搞定了。
其介绍
Interface that specifies a basic set of MongoDB operations. Implemented by {@link MongoTemplate}. Not often used but a useful option for extensibility and testability (as it can be easily mocked, stubbed, or be the target of a JDK proxy
直接上代码
实体类
package com.chzhao.mongodbtest;
import java.util.Date;
import org.springframework.data.annotation.Id;
public class Person {
public Person(String name, int age, Date birth) {
this.name = name;
this.age = age;
this.birth = birth;
}
private Date birth;
@Id
private String id;
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private int age;
}
操作类
package com.chzhao.mongodbtest;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import com.mongodb.Mongo;
public class MongoApp {
private static final Log log = LogFactory.getLog(MongoApp.class);
public static void main(String[] args) throws Exception {
MongoOperations mongoOps = new MongoTemplate(new Mongo(), "zch");
mongoOps.dropCollection(Person.class);
mongoOps.remove(new Query(where("name").is("zhao")), Person.class);
DateTime zhaoBirth = new DateTime(1985, 12, 13, 18, 23, 55);
DateTime liangBirth = new DateTime(1986, 12, 13, 18, 23, 55);
mongoOps.insert(new Person("zhao", 34, zhaoBirth.toDate()));
mongoOps.insert(new Person("liang", 24, liangBirth.toDate()));
List<Person> pList = mongoOps.find(new Query(where("name").is("zhao")),
Person.class);
for (Person p : pList) {
log.info(p.getName() + p.getAge());
}
DateTime someday = new DateTime(1986, 1, 13, 18, 23, 55);
List<Person> pList1 = mongoOps.find(new Query(where("birth").lt(someday)), Person.class);
for (Person p : pList1) {
log.info(p.getName() + p.getAge());
}
}
}
这个Query类似lambda表达式,能做很多事情,很赞。
最新文章
- 阿里云mariadb无法启动问题
- NopCommerce功能与特点介绍
- IronPython 设置包路径
- (转)MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
- jquery的extend和fn.extend的使用说明
- Flask 安装 Ubuntu 14.04
- iWeb峰会见闻
- eclipse的SVN插件的配置
- The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))
- [2013-06-05]bat脚本设置DNS
- JVM 运行时数据区总结 栈 堆 堆大小配置总结
- 第4章 同步控制 Synchronization ----同步机制的摘要
- AndroidStudio运行java的main方法
- vue导出excel
- 七年一冠、IG牛13的背后是什么!
- Linux 循环创建多个线程
- jmeter运行时间越久发送请求越来越少
- Vue 项目优化,持续更新...
- FZU2125_简单的等式
- 自己定义View步骤
热门文章
- 面试题_93_to_102_编程和代码相关的面试题
- linux/unix网络编程之epoll
- BootStrap弹窗
- SQL Server:把CSV文件导入到SQL Server表中
- [swustoj 1023] Escape
- ios 编译openssl支持arm64(转)
- POJ 2942 Knights of the Round Table (点双连通分量)
- c &; c++中const
- 开发ffmpeg/live555常见问题错误及解决方法
- zoj 3659 Conquer a New Region