您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PlayFramework完整实现一个APP(二)

2025/11/22 4:20:54发布19次查看
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(二)的内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product