(三)play之yabe项目【数据模型】

创建项目

 play new yabe

 What is the application name? [yabe] Blog Engine

 play eclipsify yabe

 play run yabe

Eclipse引入项目

file -> import -> General ->Existing Projects into Workspaces -> select root directory ...

设置数据库

选择一个内存数据库

打开yabe/conf/application.conf,去掉下面这行的注释

db=mem #使用内存数据库,使用HSQL

【hsql 数据库是一款纯Java编写的免费数据库,体积小,才563kb。仅一个hsqldb.jar文件就包括了数据库引擎,数据库驱动,还有其他用户界面操作等内容。纯Java设计,又支持 SQL99,SQL2003大部分的标准】

************************************************************************************************************

增加模型User

Java代码  收藏代码
  1. package models;  
  2.   
  3. import javax.persistence.Entity;  
  4.   
  5. import play.db.jpa.Model;  
  6.   
  7. @Entity  
  8. public class User extends Model {  
  9.     public String email;  
  10.     public String password;  
  11.     public String fullname;  
  12.     public boolean isAdmin;  
  13.       
  14.       
  15.     public User(String email,String password, String fullname) {  
  16.         this.email = email;  
  17.         this.password = password;  
  18.         this.fullname = fullname;  
  19.     }  
  20. }  

使用JUNIT进行单元测试

打开命令行,切换到测试模式

play  test yabe

play提供了默认的测试文件,打开yabe estBasicTest.java

删除aVeryImportantThingToTest,新建一个测试

Java代码  收藏代码
  1. import org.junit.*;  
  2. import java.util.*;  
  3. import play.test.*;  
  4. import models.*;  
  5.   
  6. /** 
  7.  * 测试单元 extends UnitTest 
  8.  * @author lenovo 
  9.  * 
  10.  */  
  11. public class BasicTest extends UnitTest {  
  12.   
  13.     @Test  
  14.     public void createAndRetriveUser() {  
  15.         //Create a User and save  
  16.         new User("zs@162.com","******","ZS").save();  
  17.           
  18.         //retrieve User by emial  
  19.         User user1 = User.find("email", "zs@162.com").first();  
  20.         //两种写法都可以 匹配email="zs@162.com"的User对象  
  21.         User user2 = User.find("byEmail", "zs@162.com").first();  
  22.           
  23.         assertNotNull(user1);  
  24.         assertNotNull(user2);  
  25.         assertEquals(user1, user2);  
  26.         assertEquals("ZS", user1.fullname);  
  27.         assertEquals("ZS", user2.fullname);  
  28.     }  
  29.       
  30.       
  31.   
  32. }  

打开http://localhost:9000/@tests

运行BasicTest,即上面写的测试单元,点击Start !   进行测试

结果应该为绿色!


 

接下来测试User的email和password是否正确的方法

通过email和password两个条件来找到User对象

打开User.java,添加connect():

Java代码  收藏代码
  1. package models;  
  2.   
  3. import javax.persistence.Entity;  
  4.   
  5. import play.db.jpa.Model;  
  6.   
  7. @Entity  
  8. public class User extends Model {  
  9.     public String email;  
  10.     public String password;  
  11.     public String fullname;  
  12.     public boolean isAdmin;  
  13.       
  14.       
  15.     public User(String email,String password, String fullname) {  
  16.         this.email = email;  
  17.         this.password = password;  
  18.         this.fullname = fullname;  
  19.     }  
  20.       
  21.     /** 
  22.      * 联合email和password两个条件查询User 
  23.      * @param email 
  24.      * @param password 
  25.      * @return 
  26.      */  
  27.     public static User connect(String email, String password) {  
  28.         return find("byEmailAndPassword", email, password).first();  
  29.     }  
  30. }  

 在BasicTest.java里添加测试方法

