首页 > 学院 > 开发设计 > 正文

PlayFramework完整实现一个APP(二)

2019-11-14 15:16:37
字体:
来源:转载
供稿:网友

 

1.开发DataModel

在app/moders 下新建User.java

package models;import java.util.*;import javax.persistence.*;import play.db.jpa.*;@Entitypublic class User extends Model {	public String email;	public String passWord;	public String fullname;	public String isAdmin;	public User(String email, String password, String fullname) {		this.email = email;		this.password = password;		this.fullname = fullname;	}}

@Entity标识是一个JPA entity,继承自play.db.jpa.Model,提供了JPA实现

类的字段,会自动映射到DB表中,默认表明是"User",如果要修改表明,在类上添加标签"@Table(name="blog_user")"

 

2.测试

 运行

>play test yape

或在Eclipse中运行,Test Yet Another Blog Engine

访问 http://localhost:9000/@tests, 进入测试模式

选择Test, Start执行,成功会标记为绿色,失败会有提示

 

3.写测试用例

修改 /test/BasicTest.java

@Testpublic void createAndRetrieveUser() {    	//Create a new user and save it    	new User("alex@Gmail.com", "####", "Alex").save();    	    	//Retrieve the user with email address    	User user = User.find("byEmail", "alex@gmail.com").first();    	    	//Test    	assertNotNull(user);    	assertEquals("Alex", user.fullname); }

创建User,查找User,进行断言

User继承自Model,提供了save/find等方法

 

User.java添加connect方法

public static User connect(String email, String passowrd) {	return find("byEmailAndPassword", email, passowrd).first();}

  

添加测试用例

@Testpublic void tryConnectAsUser() {    	// Create a new user and save it    	new User("bob@gmail.com", "####", "Bob").save();    	    	// Test         assertNotNull(User.connect("bob@gmail.com", "####"));        assertNull(User.connect("bob@gmail.com", "$$$$"));        assertNull(User.connect("tom@gmail.com", "####"));}

  

 

 

..


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表