mongoDB http://www.mongodb.org/
Morphia https://github.com/mongodb/morphia
DZone http://architects.dzone.com/articles/using-morphia-map-java-objects
MongoDB with Querydsl http://blog.mysema.com/2010/11/mongodb-with-querydsl.html
Querydsl http://www.querydsl.com/
Morphia is a lightweight type-safe library for mapping Java objects to/from MongoDB. Morphia provides a typesafe, and fluent Query API support with (runtime) validation. Morphia uses annotations so there are no XML files to manage or update. Morphia should feel very comfortable for any developer with JPA experience.
- Lifecycle Method/Event Support
- Works great with Guice, Spring, and other DI frameworks.
- Many extension points (new annotations, converters, mapping behavior, logging, etc.)
- Does not store Null/Empty values (by default).
- GWT support (entities are just POJOs) -- (GWT ignores annotations)
- Advanced mapper which allows raw conversion,
void toObject(DBObject)
orDBObject fromObject(Object)
Please continue by reading the QuickStart or looking at a list of the annotations. If you have further questions, please reach out to us on our mailing list.
Quick start
Including morphia in your build
<dependency>
<groupId>org.mongodb.morphia</groupId>
<artifactId>morphia</artifactId>
<version>0.107</version>
</dependency>
See the dependencies page for more detail.
import com.mongodb.DBObject;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Key;
import org.mongodb.morphia.annotations.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity("employees")
public class Employee {
@Id
private ObjectId id;
// value types are automatically persisted
private String firstName;
private String lastName;
// only non-null values are stored
private Long salary = null;
// by default fields are @Embedded
private Address address;
//references can be saved without automatic loading
private Key<Employee> manager;
//refs are stored**, and loaded automatically
@Reference
private List<Employee> underlings = new ArrayList<Employee>();
// stored in one binary field
// @Serialized EncryptedReviews;
//fields can be renamed
@Property("started")
private Date startDate;
@Property("left")
private Date endDate;
//fields can be indexed for better performance
@Indexed
private boolean active = false;
//fields can loaded, but not saved
@NotSaved
private String readButNotStored;
//fields can be ignored (no load/save)
@Transient
private int notStored;
//not @Transient, will be ignored by Serialization/GWT for example.
private transient boolean stored = true;
//Lifecycle methods -- Pre/PostLoad, Pre/PostPersist...
@PostLoad
void postLoad(DBObject dbObj) {
}
public Employee() {
}
public Employee(String firstName, String lastName, Key<Employee> manager, long salary) {
this.firstName = firstName;
this.lastName = lastName;
this.manager = manager;
this.salary = salary;
}
// getter and setter
......
}
public class Address {
private String country;
private String city;
private String address;
private String postcode;
// getter and setter
......
}
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Key;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.query.UpdateResults;
import java.net.UnknownHostException;
public class EmployeeRepository {
private Morphia morphia;
private Datastore ds;
public static void main(String[] args) {
EmployeeRepository repository = new EmployeeRepository();
repository.init();
repository.test();
}
public void init() {
try {
Mongo mongo = new MongoClient(new ServerAddress("localhost", 27017));
morphia = new Morphia();
ds = morphia.createDatastore(mongo, "testdb");
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public void test() {
ds.save(new Employee("Mister", "GOD", null, 0L));
// get an employee without a manager
Employee boss = ds.find(Employee.class).field("manager").equal(null).get();
Key<Employee> scottsKey = ds.save(new Employee("Scott", "Hernandez", ds.getKey(boss), 150 * 1000));
//add Scott as an employee of his manager
UpdateResults<Employee> res = ds.update(boss, ds.createUpdateOperations(Employee.class).add("underlings", scottsKey));
// get Scott's boss; the same as the one above.
Employee scottsBoss = ds.find(Employee.class).filter("underlings", scottsKey).get();
for (Employee e : ds.find(Employee.class, "manager", boss))
System.out.println(e.getLastName() + " " + e.getFirstName());
}
}
Note: @Reference will not save objects, just a reference to them; You must save them yourself.
相关推荐
morphia.mapPackage("your.package.with.models"); // 替换为你的实体类所在的包 } public static Datastore getDatastore() { return morphia.createDatastore(mongoClient, "yourDatabaseName"); } } ``` ...
Play 应用程序中的身份验证和授权 该项目是具有身份验证和授权功能的多模块播放应用程序的示例实现。 这附有一篇。 Play 被选为开发此应用程序的主要框架,因为它具有惊人的生态系统、插件和其他好处,例如 Akka、...
**Spring整合MongoDB基于Maven** 在现代Java开发中,Spring框架与MongoDB数据库的集成是常见的选择,尤其是在处理非关系型数据时。MongoDB是一个高性能、无模式的文档型数据库,而Spring提供了强大的框架支持,使得...
基于springboot大学生就业信息管理系统源码数据库文档.zip
基于java的驾校收支管理可视化平台的开题报告
时间序列 原木 间隔5秒钟 20241120
毕业设计&课设_基于 Vue 的电影在线预订与管理系统:后台 Java(SSM)代码,为毕业设计项目.zip
基于springboot课件通中小学教学课件共享平台源码数据库文档.zip
基于java的网上购物商城的开题报告
Delphi人脸检测与识别Demo1fdef-main.zip
基于java的咖啡在线销售系统的开题报告
基于java的自助医疗服务系统的开题报告.docx
内容概要:本文档全面介绍了Visual Basic(VB)编程语言的基础知识和高级应用。首先概述了VB的基本特性和开发环境,随后详细讲述了VB的数据类型、变量、运算符、控制结构、数组、过程与函数、变量作用域等内容。接着介绍了窗体设计、控件使用、菜单与工具栏的设计,文件操作、数据库访问等关键知识点。最后讨论了VB的学习方法、发展历史及其在桌面应用、Web应用、数据库应用、游戏开发和自动化脚本编写等领域的广泛应用前景。 适合人群:初学者和中级程序员,尤其是希望快速掌握Windows桌面应用开发的人群。 使用场景及目标:①掌握VB的基础语法和开发环境;②学会使用VB创建复杂的用户界面和功能完整的应用程序;③理解数据库操作、文件管理和网络编程等高级主题。 其他说明:Visual Basic是一种简单易学且功能强大的编程语言,尤其适合用于开发Windows桌面应用。文中不仅覆盖了基础知识,还包括了大量的实用案例和技术细节,帮助读者快速提升编程技能。
基于java的疫情期间高校防控系统开题报告.docx
基于springboot+vue社区老年人帮扶系统源码数据库文档.zip
基于java的超市商品管理系统的开题报告.docx
基于SpringBoot房屋买卖平台源码数据库文档.zip
xdu限通院23微处理器系统与应用大作业(两只老虎),适应于汇编语言keil软件,
<项目介绍> - 新闻类网站系统,基于SSM(Spring、Spring MVC、MyBatis)+MySQL开发,高分成品毕业设计,附带往届论文 - 不懂运行,下载完可以私聊问,可远程教学 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 --------
基于java的学生网上请假系统的开题报告.docx