Java代码  收藏代码
  1. import org.junit.*;  
  2. import java.util.*;  
  3. import play.test.*;  
  4. import models.*;  
  5.   
  6. /** 
  7.  * 测试单元 extends UnitTest 
  8.  * @author lenovo 
  9.  * 
  10.  */  
  11. public class BasicTest extends UnitTest {  
  12.       
  13.     /** 
  14.      * 测试用户的创建和查找 
  15.      */  
  16.     @Test  
  17.     public void createAndRetriveUser() {  
  18.         //Create a User and save  
  19.         new User("zs@162.com","******","ZS").save();  
  20.           
  21.         //retrieve User by emial  
  22.         User user1 = User.find("email", "zs@162.com").first();  
  23.         //两种写法都可以 匹配email="zs@162.com"的User对象  
  24.         User user2 = User.find("byEmail", "zs@162.com").first();  
  25.           
  26.         assertNotNull(user1);  
  27.         assertNotNull(user2);  
  28.         assertEquals(user1, user2);  
  29.         assertEquals("ZS", user1.fullname);  
  30.         assertEquals("ZS", user2.fullname);  
  31.     }  
  32.       
  33.     /** 
  34.      * 测试联合条件查询 
  35.      */  
  36.     @Test  
  37.     public void tryConnectUser() {  
  38.         //Create a user and save it  
  39.         new User("zs@1.com", "123", "zhangsan").save();  
  40.           
  41.         //Test  
  42.         assertNotNull(User.connect("zs@1.com", "123"));  
  43.         assertNull(User.connect("zs@1.com", "234"));  
  44.         assertNull(User.connect("zs@2.com", "234"));  
  45.     }  
  46.   
  47. }  

 http://localhost:9000/@tests

测试BasicTest项,应该全部通过!

增加模型Post

Post类作为博客的实体类,与User之间为多对一的关系

Java代码  收藏代码
  1. package models;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.persistence.Entity;  
  6. import javax.persistence.Lob;  
  7. import javax.persistence.ManyToOne;  
  8.   
  9. import play.db.jpa.Model;  
  10.   
  11. @Entity  
  12. public class Post extends Model {  
  13.     public String title;  
  14.     public Date postedAt;  
  15.       
  16.     //@Lob标注:声明这是一个超大文本数据类型,用于存储发布的博客内容  
  17.     @Lob  
  18.     public String content;  
  19.       
  20.     //@ManyToOne:声明Post与User之间是多对一的关系  
  21.     //一个用户可以发布多个博客,一个博客只能被一个用户所发布  
  22.     @ManyToOne  
  23.     public User author;  
  24.   
  25.     public Post(String title, String content, User author) {  
  26.         this.title = title;  
  27.         this.content = content;  
  28.         this.author = author;  
  29.         this.postedAt = new Date();  
  30.     }  
  31.       
  32.       
  33. }  

对Post类进行测试

