使用Morphia访问MongoDB数据库

安装

要在ActFramework应用中使用Morphia,请在pom.xml文件中添加以下依赖:

  1. <dependency>
  2. <groupId>org.actframework</groupId>
  3. <artifactId>act-morphia</artifactId>
  4. <version>0.1.1-SNAPSHOT</version>
  5. </dependency>

配置

简单的配置:

  1. db.uri=mongodb://localhost/mydb

小贴士 你甚至不需要任何配置. ActFramework会自动连接到本地MongoDB服务器的test数据库

稍微复杂一点的配置:

  1. db.url=mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,hostN:portN/dbname?replicaSet=...&connectTimeoutMS=...

域模型

下面创建一个简单的域模型,该模型有两个字段:

  1. name
  2. price
  1. package com.mycom.myprj;
  2. import org.mongodb.morphia.annotations.Entity;
  3. import act.db.morphia.MorphiaModel;
  4. @Entity("prod")
  5. public class Product extends MorphiaModel<Product> {
  6. private String name;
  7. private int price;
  8. public String getName() {
  9. return name;
  10. }
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14. public int getPrice() {
  15. return price;
  16. }
  17. public void setPrice(int price) {
  18. this.price = price;
  19. }
  20. }

数据访问对象和CRUD

以下代码演示如何使用MorphiaDao来进行CRUD操作:

  1. package com.mycom.myprj;
  2. import act.controller.Controller;
  3. import act.db.morphia.MorphiaDao;
  4. import org.bson.types.ObjectId;
  5. import org.osgl.mvc.annotation.GetAction;
  6. import org.osgl.mvc.annotation.PostAction;
  7. import org.osgl.mvc.annotation.PutAction;
  8. @Controller("/prod")
  9. public class ProductController extends Controller.Util {
  10. private MorphiaDao<Product> dao = Product.dao();
  11. @GetAction
  12. public Iterable<Product> list() {
  13. return dao.findAll();
  14. }
  15. @GetAction("/{id}")
  16. public Product show(String id) {
  17. return dao.findById(new ObjectId(id));
  18. }
  19. @PostAction
  20. public void create(Product product) {
  21. dao.save(product);
  22. }
  23. @PutAction("/{id}/name")
  24. public void update(String id, String name) {
  25. Product product = dao.findById(new ObjectId(id));
  26. notFoundIfNull(product);
  27. product.setName(name);
  28. dao.save(product);
  29. }
  30. @DeleteAction
  31. public void delete(String id) {
  32. dao.deleteById(new ObjectId(id));
  33. }
  34. }

查询操作

  1. // find by name
  2. Iterable<Product> products = dao.findBy("name", name);
  3. // find all that price is less than 10000
  4. Iterable<Product> products = dao.findBy("price <", 100000);
  5. // find by name and price
  6. Iterable<Product> products = dao.findBy("name, price <", name, 100000);
  7. // find by name using regular expression
  8. Iterable<Product> products = dao.findBy("name", Pattern.compile("laptop"));

使用扩展的DAO类

你可以根据需要扩展MorphiaDao类,并加入业务逻辑

  1. @Entity("prod")
  2. public class Product extends MorphiaModel<Product> {
  3. private String name;
  4. private int price;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public int getPrice() {
  12. return price;
  13. }
  14. public void setPrice(int price) {
  15. this.price = price;
  16. }
  17. public static class Dao extends MorphiaDao<Product> {
  18. public static final int LOW_PRICE = 10000;
  19. public static final int HIGH_PRICE = 999900;
  20. public Dao() {
  21. super(Product.class);
  22. }
  23. public Iterable findLowPriceProducts() {
  24. return findBy("price <", LOW_PRICE);
  25. }
  26. public Iterable findHighPriceProducts() {
  27. return findBy("price >", HIGH_PRICE);
  28. }
  29. }
  30. }

使用扩展的DAO类

假如你定义了扩展的DAO,你可以使用同样的接口来获取其实例:

  1. //private MorphiaDao<Product> dao = Product.dao();
  2. private Product.Dao dao = Product.dao();

返回目录