Play Framework 1.4 学习笔记 Model类型,Hibernate持久化,文件上传

Play的Model部分会自动生成getter和setter

当定义这样的类时

public class Product {
	public String name;
	public Integer price;
}

加载的类将是

public class Product {
 
    public String name;
    public Integer price;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getPrice() {
        return price;
    }
 
    public void setPrice(Integer price) {
        this.price = price;
    }
}

然后如果想要访问属性时:

product.name = "My product";
product.price = 58;

加载时会转换为

product.setName("My product");
product.setPrice(58);

如果有需要,可以自定义getter和setter方法

使用Hibernate持久化对象模型

  • 只需要加一个注解@Entity
  • Play提供了支持来处理JPA,需要继承play.db.jpa.Model

事务管理

  • 如果需要强制回滚,调用JPA.setRollbackOnly()
  • 在Controller中的方法加@Transctional(readOnly=true)表示只读
  • 如果不想开启事务,可以使用@NoTransaction,也可以加在类上
  • 使用@NoTransaction时,不会从连接池获取连接,从而提高速度

play.db.jpa.Model类

  • 继承该类会自动生成一个Long类型的id并作为主键

GenericMode类

  • 如果不想用Model的id,可以继承此类,自定义id类型
@Entity
public class User extends GenericModel {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    public String id;

    @Required
    public String name;

    @Required
    @MaxSize(value=255, message = "email.maxsize")
    @play.data.validation.Email
    public String mail;
}

Model提供的查询方法

  • 通过id查找
Post aPost = Post.findById(5L);
  • 查询所有,分页查询
List<Post> posts = Post.findAll();
// 等同于
List<Post> posts = Post.all().fetch();
// 进行分页:表示从第50条数据开始查询100条
List<Post> posts = Post.all().from(50).fetch(100);
  • 条件查询
Post.find("byTitle", "My first post").fetch();
Post.find("byTitleLike", "%hello%").fetch();
Post.find("byAuthorIsNull").fetch();
Post.find("byTitleLikeAndAuthor", "%hello%", connectedUser).fetch();
  • 条件查询遵循以下语法
    LessThan -小于给定值
    LessThanEquals -小于或等于给定值
    GreaterThan -大于给定值
    GreaterThanEquals -大于或等于给定值
    Like -等同于类似SQL的表达式,但该属性将始终转换为小写。
    Ilike -与Like相似,但不区分大小写,这意味着您的参数也将转换为小写。
    Elike -等同于类似SQL的表达式,无需转换。
    NotEqual -不相等
    Between -两个值之间(需要两个参数)
    IsNotNull -不是空值(不需要参数)
    IsNull -为空值(不需要参数)
  • 使用JPQL查询
Post.find(
    "select p from Post p, Comment c " +
    "where c.post = p and c.subject like ?", "%hop%"
);
// 可以简写
Post.find("title", "My first post").fetch();
Post.find("title like ?", "%hello%").fetch();
Post.find("author is null").fetch();
Post.find("title like ? and author is null", "%hello%").fetch();
Post.find("title like ? and author is null order by postDate", "%hello%").fetch();

Count计数

long postCount = Post.count();
long userPostCount = Post.count("author = ?", connectedUser);

文件上传

  • 使用Blob类存储,使用randerBinary()进行显示
public class User extends Model {
 
   public String name;
   public Blob photo;
}
----------------------------------------------
#{form @addUser(), enctype:'multipart/form-data'}
   <input type="file" name="user.photo">
   <input type="submit" name="submit" value="Upload">
#{/form}
----------------------------------------------
public static void addUser(User user) {
   user.save();
   index();
}
----------------------------------------------
public static void file() {
   User user = User.find("name", "a").first();
   renderBinary(user.photo.get());
}

详细内容参考官方文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值