国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

PlayFramework完整實現一個APP(三)

2019-11-14 15:16:33
字體:
來源:轉載
供稿:網友

 

 1.添加Post類

package models;import java.util.*;import javax.persistence.*;import play.db.jpa.*;@Entity@Table(name = "blog_post")public class Post extends Model {	public String title;	public Date postedAt;	@Lob	public String content;	@ManyToOne	public User author;	public Post(User author, String title, String content) {		this.author = author;		this.title = title;		this.content = title;	}}

@Lob 標識,字段是一個large text的類型,@ManyToOne 標識每個Post只能對應一個User,一個User可以對應多個Post

 

2. 添加測試用例

        @Test	public void createPost() {		// Create a new user and save it		User user = new User("bob@Gmail.com", "####", "Bob").save();		// Create a new post		new Post(user, "My first post", "Hello world").save();		// Test that the post has been created		assertEquals(1, Post.count());		// Retrieve all posts created by user		List<Post> posts = Post.find("byAuthor", user).fetch();		// Tests		assertEquals(1, posts.size());		Post firstPost = posts.get(0);		assertNotNull(firstPost);		assertEquals(user, firstPost.author);		assertEquals("My first post", firstPost.title);		assertEquals("Hello world", firstPost.content);		assertNotNull(firstPost.postedAt);	}    

  

3.添加Comment類

@Entitypublic class Comment extends Model {	public String author;	public Date postedAt;	@Lob	public String content;	@ManyToOne	public Post post;	public Comment(Post post, String author, String content) {		this.post = post;		this.author = author;		this.content = content;		this.postedAt = new Date();	}}

  

4.添加測試用例

@Testpublic void postComments() {    // Create a new user and save it    User bob = new User("bob@gmail.com", "secret", "Bob").save();     // Create a new post    Post bobPost = new Post(bob, "My first post", "Hello world").save();     // Post a first comment    new Comment(bobPost, "Jeff", "Nice post").save();    new Comment(bobPost, "Tom", "I knew that !").save();     // Retrieve all comments    List<Comment> bobPostComments = Comment.find("byPost", bobPost).fetch();     // Tests    assertEquals(2, bobPostComments.size());     Comment firstComment = bobPostComments.get(0);    assertNotNull(firstComment);    assertEquals("Jeff", firstComment.author);    assertEquals("Nice post", firstComment.content);    assertNotNull(firstComment.postedAt);     Comment secondComment = bobPostComments.get(1);    assertNotNull(secondComment);    assertEquals("Tom", secondComment.author);    assertEquals("I knew that !", secondComment.content);    assertNotNull(secondComment.postedAt);}

  

5.在Post類中添加Comment

@OneToMany(mappedBy="post", cascade=CascadeType.ALL)public List<Comment> comments;	public Post(User author, String title, String content) {	this.comments = new ArrayList<Comment>();	this.author = author;	this.title = title;	this.content = title;	this.postedAt = new Date();}

  

6.在Post類中添加方法

public Post addComment(String author, String content) {    Comment newComment = new Comment(this, author, content).save();    this.comments.add(newComment);    this.save();    return this;}

  

7.添加測試用例

@Testpublic void useTheCommentsRelation() {    // Create a new user and save it    User bob = new User("bob@gmail.com", "secret", "Bob").save();     // Create a new post    Post bobPost = new Post(bob, "My first post", "Hello world").save();     // Post a first comment    bobPost.addComment("Jeff", "Nice post");    bobPost.addComment("Tom", "I knew that !");     // Count things    assertEquals(1, User.count());    assertEquals(1, Post.count());    assertEquals(2, Comment.count());     // Retrieve Bob's post    bobPost = Post.find("byAuthor", bob).first();    assertNotNull(bobPost);     // Navigate to comments    assertEquals(2, bobPost.comments.size());    assertEquals("Jeff", bobPost.comments.get(0).author);        // Delete the post    bobPost.delete();        // Check that all comments have been deleted    assertEquals(1, User.count());    assertEquals(0, Post.count());    assertEquals(0, Comment.count());}

  

 

運行Test,如有異常會出現下方提示

 

 

 

.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 山丹县| 卫辉市| 隆子县| 鄢陵县| 祁连县| 噶尔县| 雷山县| 太和县| 额尔古纳市| 安国市| 德钦县| 天长市| 南郑县| 鸡西市| 确山县| 元江| 东山县| 镇平县| 茂名市| 盐津县| 眉山市| 苍梧县| 化隆| 新兴县| 伊川县| 北宁市| 凯里市| 郯城县| 长春市| 湖南省| 武宣县| 湾仔区| 南阳市| 安化县| 色达县| 轮台县| 永靖县| 华池县| 延长县| 枣庄市| 勃利县|