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更细节的文档点这里。

posted @   Mars.wang  阅读(729)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示