初次接觸play2,採用的ebeans做爲ORM框架。網上的資料並很少,總結以下:html
數據的查詢能夠放在model類裏,也能夠放在controllers裏面,我更傾向於後者,感受數據流比較完整,好理解,好維護。java
1.models操縱數據庫ios
package models; import java.util.Date; import java.util.List; import javax.persistence.Entity; import javax.persistence.Id; import play.db.ebean.Model; import play.db.ebean.Model.Finder; import play.data.format.*; /** * Created by wangbin10 on 2017/1/5. */ @Entity public class AppActive extends Model{ @Id public Integer id; @Formats.DateTime(pattern="yyyy-MM-dd") public Date push_date; public Integer adr_rate; public Integer ios_rate; public static Finder<Integer,AppActive> find=new Finder<Integer,AppActive>(Integer.class,AppActive.class); public static List<AppActive> findAll(){ return find.all(); } public static List<AppActive> findFactor(String start_date,String end_date){ return find.where().eq("push_date","2016-12-30").findList(); return find.where().ne("push_date","2016-12-31").findList(); return find.where().in("push_date",daterange).findList(); return find.where().gt("push_date","2016-12-29").findList(); return find.where().ge("push_date","2016-12-29").findList(); return find.where().lt("push_date","2016-12-31").findList(); return find.where().le("push_date","2016-12-31").findList(); return find.where().gt("push_date","2016-12-29").le("push_date","2016-12-31").findList(); return find.where().like("push_date","2016-12-3%").findList(); return find.where().between("push_date",start_date,end_date).findList(); } }
2.controllers中操做數據庫:git
public static Result app_active(){ Form<DateForm> daterange=Form.form(DateForm.class); DynamicForm in = Form.form().bindFromRequest(); if(in.get("start_date")==null){ String start_date = "2016-12-29"; String end_date = "2017-01-01"; List<AppActive> actives=AppActive.find.where().between("push_date",start_date,end_date).findList(); List<Long> push_date=new ArrayList<Long>(); List<Integer> adr_rate=new ArrayList<Integer>(); List<Integer> ios_rate=new ArrayList<Integer>(); for(AppActive active:actives){ push_date.add((active.push_date).getTime()); adr_rate.add((Integer) active.adr_rate); ios_rate.add((Integer) active.ios_rate); } return ok(views.html.app_active.render(push_date,adr_rate,ios_rate,actives,daterange)); }else { String start_date = in.get("start_date"); String end_date = in.get("end_date"); List<AppActive> actives = AppActive.find.where().between("push_date", start_date, end_date).findList(); List<Long> push_date = new ArrayList<Long>(); List<Integer> adr_rate = new ArrayList<Integer>(); List<Integer> ios_rate = new ArrayList<Integer>(); for (AppActive active : actives) { push_date.add((active.push_date).getTime()); adr_rate.add((Integer) active.adr_rate); ios_rate.add((Integer) active.ios_rate); } return ok(views.html.app_active.render(push_date, adr_rate, ios_rate, actives, daterange)); } }
3.從表單獲取數據存入數據庫github
public static Result postRegister(){ Form<Registration> userForm=Form.form(Registration.class).bindFromRequest(); User user=new User(userForm.get().email,userForm.get().password); user.save(); return ok("registered"); }
對應的HTML表單代碼以下:數據庫
@(userForm: Form[controllers.Application.Registration]) <!DOCTYPE html> <html> <body> <h1> Registration </h1> @helper.form(action = routes.Application.postRegister()) { @helper.inputText(userForm("email")) @helper.inputPassword(userForm("password")) <input type="submit"> } </body> </html>
play的表單也很簡單有意思,我會在其餘博文專門講它。app
Ebeans更細節的文檔點這裏。框架