(三)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
- package models;
- import javax.persistence.Entity;
- import play.db.jpa.Model;
- @Entity
- public class User extends Model {
- public String email;
- public String password;
- public String fullname;
- public boolean isAdmin;
- public User(String email,String password, String fullname) {
- this.email = email;
- this.password = password;
- this.fullname = fullname;
- }
- }
使用JUNIT进行单元测试
打开命令行,切换到测试模式
play test yabe
play提供了默认的测试文件,打开yabe\test\BasicTest.java
删除aVeryImportantThingToTest,新建一个测试
- import org.junit.*;
- import java.util.*;
- import play.test.*;
- import models.*;
- /**
- * 测试单元 extends UnitTest
- * @author lenovo
- *
- */
- public class BasicTest extends UnitTest {
- @Test
- public void createAndRetriveUser() {
- //Create a User and save
- new User("zs@162.com","******","ZS").save();
- //retrieve User by emial
- User user1 = User.find("email", "zs@162.com").first();
- //两种写法都可以 匹配email="zs@162.com"的User对象
- User user2 = User.find("byEmail", "zs@162.com").first();
- assertNotNull(user1);
- assertNotNull(user2);
- assertEquals(user1, user2);
- assertEquals("ZS", user1.fullname);
- assertEquals("ZS", user2.fullname);
- }
- }
打开http://localhost:9000/@tests
运行BasicTest,即上面写的测试单元,点击Start ! 进行测试
结果应该为绿色!
接下来测试User的email和password是否正确的方法
通过email和password两个条件来找到User对象
打开User.java,添加connect():
- package models;
- import javax.persistence.Entity;
- import play.db.jpa.Model;
- @Entity
- public class User extends Model {
- public String email;
- public String password;
- public String fullname;
- public boolean isAdmin;
- public User(String email,String password, String fullname) {
- this.email = email;
- this.password = password;
- this.fullname = fullname;
- }
- /**
- * 联合email和password两个条件查询User
- * @param email
- * @param password
- * @return
- */
- public static User connect(String email, String password) {
- return find("byEmailAndPassword", email, password).first();
- }
- }
在BasicTest.java里添加测试方法
- import org.junit.*;
- import java.util.*;
- import play.test.*;
- import models.*;
- /**
- * 测试单元 extends UnitTest
- * @author lenovo
- *
- */
- public class BasicTest extends UnitTest {
- /**
- * 测试用户的创建和查找
- */
- @Test
- public void createAndRetriveUser() {
- //Create a User and save
- new User("zs@162.com","******","ZS").save();
- //retrieve User by emial
- User user1 = User.find("email", "zs@162.com").first();
- //两种写法都可以 匹配email="zs@162.com"的User对象
- User user2 = User.find("byEmail", "zs@162.com").first();
- assertNotNull(user1);
- assertNotNull(user2);
- assertEquals(user1, user2);
- assertEquals("ZS", user1.fullname);
- assertEquals("ZS", user2.fullname);
- }
- /**
- * 测试联合条件查询
- */
- @Test
- public void tryConnectUser() {
- //Create a user and save it
- new User("zs@1.com", "123", "zhangsan").save();
- //Test
- assertNotNull(User.connect("zs@1.com", "123"));
- assertNull(User.connect("zs@1.com", "234"));
- assertNull(User.connect("zs@2.com", "234"));
- }
- }
测试BasicTest项,应该全部通过!
增加模型Post
Post类作为博客的实体类,与User之间为多对一的关系
- package models;
- import java.util.Date;
- import javax.persistence.Entity;
- import javax.persistence.Lob;
- import javax.persistence.ManyToOne;
- import play.db.jpa.Model;
- @Entity
- public class Post extends Model {
- public String title;
- public Date postedAt;
- //@Lob标注:声明这是一个超大文本数据类型,用于存储发布的博客内容
- @Lob
- public String content;
- //@ManyToOne:声明Post与User之间是多对一的关系
- //一个用户可以发布多个博客,一个博客只能被一个用户所发布
- @ManyToOne
- public User author;
- public Post(String title, String content, User author) {
- this.title = title;
- this.content = content;
- this.author = author;
- this.postedAt = new Date();
- }
- }
对Post类进行测试
- import org.junit.*;
- import java.util.*;
- import play.test.*;
- import models.*;
- /**
- * 测试单元 extends UnitTest
- * @author lenovo
- *
- */
- public class BasicTest extends UnitTest {
- /**
- * 清空数据库中的数据,释放内存空间
- * Fixtures帮助在测试期间管理数据库
- */
- @Before
- public void setup() {
- Fixtures.deleteDatabase();
- }
- /**
- * 测试用户的创建和查找
- */
- @Test
- public void createAndRetriveUser() {
- //Create a User and save
- new User("zs@162.com","******","ZS").save();
- //retrieve User by emial
- User user1 = User.find("email", "zs@162.com").first();
- //两种写法都可以 匹配email="zs@162.com"的User对象
- User user2 = User.find("byEmail", "zs@162.com").first();
- assertNotNull(user1);
- assertNotNull(user2);
- assertEquals(user1, user2);
- assertEquals("ZS", user1.fullname);
- assertEquals("ZS", user2.fullname);
- }
- /**
- * 测试联合条件查询
- */
- @Test
- public void tryConnectUser() {
- //Create a user and save it
- new User("zs@1.com", "123", "zhangsan").save();
- //Test
- assertNotNull(User.connect("zs@1.com", "123"));
- assertNull(User.connect("zs@1.com", "234"));
- assertNull(User.connect("zs@2.com", "234"));
- }
- /**
- * 测试Post类
- */
- @Test
- public void createPost() {
- //Create a User and save it
- User Mike = new User("ls@1.com", "111", "Mike").save();
- //Create 2 Post
- new Post("First Blog", "first", Mike).save();
- new Post("Second Blog", "second", Mike).save();
- //测试是否成功创建了2个Post对象
- assertEquals(2, Post.count());
- //获取lisi发布的所有博客
- List<Post> posts = Post.find("byAuthor", Mike).fetch();
- assertEquals(2, posts.size());
- assertNotNull(posts.get(0));
- assertNotNull(posts.get(1));
- assertEquals(Mike, posts.get(0).author);
- assertEquals("First Blog", posts.get(0).title);
- assertEquals("second", posts.get(1).content);
- assertNotNull(posts.get(1).postedAt);
- }
- }
增加模型Comment
Comment 与 Post 之间为多对一的关系,多条评论对应一篇博客
- package models;
- import java.util.Date;
- import javax.persistence.Entity;
- import javax.persistence.Lob;
- import javax.persistence.ManyToOne;
- import play.db.jpa.Model;
- @Entity
- public class Comment extends Model {
- public String author;
- public Date postedAt;
- @Lob
- public String content;
- //一篇博客对应多条评论,一个评论属于一篇博客
- //评论与博客的关系:多对一
- @ManyToOne
- public Post post;
- public Comment(String author, String content, Post post) {
- super();
- this.author = author;
- this.content = content;
- this.post = post;
- this.postedAt = new Date();
- }
- }
修改模型Post,增加Comment属性
注:上面的User类也可以持有一个关于Post的集合,方法与此类似。
- package models;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import javax.persistence.CascadeType;
- import javax.persistence.Entity;
- import javax.persistence.Lob;
- import javax.persistence.ManyToOne;
- import javax.persistence.OneToMany;
- import play.db.jpa.Model;
- @Entity
- public class Post extends Model {
- public String title;
- public Date postedAt;
- //@Lob标注:声明这是一个超大文本数据类型,用于存储发布的博客内容
- @Lob
- public String content;
- //@ManyToOne:声明Post与User之间是多对一的关系
- //一个用户可以发布多个博客,一个博客只能被一个用户所发布
- @ManyToOne
- public User author;
- //1篇博客对应多个评论
- //删除某篇博客,则级联删除其评论
- @OneToMany(mappedBy="post", cascade=CascadeType.ALL)
- public List<Comment> comments;
- public Post(String title, String content, User author) {
- this.comments = new ArrayList<Comment>(0);
- this.title = title;
- this.content = content;
- this.author = author;
- this.postedAt = new Date();
- }
- /**
- * 在Post中实现评论的添加保存操作
- * 同时更新Post所持有的Comment的集合!
- */
- public Post addComment(String author, String content) {
- //保存对方
- Comment newComment = new Comment(author, content, this).save();
- //把对方加入到自己管理的集合中
- this.comments.add(newComment);
- //同步到数据库
- this.save();
- return this;
- }
- }
所有的测试代码
- import org.junit.*;
- import java.util.*;
- import play.test.*;
- import models.*;
- /**
- * 测试单元 extends UnitTest
- * @author lenovo
- *
- */
- public class BasicTest extends UnitTest {
- /**
- * 清空数据库中的数据,释放内存空间
- * Fixtures帮助在测试期间管理数据库
- */
- @Before
- public void setup() {
- Fixtures.deleteDatabase();
- }
- /**
- * 测试用户的创建和查找
- */
- @Test
- public void createAndRetriveUser() {
- //Create a User and save
- new User("zs@162.com","******","ZS").save();
- //retrieve User by emial
- User user1 = User.find("email", "zs@162.com").first();
- //两种写法都可以 匹配email="zs@162.com"的User对象
- User user2 = User.find("byEmail", "zs@162.com").first();
- assertNotNull(user1);
- assertNotNull(user2);
- assertEquals(user1, user2);
- assertEquals("ZS", user1.fullname);
- assertEquals("ZS", user2.fullname);
- }
- /**
- * 测试联合条件查询
- */
- @Test
- public void tryConnectUser() {
- //Create a user and save it
- new User("zs@1.com", "123", "zhangsan").save();
- //Test
- assertNotNull(User.connect("zs@1.com", "123"));
- assertNull(User.connect("zs@1.com", "234"));
- assertNull(User.connect("zs@2.com", "234"));
- }
- /**
- * 测试Post类
- */
- @Test
- public void createPost() {
- //Create a User and save it
- User Mike = new User("ls@1.com", "111", "Mike").save();
- //Create 2 Post
- new Post("First Blog", "first", Mike).save();
- new Post("Second Blog", "second", Mike).save();
- //测试是否成功创建了2个Post对象
- assertEquals(2, Post.count());
- //获取lisi发布的所有博客
- List<Post> posts = Post.find("byAuthor", Mike).fetch();
- assertEquals(2, posts.size());
- assertNotNull(posts.get(0));
- assertNotNull(posts.get(1));
- assertEquals(Mike, posts.get(0).author);
- assertEquals("First Blog", posts.get(0).title);
- assertEquals("second", posts.get(1).content);
- assertNotNull(posts.get(1).postedAt);
- }
- /**
- * 测试Post与User的多对一关系
- */
- @Test
- public void testPost2User() {
- User Mike = new User("ls@1.com", "111", "Mike").save();
- Mike.addPost("First Blog", "first");
- Mike.addPost("Second Blog", "second");
- assertNotNull(Mike);
- assertEquals(2, Post.count());
- //从新查询User
- Mike = User.connect("ls@1.com", "111");
- //直接从User中获取Post的集合
- assertEquals(2, Mike.posts.size());
- assertEquals("first", Mike.posts.get(0).content);
- assertEquals("second", Mike.posts.get(1).content);
- }
- /**
- * 测试Comment类
- * 单向,由Comment得到Post
- */
- @Test
- public void testComment() {
- User Mike = new User("ls@1.com", "111", "Mike").save();
- Post post = new Post("First Blog", "first", Mike).save();
- new Comment("jeff", "nice post", post).save();
- new Comment("henrry", "good post", post).save();
- //获取博客的所有评论
- List<Comment> comments = Comment.find("byPost", post).fetch();
- assertEquals(2, comments.size());
- Comment firstComment = comments.get(0);
- assertNotNull(firstComment);
- assertEquals("jeff", firstComment.author);
- assertEquals("nice post", firstComment.content);
- assertNotNull(firstComment.postedAt);
- Comment secondComment = comments.get(1);
- assertNotNull(secondComment);
- assertEquals("henrry", secondComment.author);
- assertEquals("good post", secondComment.content);
- assertNotNull(firstComment.postedAt);
- }
- /**
- * 测试Comment与Post的多对一关系
- * 双向,由Post直接得到与此关联的Comment集合
- */
- @Test
- public void testComment2PostRelation() {
- //User
- User bob = new User("bob@123.com","111","Bob well").save();
- //Post
- Post post = new Post("First post","hello kelly",bob).save();
- //Comment
- post.addComment("jeff", "Nice Post!");
- post.addComment("Tom", "I knew that!");
- assertEquals(1, User.count());
- assertEquals(1, Post.count());
- assertEquals(2, Comment.count());
- //Retrieve Bob's post
- post = Post.find("byAuthor", bob).first();
- assertNotNull(post);
- //Navigation to comments
- assertEquals(2, post.comments.size());
- assertEquals("jeff", post.comments.get(0).author);
- //delete Post
- post.delete();
- //check that all commonts have been deleted
- assertEquals(1, User.count());
- assertEquals(0, Post.count());
- assertEquals(0, Comment.count());
- }
- }