配置play环境把play的路径添加到系统环境变量的PATH路径中1.进入CMD环境,测试配置是否成功play创建一个简单项目,UserPostCommentTag2.这里我们用samples-and-tests下的yabe项目来做例子。3.在cmd中playnewyabe4.进入创建的目录运行playrun命令.在浏览器中输入查看创建的项目是否成功。5.使用playeclipsify表示把项目转换成一个ECLIPSE项目。6.输入playtest表示以测试模式启动。在浏览器中输入@tests表示进入测试JUNIT页面,并可进行测试7.创建实体BEAN,play的实体BEAN使用的是JPA的实体8.把项目导入eclipse中,在models包中创建类,内容如下:packagemodels;importjavax.persistence.Entity;importplay.db.jpa.Model;@EntitypublicclassUserextendsModel{publicStringemail;publicStringpassword;publicStringfullname;publicStringisAdmin;publicUser(Stringemail,Stringpassword,Stringfullname){this.email=email;this.password=password;this.fullname=fullname;}}关于实体中类的注解可以通过查看JPA2.0的相关文档来进行了解.注意一点,我在创建的实体类中并没有添加ID属性,但是其实ID属性是必须的属性,如果我们在实体类中没有显示的指定ID属性,PLAY会给我们创建一个默认的id属性,这个属性的值为自动增加值。集成JUNIT单元测试在test包目录下新建一个UserTest的测试类,继承UnitTest类,如下:importmodels.User;importorg.junit.Test;importplay.test.UnitTest;publicclassUserTestextendsUnitTest{@TestpublicvoidcreateAndRetrieveUser(){//添加Useruser=newUser(yh.sniaw@gmail.com,123456,小机);assertNotNull(user.save());//查询条件下的所有信息,并返回第一个Usersearch=user.find(byEmailLike,%gmail%).first();assertNotNull(search);assertEquals(123456,search.password);}}运行playtest在浏览器中输入@tests选择UserTest点击start按钮测试。在User实体中编写一个查询的方法,并在测试类中添加一个测试方法,不需要重启浏览器,直接进行测试。查看效果。publicstaticUserconnect(Stringemail,Stringpassword){returnUser.find(byEmailLikeAndPassword,%+email+%,password).first();}@TestpublicvoidtestConnectMethod(){//添加Useruser=newUser(yh.sniaw@gmail.com,123456,小机);assertNotNull(user.save());//查询assertNotNull(User.connect(gmail,123456));assertNull(User.connect(test,123456));assertNull(User.connect(yh.sniaw@gmail.com,aa));assertNotNull(User.connect(yh.sniaw@gmail.com,123456));}新建一个Post实体类,类的关系与User为多对一的关系。packagemodels;importjava.util.Date;importjavax.persistence.Entity;importjavax.persistence.Lob;importjavax.persistence.ManyToOne;importplay.db.jpa.Model;@EntitypublicclassPostextendsModel{publicStringtitle;publicDatepostedAt;@LobpublicStringcontent;@ManyToOnepublicUserauthor;publicPost(Userauthor,Stringtitle,Stringcontent){this.author=author;this.title=title;this.content=content;this.postedAt=newDate();}}创建一个测试方法:@TestpublicvoidcreatePost(){//添加Useruser=newUser(yh.sniaw@gmail.com,123456,小机);assertNotNull(user.save());Postpost=newPost(user,title,content);assertNotNull(post.save());assertEquals(1,Post.count());ListPostlist=Post.find(byAuthor,user).fetch();assertEquals(1,list.size());PostfirstPost=list.get(0);assertNotNull(firstPost);assertEquals(title,firstPost.title);assertEquals(content,firstPost.content);assertNotNull(firstPost.postedAt);assertEquals(user,firstPost.author);}查看测试结果。新建一个Comment实体类,实体类与Post为多对一的关系。packagemodels;importjava.util.Date;importjavax.persistence.Entity;importjavax.persistence.Lob;importjavax.persistence.ManyToOne;importplay.db.jpa.Model;@EntitypublicclassCommentextendsModel{publicStringauthor;@LobpublicStringcontent;publicDatepostedAt;@ManyToOnepublicPostpost;publicComment(Postpost,Stringauthor,Stringcontent){this.post=post;this.author=author;this.content=content;this.postedAt=newDate();}}新建一个测试Comment方法。@TestpublicvoidcreateComments(){Useruser=newUser(yh.sniaw@gmail.com,123456,小机);assertNotNull(user.save());Postpost=newPost(user,title,content);assertNotNull(post.save());newComment(post,author1,content1).save();newComment(post,author2,content2).save();ListCommentlist=Comment.find(byPost,post).fetch();assertEquals(2,list.size());CommentfirstComment=list.get(0);assertEquals(author1,firstComment.author);assertNotNull(firstComment.postedAt);}为Post添加一对多关系mappedBy表示找到Comment类中的post对象进行反转@OneToMany(mappedBy=post,cascade=CascadeType.ALL)SetCommentcomments;publicPost(Userauthor,Stringtitle,Stringcontent){comments=newLinkedHashSetComment(0);this.author=author;this.title=title;this.content=content;this.postedAt=newDate();}添加测试方法:@TestpublicvoidpostAddComments(){Useruser=newUser(yh.sniaw@gmail.com,123456,小机);assertNotNull(user.save());Postpost=newPost(user,title,content);assertNotNull(post.save());post.comments.add(newComment(post,author1,content1));post.comments.add(newComment(post,author2,content2));post.save();PostfirstPost=Post.find(byTitle,title).first();assertEquals(2,firstPost.comments.size());assertEquals(2,Comment.count());}编写视图显示1.添加在服务器启动时需要处理的事情。如加载一些基础数据:在models中添加一个Bootstarp类。此类继承Job类。packagemodels;importplay.jobs.Job;importplay.jobs.OnApplicationStart;importplay.test.Fixtures;@OnApplicationStartpublicclassBootstarpextendsJob{publicvoidloadUsers(){if(User.count()1){Fixtures.loadModels(init_user.yml);}}}在conf目录添加init_user.yml文件。文件内容可以从test包下的data.yml文件中复制出来。并做一些修改。代码格式每一个对象用空行表示结束。值中–表示引用一个对象,-表示换行User(bob):email:bob@gmail.compassword:secretfullname:BobisAdmin:trueUser(jeff):email:jeff@gmail.compassword:secretfullname:Jeff以上代码为YAML代码,代码语法见YAML语法说明:以上代码表示添加一个User对象,对象名称为bob,构造方法中传入了三个参数与值。创建一个测试方法进行测试,看看USER对象是否已经创建。代码如下:@TestpublicvoidshowUser(){System.out.println(User.count());Useruser=User.find(byEmail,bob@gmail.com).first();ass