java play框架 mysql,有关使用Ebean的Play框架和MySQL的简单CRUD教程?

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:

41Z4F.png

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

解决方案

Hooray!!!

List action

public static Result list(){

List products = Product.findAll();

return ok(play.libs.Json.toJson(products));

}

findAll method in Product Model

public static List 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 find = new Model.Finder(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 findAll(){

return Product.find.orderBy("id").findList();

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值