Rumah >Java >javaTutorial >PlayFramework完整实现一个APP(二)
1.开发DataModel
在app\moders 下新建User.java
package models;
import java.util.*;
import javax.persistence.*;
import play.db.jpa.*;
@Entity
public 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
@Test
public 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();
}
添加测试用例
@Test
public 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", "####"));
}
..
以上就是PlayFramework完整实现一个APP(二)的内容,更多相关内容请关注PHP中文网(www.php.cn)!