play framework学习笔记之模型域model

在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();



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值