现在的位置: 首页 > 综合 > 正文

play framework学习笔记之模型域model

2013年10月09日 ⁄ 综合 ⁄ 共 1010字 ⁄ 字号 评论关闭

在models包下

例子代码

 

@Entity

public class Post extends Model {


public String title;



public String content;



public Date postDate;



@ManyToOne


public Author author;



@OneToMany


public List<Comment> comments;

 

 

//一些原来的service层的方法,play framework采用的是模型域充血模型

}

 

可见所有的filed都是public的没有提供get,set方法,一切都只是为了开发的速度,和代码的简洁。

extends Model我们将不用写id,将会继承得到它。并且Model提供了很多很方便的方法。

我们经常会用到的是它的 save() and find().方法,记住他们都是Model的static 方法,static方法在play framework中随处可见

我们可以这么用 find("byTitleAndContent", title, content).first();  返回我们想要的那个Post实体

我们再来看一个更加复杂,但是很有用的方法

为了检索tags多个tag标签的文章,取的是并集。

 

find(
"select distinct p from Post p join p.tags as t where t.name in (:tags) group by p.id, p.author, p.title, p.content,p.postedAt having count(t.id) = :size"
).bind("tags", tags).bind("size", tags.length).fetch();

 

Note that we can’t use the Post.find(“…”, tags, tags.count) signature here. It’s just becausetags is already a vararg.


Model类全是静态方法,而且简单直接,我们应该仔细研究它,它值得如此,如果我们希望快速开发web。

 

the tag cloud

 

List<Map> result = Tag.find(
"select new map(t.name as tag, count(p.id) as pound) from Post p join p.tags as t group by t.name order by t.name"
).fetch();



抱歉!评论已关闭.