Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Latest commit

 

History

History
88 lines (67 loc) · 2.24 KB

record.md

File metadata and controls

88 lines (67 loc) · 2.24 KB

database

Eloquent ORM for Java

目录

总览

使用查询构造器或者ORM查询数据库记录时, 返回的类型一般为

  • 单条记录gaarason.database.eloquent.Record<T>
  • 多条记录gaarason.database.eloquent.RecordList<T>
  • 受影响的行数int

普通java对象

gaarason.database.eloquent.Record<T>通过toObject可以转化为对应的泛型实体
gaarason.database.eloquent.RecordList<T>通过toObjectList可以转化为对应的泛型实体列表

通用map对象

gaarason.database.eloquent.Record<T>通过toMap可以转化为Map<String, Object>
gaarason.database.eloquent.RecordList<T>通过toMapList可以转化List<Map<String, Object>>

ORM

对于gaarason.database.eloquent.Record<T>对象提供ORM相关的能力
所有操作均可触发数据模型事件

查询

// 查找id=3的记录, 记录不存在则抛出`EntityNotFoundException`异常
Record<Student> record = studentModel.findOrFail("3");

// 查找id=3的记录, 记录不存在则返回null
Record<Student> record = studentModel.newQuery().where("id","3").first();

更新

// 先获取record
Record<Student> record = studentModel.findOrFail("3");

// 此处不应使用 toObject() 获取具体泛型对象
Student student = record.getEntity();

// 设置属性
student.setName("肖邦");

// 保存
record.save();

新增

// 先获取新的 record
Record<Student> record = studentModel.newRecord();

// 此处不应使用 toObject() 获取具体泛型对象
Student student = record.getEntity();

// 设置属性
student.setName("肖邦");

// 保存
record.save();

删除

// 先获取 record
Record<Student> record = studentModel.findOrFail("3");

// 删除
record.delete();