play框架之ORM
2024-08-27 12:01:49
初次接触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更细节的文档点这里。
最新文章
- oracle11g 重装操作系统后,如何利用原有oracle表空间文件还原数据库
- composer 一些使用说明
- VC 解密OUTLOOK pop3保存注册表密码
- 详解for循环(各种用法)
- 【原创】Android开发之ADB及ADB SHELl命令的应用
- [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I &; II (Populating Next Right Pointers in Each Node I &; II)
- SIFT解析(二)特征点位置确定
- 一文让你明白Redis主从同步
- [Swift]LeetCode697. 数组的度 | Degree of an Array
- 多线程shell脚本检测主机存活
- B站(Bilibili) 视频的下载。
- iOS 模拟器运行不能联网 PAC Fetch failed with error
- SQL service 中的 ”输入SQL命令窗口“ 打开了 “属性界面” 回到 ”输入SQL命令窗口“
- Latex数学公式编写
- .NET:System.Security.Cryptography.CryptographicException 的解决办法
- TensorFlow-GPU:查看电脑显卡型号是否支持CUDN,以及相关软件下载与介绍
- 解决MVC 时间序列化的方法
- RNA-seq要做几次生物学重复?找出来的100%都是真正的应答基因
- 《JavaScript网页特效经典300例》
- JQuery实现表格的全选和反选,以及分页勾选保存(laypage插件分页的使用)