1. Develop DataModel
Create a new User.java under appmoders
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 identifier is a JPA entity, inherited from play.db.jpa.Model, which provides fields of the JPA implementation
class, which will automatically Map to the DB table, the default indication is "User", if you want to modify the indication, add the label "@Table(name="blog_user")" to the class
2. Test
Run
> play test yape
or run in Eclipse, Test Yet Another Blog Engine
Visit http://localhost:9000/@tests, enter the test mode
Select Test, Start execution, success will be marked as Green, failure will prompt
3. Write test case
Modify /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);
}
Create User, find User, make assertions
User inherits from Model , provides savefind and other methods
User.java adds the connect method
public static User connect(String email, String passowrd) {
return find("byEmailAndPassword", email, passowrd).first();
}
Add test case
@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", "####"));
}
..
That’s it PlayFramework completely implements the content of an APP (2). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!