zoukankan      html  css  js  c++  java
  • play框架之ORM

    初次接触play2,采用的ebeans作为ORM框架。网上的资料并不多,总结如下:

    数据的查询可以放在model类里,也可以放在controllers里面,我更倾向于后者,感觉数据流比较完整,好理解,好维护。

    1.models操纵数据库

    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中操作数据库:

        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.从表单获取数据存入数据库

        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的表单也很简单有意思,我会在其他博文专门讲它。

    Ebeans更细节的文档点这里。

  • 相关阅读:
    linux查看硬件信息及驱动设备相关整理
    qt 画图相关
    glaux.h vs2008
    技巧:Vimdiff 使用
    Linux下显示硬件信息(二)lshw
    Linux下硬件信息查看(一)dmidecode
    qt delegate
    RFKill 【解决opensuse11.4 无法开启无线网的问题】
    板子上运行提示 找不到qml插件
    CRISPDM
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/6530579.html
Copyright © 2011-2022 走看看