Mysql
 sql >> Base de données >  >> RDS >> Mysql

Tutoriel CRUD simple sur Play Framework et MySQL utilisant Ebean ?

Hourra !!!

Action de liste

public static Result list(){
    List<Product> products = Product.findAll();
    return ok(play.libs.Json.toJson(products));
}

méthode findAll dans le modèle de produit

public static  List<Product> findAll(){
    return  Product.find.orderBy("id").findList();  
}

Enfin, je dois activer l'évolution dans /conf/application.conf en décommentant la ligne suivante

# evolutionplugin=disabled

Ajouter @Entity juste avant que la classe publique Product n'étende Model{

Code final :

package models;

import java.util.List;

import javax.persistence.Entity;

import play.db.*;
import play.db.ebean.Model;

import play.api.db.DB;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.Query;


@Entity

public class Product extends Model{

    public int id;
    public String name;
    public String description;

    public static Model.Finder<String, Product> find = new Model.Finder<String, Product>(String.class, Product.class);

    public Product(){

    }

    public Product(int id, String name, String description){
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public static  List<Product> findAll(){
        return  Product.find.orderBy("id").findList();
    }
}

J'espère que cela aidera tous ceux qui découvrent également Play Java