gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:43:57 24 4
gpt4 key购买 nike

您好,我是 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 传递名称、位置以进行更新。

最佳答案

嗯...你需要使用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>

Action

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 可能更好。用于映射模型 - 它将帮助您进行数据验证。

关于java - 使用playframework删除,更新mysql中的单个记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30207300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com