使用playframework和Form <t>删除MYSQL中的记录

时间:2015-05-14 06:42:02

标签: 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.deleteBar()" method="get">
<label for="name"> Enter Your name to be deleted</label>  <input name="name"/> <br>
<input type="submit" value="Delete single record"/>
</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 deleteBar(String name) {

    SqlUpdate down = Ebean.createSqlUpdate("DELETE FROM bar WHERE name = :param1 ");
    down.setParameter("param1", bar.name);
    int deletedCount = down.execute();
    return ok("Deleted " + deletedCount + " record(s) " + bar.name);
}

}

路线如下:

GET     /                           controllers.Application.index()
POST    /bars2                      controllers.Application.addBar()
GET     /bars                       controllers.Application.getBars()
GET     /bars1                      controllers.Application.deleteBars()
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. 我正在使用DELETE的直接SQL查询,但我想知道我是如何实现相同的 映射模型的表单类 - 它也有助于数据验证。请提供伪代码......我经历了以下内容,但没有运气
  2. Form class link

1 个答案:

答案 0 :(得分:0)

您无需使用表单删除记录 - 为什么?你只需要搜索的标识符或参数,你有两种方法 - 首先是用SqlUpdate显示第二种方法是使用Ebean的方法:

List<Bar> bars = Bar.find.where().eq("name", name).findList();
for (Bar bar : bars) {
    bar.delete();
}

如果您想通过@Id字段删除单个记录,则可以执行此操作(如果您确定存在具有此ID的Bar):

Bar.find.ref(barId).delete();