使用Ebean访问SQL Database

安装

在你的pom.xml文件中加上以下依赖:

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

根据你的数据库类型,你也需要加入相应的JDBC访问包的依赖。比如:

  1. <dependency>
  2. <groupId>com.h2database</groupId>
  3. <artifactId>h2</artifactId>
  4. <version>1.4.178</version>
  5. </dependency>

配置

  1. # If you have only one DBPlugin in your class path, then
  2. # you do not need to specify the db.impl configuration
  3. db.impl=act.db.ebean.EbeanPlugin
  4. # database driver default to org.h2.Driver
  5. db.driver=...
  6. # database Url default to jdbc:h2:mem:tests
  7. db.url=...
  8. # username default is empty
  9. db.username=...
  10. # password default is empty
  11. db.password=...
  12. # If specified then app scan package will be used instead
  13. db.db2.agentPackage=act.doc.sample.**

域模型

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

  1. firstName
  2. lastName
  3. address
  1. package com.mycom.myprj;
  2. import act.db.DB;
  3. import javax.persistence.Entity;
  4. import javax.persistence.Id;
  5. @Entity(name = "ctct")
  6. public class Contact {
  7. @Id
  8. private Long id;
  9. private String fn;
  10. private String ln;
  11. private String addr;
  12. public long getId() {
  13. return null == id ? -1 : id;
  14. }
  15. public String getFirstName() {
  16. return fn;
  17. }
  18. public void setFirstName(String fn) {
  19. this.fn = fn;
  20. }
  21. public String getLastName() {
  22. return ln;
  23. }
  24. public void setLastName(String ln) {
  25. this.ln = ln;
  26. }
  27. public String getAddress() {
  28. return addr;
  29. }
  30. public void setAddress(String addr) {
  31. this.addr = addr;
  32. }
  33. }

注意 和Morphia访问层不同,Ebean访问层目前暂时不提供类似MorphiaModel的父类.

数据访问对象和CRUD

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

  1. package com.mycom.myprj;
  2. import act.app.App;
  3. import act.controller.Controller;
  4. import act.db.ebean.EbeanDao;
  5. import org.osgl.$;
  6. import org.osgl.mvc.annotation.DeleteAction;
  7. import org.osgl.mvc.annotation.GetAction;
  8. import org.osgl.mvc.annotation.PostAction;
  9. import org.osgl.mvc.annotation.PutAction;
  10. import javax.inject.Inject;
  11. @Controller("/ctct")
  12. public class ContactController extends Controller.Util {
  13. private EbeanDao<Long, Contact> dao;
  14. @Inject
  15. public ContactController(EbeanDao<Long, Contact> dao) {
  16. this.dao = dao;
  17. }
  18. @GetAction
  19. public Iterable<Contact> list() {
  20. return dao.findAll();
  21. }
  22. @PostAction
  23. public void create(Contact ctct) {
  24. dao.save(ctct);
  25. }
  26. @GetAction("/{id}")
  27. public Contact show(long id) {
  28. return dao.findById(id);
  29. }
  30. @PutAction("/{id}/addr")
  31. public void updateAddress(long id, String value) {
  32. Contact ctct = dao.findById(id);
  33. notFoundIfNull(ctct);
  34. ctct.setAddress(value);
  35. dao.save(ctct);
  36. }
  37. @DeleteAction
  38. public void delete(long id) {
  39. dao.deleteById(id);
  40. }
  41. }

查询操作

  1. // find by last name
  2. Iterable<Contact> contacts = dao.findBy("firstName", firstName);
  3. // find by both first and last name
  4. Iterable<Product> contacts = dao.findBy("firstName,lastName", firstName, lastName);
  5. // find by firstName using regular expression
  6. Iterable<Product> contacts = dao.findBy("firstName", Pattern.compile(firstName));

扩展EbeanDao

TBD

使用扩展的DAO类

假如你定义了扩展的DAO,你可以直接使用依赖注入来获取其实例:

  1. //private EbeanDao<Contact> dao = $.cast(app.dbServiceManager().dao(Contact.class));
  2. private Contact.Dao dao = $.cast(app.dbServiceManager().dao(Contact.class));

返回目录