Java代码  收藏代码
  1. import org.junit.*;  
  2. import java.util.*;  
  3. import play.test.*;  
  4. import models.*;  
  5.   
  6. /** 
  7.  * 测试单元 extends UnitTest 
  8.  * @author lenovo 
  9.  * 
  10.  */  
  11. public class BasicTest extends UnitTest {  
  12.       
  13.     /** 
  14.      * 清空数据库中的数据,释放内存空间 
  15.      * Fixtures帮助在测试期间管理数据库 
  16.      */  
  17.     @Before  
  18.     public void setup() {  
  19.         Fixtures.deleteDatabase();  
  20.     }  
  21.       
  22.     /** 
  23.      * 测试用户的创建和查找 
  24.      */  
  25.     @Test  
  26.     public void createAndRetriveUser() {  
  27.         //Create a User and save  
  28.         new User("zs@162.com","******","ZS").save();  
  29.           
  30.         //retrieve User by emial  
  31.         User user1 = User.find("email", "zs@162.com").first();  
  32.         //两种写法都可以 匹配email="zs@162.com"的User对象  
  33.         User user2 = User.find("byEmail", "zs@162.com").first();  
  34.           
  35.         assertNotNull(user1);  
  36.         assertNotNull(user2);  
  37.         assertEquals(user1, user2);  
  38.         assertEquals("ZS", user1.fullname);  
  39.         assertEquals("ZS", user2.fullname);  
  40.     }  
  41.       
  42.     /** 
  43.      * 测试联合条件查询 
  44.      */  
  45.     @Test  
  46.     public void tryConnectUser() {  
  47.         //Create a user and save it  
  48.         new User("zs@1.com", "123", "zhangsan").save();  
  49.           
  50.         //Test  
  51.         assertNotNull(User.connect("zs@1.com", "123"));  
  52.         assertNull(User.connect("zs@1.com", "234"));  
  53.         assertNull(User.connect("zs@2.com", "234"));  
  54.     }  
  55.       
  56.       
  57.     /** 
  58.      * 测试Post类 
  59.      */  
  60.     @Test  
  61.     public void createPost() {  
  62.         //Create a User and save it   
  63.         User Mike = new User("ls@1.com", "111", "Mike").save();  
  64.           
  65.         //Create 2 Post  
  66.         new Post("First Blog", "first", Mike).save();  
  67.         new Post("Second Blog", "second", Mike).save();  
  68.           
  69.         //测试是否成功创建了2个Post对象  
  70.         assertEquals(2, Post.count());  
  71.           
  72.         //获取lisi发布的所有博客  
  73.         List<Post> posts = Post.find("byAuthor", Mike).fetch();  
  74.           
  75.         assertEquals(2, posts.size());  
  76.           
  77.         assertNotNull(posts.get(0));  
  78.         assertNotNull(posts.get(1));  
  79.           
  80.         assertEquals(Mike, posts.get(0).author);  
  81.           
  82.         assertEquals("First Blog", posts.get(0).title);  
  83.           
  84.         assertEquals("second", posts.get(1).content);  
  85.           
  86.         assertNotNull(posts.get(1).postedAt);  
  87.           
  88.     }  
  89.       
  90. }  

 增加模型Comment

Comment 与 Post 之间为多对一的关系,多条评论对应一篇博客

Java代码  收藏代码
  1. package models;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.persistence.Entity;  
  6. import javax.persistence.Lob;  
  7. import javax.persistence.ManyToOne;  
  8.   
  9. import play.db.jpa.Model;  
  10.   
  11. @Entity  
  12. public class Comment extends Model {  
  13.     public String author;  
  14.     public Date postedAt;  
  15.       
  16.     @Lob  
  17.     public String content;  
  18.       
  19.     //一篇博客对应多条评论,一个评论属于一篇博客  
  20.     //评论与博客的关系:多对一  
  21.     @ManyToOne  
  22.     public Post post;  
  23.   
  24.     public Comment(String author, String content, Post post) {  
  25.         super();  
  26.         this.author = author;  
  27.         this.content = content;  
  28.         this.post = post;  
  29.         this.postedAt = new Date();  
  30.     }  
  31.       
  32. }  

修改模型Post,增加Comment属性

注:上面的User类也可以持有一个关于Post的集合,方法与此类似。

