Play Framework 1.4 學習筆記 Model類型,Hibernate持久化,文件上傳

Play的Model部分會自動生成getter和setter

當定義這樣的類時

public class Product {
	public String name;
	public Integer price;
}

加載的類將是

public class Product {
 
    public String name;
    public Integer price;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getPrice() {
        return price;
    }
 
    public void setPrice(Integer price) {
        this.price = price;
    }
}

然後如果想要訪問屬性時:

product.name = "My product";
product.price = 58;

加載時會轉換爲

product.setName("My product");
product.setPrice(58);

如果有需要,可以自定義getter和setter方法

使用Hibernate持久化對象模型

  • 只需要加一個註解@Entity
  • Play提供了支持來處理JPA,需要繼承play.db.jpa.Model

事務管理

  • 如果需要強制回滾,調用JPA.setRollbackOnly()
  • 在Controller中的方法加@Transctional(readOnly=true)表示只讀
  • 如果不想開啓事務,可以使用@NoTransaction,也可以加在類上
  • 使用@NoTransaction時,不會從連接池獲取連接,從而提高速度

play.db.jpa.Model類

  • 繼承該類會自動生成一個Long類型的id並作爲主鍵

GenericMode類

  • 如果不想用Model的id,可以繼承此類,自定義id類型
@Entity
public class User extends GenericModel {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    public String id;

    @Required
    public String name;

    @Required
    @MaxSize(value=255, message = "email.maxsize")
    @play.data.validation.Email
    public String mail;
}

Model提供的查詢方法

  • 通過id查找
Post aPost = Post.findById(5L);
  • 查詢所有,分頁查詢
List<Post> posts = Post.findAll();
// 等同於
List<Post> posts = Post.all().fetch();
// 進行分頁:表示從第50條數據開始查詢100條
List<Post> posts = Post.all().from(50).fetch(100);
  • 條件查詢
Post.find("byTitle", "My first post").fetch();
Post.find("byTitleLike", "%hello%").fetch();
Post.find("byAuthorIsNull").fetch();
Post.find("byTitleLikeAndAuthor", "%hello%", connectedUser).fetch();
  • 條件查詢遵循以下語法
    LessThan -小於給定值
    LessThanEquals -小於或等於給定值
    GreaterThan -大於給定值
    GreaterThanEquals -大於或等於給定值
    Like -等同於類似SQL的表達式,但該屬性將始終轉換爲小寫。
    Ilike -與Like相似,但不區分大小寫,這意味着您的參數也將轉換爲小寫。
    Elike -等同於類似SQL的表達式,無需轉換。
    NotEqual -不相等
    Between -兩個值之間(需要兩個參數)
    IsNotNull -不是空值(不需要參數)
    IsNull -爲空值(不需要參數)
  • 使用JPQL查詢
Post.find(
    "select p from Post p, Comment c " +
    "where c.post = p and c.subject like ?", "%hop%"
);
// 可以簡寫
Post.find("title", "My first post").fetch();
Post.find("title like ?", "%hello%").fetch();
Post.find("author is null").fetch();
Post.find("title like ? and author is null", "%hello%").fetch();
Post.find("title like ? and author is null order by postDate", "%hello%").fetch();

Count計數

long postCount = Post.count();
long userPostCount = Post.count("author = ?", connectedUser);

文件上傳

  • 使用Blob類存儲,使用randerBinary()進行顯示
public class User extends Model {
 
   public String name;
   public Blob photo;
}
----------------------------------------------
#{form @addUser(), enctype:'multipart/form-data'}
   <input type="file" name="user.photo">
   <input type="submit" name="submit" value="Upload">
#{/form}
----------------------------------------------
public static void addUser(User user) {
   user.save();
   index();
}
----------------------------------------------
public static void file() {
   User user = User.find("name", "a").first();
   renderBinary(user.photo.get());
}

詳細內容參考官方文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章