Home> Java> javaTutorial> body text

PlayFramework completely implements an APP (2)

黄舟
Release: 2016-12-23 16:36:35
Original
910 people have browsed it

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

PlayFramework completely implements an APP (2)


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 (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!