【问题标题】:Simple CRUD tutorial about Play Framework and MySQL using Ebean?关于使用 Ebean 的 Play Framework 和 MySQL 的简单 CRUD 教程?
【发布时间】:2014-10-12 16:59:35
【问题描述】:

我是 Play 框架的新手。我已经开始学习它,到目前为止我很享受它。 我已经开始学习 Play Java。

我的控制器和模型设置如下:

控制器:

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
    }
}

型号:

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
    }
}

为了启用 MySql,我已经编辑了 /conf/application.conf 如下:

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.*"

我有一个 play_db 数据库,其中一张表如下所示:

我的问题是如何使用 ebean 和 MySQL 获取 Product 模型中的所有产品。 有人可以指点我一个简单的 crud 教程,它使用 play java 与 ebean 和 MySql 结合使用吗?谢谢

有人吗?

注意 顺便说一句,我正在使用 Play v.2.3.5 for Java

【问题讨论】:

    标签: java mysql playframework-2.0 playframework-2.3


    【解决方案1】:

    万岁!!!

    列表操作

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

    产品模型中的 findAll 方法

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

    最后,我必须通过取消注释以下行来启用 /conf/application.conf 中的进化

    # evolutionplugin=disabled
    

    public class Product extends Model{

    之前添加@Entity

    最终代码:

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

    我希望这对 Play Java 新手有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多