【问题标题】:DELETE, UPDATE a single record in mysql using playframework使用playframework删除,更新mysql中的单个记录
【发布时间】:2015-05-13 06:34:17
【问题描述】:

您好,我是 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>
}

应用程序.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 中传递名称、位置以进行更新。

【问题讨论】:

    标签: java mysql playframework


    【解决方案1】:

    嗯...您需要使用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&lt;T&gt; class 映射模型可能会更好 - 它会帮助您进行数据验证。

    【讨论】:

    • 谢谢。我如何将 foo 从文本框中传递到 @routes.Application.deleteBar(?)。 2. Form Class 的任何例子
    • ad.2 只需在答案中点击它,我在那里链接了一个文档
    猜你喜欢
    • 1970-01-01
    • 2015-02-13
    • 2019-05-26
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 2023-03-30
    相关资源
    最近更新 更多