使用playframework删除,更新mysql中的单个记录

时间:2015-05-13 06:34:17

标签: java mysql playframework

您好我是Play框架的新手。我想在Play框架中使用MYSQL数据库实现CRUD操作。

我能够成功删除,更新整个表,但无法成功删除单个记录。我想传递来自html的值。

index.scala.html

@(message: String)

@main("Welcome to Play") {

 <ul id="bars">

    </ul>

<form action="@routes.Application.addBar()" method="post">
    <label for="name"> Enter Your name</label>  <input name="name"/> <br>
    <label for="place"> Enter Your place</label>  <input name="place"/> <br>
    <input type="submit"/>
</form>

<form action="@routes.Application.getBars()" method="get">
    <label for="retrieve"> Retrieve the details from table </label>
      <input type="submit"/>
</form>

<form action="@routes.Application.deleteBars()" method="get">
    <label for="Delete"> Delete all the details from table </label>
      <input type="submit"/>
</form>

<form action="@routes.Application.updateBar()" method="post">
    <label for="name"> Enter Your name</label>  <input  name="name"/> <br>
    <label for="place"> Enter Your place</label>  <input name="place"/> <br>
    <input type="submit"/>
</form>
}

Application.java

package controllers;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.SqlUpdate;
import java.util.List;
import models.Bar;
import play.*;
import play.data.Form;
import play.db.ebean.Model;
import play.mvc.*;
import play.twirl.api.Content;
import views.html.*;
import play.libs.Json;
public class Application extends Controller {

    public static Result index() {
        int rowCount1 = Bar.find.findRowCount();

        return ok(index.render("Your new application is ready."));
    }

    public static Result addBar() {
        Bar bar = Form.form(Bar.class).bindFromRequest().get();
        bar.save();
        return redirect(routes.Application.index());
    }

    public static Result Login() { 
        Bar bar = Form.form(Bar.class).bindFromRequest().get();
        bar.save();
        return redirect(routes.Application.index());
    }
    public static Result getBars(){
        List<Bar> bars = new Model.Finder(String.class, Bar.class).all();
        return ok(Json.toJson(bars));
    }

    public static Result deleteBars() {
    SqlUpdate down = Ebean.createSqlUpdate("DELETE FROM bar");
    down.execute(); 
    return redirect(routes.Application.index());
    }

    public static Result updateBar() {

    SqlUpdate down = Ebean.createSqlUpdate("UPDATE bar SET place = 'asa'");
    down.execute(); 
    return ok();
    }    

}

路线如下:

GET     /                           controllers.Application.index()
POST    /bars2                      controllers.Application.addBar()
GET     /bars                       controllers.Application.getBars()
GET     /bars1                      controllers.Application.deleteBars()
POST     /updateBar/                controllers.Application.updateBar()
GET     /updateBar1/                controllers.Application.Login()

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)

我想做的是:

  1. 从HTML传递名称以进行删除,以便我可以删除记录。
  2. 传递名称,从HTML放置以进行更新。

1 个答案:

答案 0 :(得分:0)

嗯......您需要使用WHERE子句指出要删除的记录,如同每个常见的SQL statement

一样

<强>路线

GET /bar/:name/delete  controllers.Application.deleteBar(name: String)

<强> HTML

<form action='@routes.Application.deleteBar("foo")'>
    <input type="submit" value="Delete `foo`"/>
</form>

<强>动作

public static Result deleteBar(String name) {

    SqlUpdate down = Ebean.createSqlUpdate("DELETE FROM bar WHERE name = :param1 ");
    down.setParameter("param1", name);
    int deletedCount = down.execute();

    return ok("Deleted " + deletedCount + " record(s)");
}

你需要对UPDATE做同样的事情,尽管在Play中使用Form<T> class来映射模型可能更好 - 它可以帮助你进行数据验证。