您好我是Play framework的新手。我想在播放框架中使用MYSQL數據庫執行CRUD操作。使用playframework刪除MYSQL中的記錄和表格<T>
我已經能夠成功刪除,更新整個表,但無法成功刪除單個記錄。我想傳遞來自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)
我想要做的是:
- 我使用直接的SQL查詢刪除,但我想知道我可以如何實現相同的 映射模型的表單類 - 它也有助於數據驗證。請提供的僞代碼...我通過下面的東西去了,但沒有運氣
deleteBar方法是使用PARAM,但你沒有在路線和方法deleteBar名PARAM定義不使用 – Premraj