Simple CRUD tutorial about Play Framework and MySQL using Ebean?

ぐ巨炮叔叔 提交于 2019-12-09 17:27:51

问题


I am new to Play Framework. I have started learning it and so far I am enjoying it. I have started to learn Play Java.

I have my controller and model set up as follow:

Controller:

package controllers;

import play.mvc.Controller;
import play.mvc.Result;

//Import Product model
import models.Product;

public class Products extends Controller{


    /**
     * List all Products
     */
    public static Result list(){
        Object allProducts = Product.findAll();
        return ok((Content) allProducts); //return all products
    }
}

Model:

package models;

import java.util.List;
import play.db.*;

import play.api.db.DB;

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

public class Product {

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

    public Product(){

    }

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

    public static  String findAll(){
        //Using ebean and MySql, fech the product table
        //and return all products
    }
}

To enable the use of MySql, I have already edited the /conf/application.conf as follow:

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/play_db?characterEncoding=UTF-8"
db.default.user=user 
db.default.password=pass
ebean.default="models.*"

I have a play_db database with one table shown as follow:

My problem is how to fetch all the products in the Product model using ebean and MySQL. Can someone please point me to a simple crud tutorial which uses play java in combination with ebean and MySql? Thanks

Anyone?

NOTE By the way, I am using Play v.2.3.5 for Java


回答1:


Hooray!!!

List action

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

findAll method in Product Model

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

Lastly, I have to enable evolution in /conf/application.conf by uncommenting the following line

# evolutionplugin=disabled

Add @Entity just before public class Product extends Model{

Final code:

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();
    }
}

I hope this will help anyone who is also new to Play Java



来源:https://stackoverflow.com/questions/26327664/simple-crud-tutorial-about-play-framework-and-mysql-using-ebean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!