Java代码  收藏代码
  1. package models;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Date;  
  5. import java.util.List;  
  6.   
  7. import javax.persistence.CascadeType;  
  8. import javax.persistence.Entity;  
  9. import javax.persistence.Lob;  
  10. import javax.persistence.ManyToOne;  
  11. import javax.persistence.OneToMany;  
  12.   
  13. import play.db.jpa.Model;  
  14.   
  15. @Entity  
  16. public class Post extends Model {  
  17.     public String title;  
  18.     public Date postedAt;  
  19.       
  20.     //@Lob标注:声明这是一个超大文本数据类型,用于存储发布的博客内容  
  21.     @Lob  
  22.     public String content;  
  23.       
  24.     //@ManyToOne:声明Post与User之间是多对一的关系  
  25.     //一个用户可以发布多个博客,一个博客只能被一个用户所发布  
  26.     @ManyToOne  
  27.     public User author;  
  28.       
  29.     //1篇博客对应多个评论  
  30.     //删除某篇博客,则级联删除其评论  
  31.     @OneToMany(mappedBy="post", cascade=CascadeType.ALL)  
  32.     public List<Comment> comments;  
  33.   
  34.     public Post(String title, String content, User author) {  
  35.         this.comments = new ArrayList<Comment>(0);  
  36.         this.title = title;  
  37.         this.content = content;  
  38.         this.author = author;  
  39.         this.postedAt = new Date();  
  40.     }  
  41.       
  42.     /** 
  43.      * 在Post中实现评论的添加保存操作 
  44.      * 同时更新Post所持有的Comment的集合! 
  45.      */  
  46.     public Post addComment(String author, String content) {  
  47.         //保存对方  
  48.         Comment newComment = new Comment(author, content, this).save();  
  49.         //把对方加入到自己管理的集合中  
  50.         this.comments.add(newComment);  
  51.         //同步到数据库  
  52.         this.save();  
  53.         return this;  
  54.     }  
  55. }  

 所有的测试代码

Java代码  收藏代码
  1. import org.junit.*;  
  2. import java.util.*;  
  3. import play.test.*;  
  4. import models.*;  
  5.   
  6. /** 
  7.  * 测试单元 extends UnitTest 
  8.  * @author lenovo 
  9.  * 
  10.  */  
  11. public class BasicTest extends UnitTest {  
  12.       
  13.     /** 
  14.      * 清空数据库中的数据,释放内存空间 
  15.      * Fixtures帮助在测试期间管理数据库 
  16.      */  
  17.     @Before  
  18.     public void setup() {  
  19.         Fixtures.deleteDatabase();  
  20.     }  
  21.       
  22.     /** 
  23.      * 测试用户的创建和查找 
  24.      */  
  25.     @Test  
  26.     public void createAndRetriveUser() {  
  27.         //Create a User and save  
  28.         new User("zs@162.com","******","ZS").save();  
  29.           
  30.         //retrieve User by emial  
  31.         User user1 = User.find("email", "zs@162.com").first();  
  32.         //两种写法都可以 匹配email="zs@162.com"的User对象  
  33.         User user2 = User.find("byEmail", "zs@162.com").first();  
  34.           
  35.         assertNotNull(user1);  
  36.         assertNotNull(user2);  
  37.         assertEquals(user1, user2);  
  38.         assertEquals("ZS", user1.fullname);  
  39.         assertEquals("ZS", user2.fullname);  
  40.     }  
  41.       
  42.     /** 
  43.      * 测试联合条件查询 
  44.      */  
  45.     @Test  
  46.     public void tryConnectUser() {  
  47.         //Create a user and save it  
  48.         new User("zs@1.com", "123", "zhangsan").save();  
  49.           
  50.         //Test  
  51.         assertNotNull(User.connect("zs@1.com", "123"));  
  52.         assertNull(User.connect("zs@1.com", "234"));  
  53.         assertNull(User.connect("zs@2.com", "234"));  
  54.     }  
  55.       
  56.       
  57.     /** 
  58.      * 测试Post类 
  59.      */  
  60.     @Test  
  61.     public void createPost() {  
  62.         //Create a User and save it   
  63.         User Mike = new User("ls@1.com", "111", "Mike").save();  
  64.           
  65.         //Create 2 Post  
  66.         new Post("First Blog", "first", Mike).save();  
  67.         new Post("Second Blog", "second", Mike).save();  
  68.           
  69.         //测试是否成功创建了2个Post对象  
  70.         assertEquals(2, Post.count());  
  71.           
  72.         //获取lisi发布的所有博客  
  73.         List<Post> posts = Post.find("byAuthor", Mike).fetch();  
  74.           
  75.         assertEquals(2, posts.size());  
  76.           
  77.         assertNotNull(posts.get(0));  
  78.         assertNotNull(posts.get(1));  
  79.           
  80.         assertEquals(Mike, posts.get(0).author);  
  81.           
  82.         assertEquals("First Blog", posts.get(0).title);  
  83.           
  84.         assertEquals("second", posts.get(1).content);  
  85.           
  86.         assertNotNull(posts.get(1).postedAt);  
  87.           
  88.     }  
  89.       
  90.     /** 
  91.      * 测试Post与User的多对一关系 
  92.      */  
  93.     @Test  
  94.     public void testPost2User() {  
  95.         User Mike = new User("ls@1.com", "111", "Mike").save();  
  96.         Mike.addPost("First Blog", "first");  
  97.         Mike.addPost("Second Blog", "second");  
  98.           
  99.         assertNotNull(Mike);  
  100.         assertEquals(2, Post.count());  
  101.           
  102.         //从新查询User  
  103.         Mike = User.connect("ls@1.com", "111");  
  104.           
  105.         //直接从User中获取Post的集合  
  106.         assertEquals(2, Mike.posts.size());  
  107.           
  108.         assertEquals("first", Mike.posts.get(0).content);  
  109.         assertEquals("second", Mike.posts.get(1).content);  
  110.     }  
  111.       
  112.     /** 
  113.      * 测试Comment类 
  114.      * 单向,由Comment得到Post 
  115.      */  
  116.     @Test  
  117.     public void testComment() {  
  118.         User Mike = new User("ls@1.com", "111", "Mike").save();  
  119.         Post post = new Post("First Blog", "first", Mike).save();  
  120.         new Comment("jeff", "nice post", post).save();  
  121.         new Comment("henrry", "good post", post).save();  
  122.           
  123.         //获取博客的所有评论  
  124.         List<Comment> comments = Comment.find("byPost", post).fetch();  
  125.           
  126.         assertEquals(2, comments.size());  
  127.           
  128.         Comment firstComment = comments.get(0);  
  129.         assertNotNull(firstComment);  
  130.         assertEquals("jeff", firstComment.author);  
  131.         assertEquals("nice post", firstComment.content);  
  132.         assertNotNull(firstComment.postedAt);  
  133.           
  134.         Comment secondComment = comments.get(1);  
  135.         assertNotNull(secondComment);  
  136.         assertEquals("henrry", secondComment.author);  
  137.         assertEquals("good post", secondComment.content);  
  138.         assertNotNull(firstComment.postedAt);  
  139.           
  140.     }  
  141.       
  142.     /** 
  143.      * 测试Comment与Post的多对一关系 
  144.      * 双向,由Post直接得到与此关联的Comment集合 
  145.      */  
  146.     @Test  
  147.     public void testComment2PostRelation() {  
  148.         //User  
  149.         User bob = new User("bob@123.com","111","Bob well").save();  
  150.         //Post  
  151.         Post post = new Post("First post","hello kelly",bob).save();  
  152.         //Comment  
  153.         post.addComment("jeff", "Nice Post!");  
  154.         post.addComment("Tom", "I knew that!");  
  155.           
  156.         assertEquals(1, User.count());  
  157.         assertEquals(1, Post.count());  
  158.         assertEquals(2, Comment.count());  
  159.           
  160.         //Retrieve Bob's post  
  161.         post = Post.find("byAuthor", bob).first();  
  162.         assertNotNull(post);  
  163.           
  164.         //Navigation to comments  
  165.         assertEquals(2, post.comments.size());  
  166.         assertEquals("jeff", post.comments.get(0).author);  
  167.   
  168.         //delete Post  
  169.         post.delete();  
  170.           
  171.         //check that all commonts have been deleted  
  172.         assertEquals(1, User.count());  
  173.         assertEquals(0, Post.count());  
  174.         assertEquals(0, Comment.count());  
  175.     }  
  176.       
  177. }  
原文地址:https://www.cnblogs.com/zhiji6/p/4445042.html