博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
手把手教你在Flutter项目优雅的使用ORM数据库--下篇
阅读量:7008 次
发布时间:2019-06-28

本文共 6013 字,大约阅读时间需要 20 分钟。

An orm database Flutter plugin.

之前发了一篇文章,很多人咨询使用也提了一些宝贵的意见,说不希望要写lua,这样不够优雅,也增加了学习成本。细想了一下,确实是,对flutter项目开发来讲,最好就是纯flutter版的orm框架,于是我就写了一个flutter版的 ,使用的我放到github上了,大家可以下载来玩玩。下面我介绍一下flutter_orm_plugin提供的所有api。

添加orm表

flutter_orm_plugin中一个orm对应一个表,例如demo中Student表,它所在的db名字叫School,表名叫Student,它包含如下四个字段:

studentId 在数据库中是Integer类型,主键,自增的。

name 在数据库中是Text类型

class 在数据库中是Text类型,是外键,关联的表是另一个表Class表

score 在数据库中是Real类型

创建这样的表的代码在demo的中

Map
fields = new Map
(); fields["studentId"] = Field(FieldType.Integer, primaryKey: true , autoIncrement: true); fields["name"] = Field(FieldType.Text); fields["class"] = Field(FieldType.Text, foreignKey: true, to: "School_Class"); fields["score"] = Field(FieldType.Real); FlutterOrmPlugin.createTable("School","Student",fields);复制代码

数据库中某一列的数据通过Field类定义,我们先看看Field的定义就可以知道我们的orm对象支持哪些属性了

class Field {  final FieldType type;//类型包括Integer、Real、Blob、Char、Text、Boolean  bool unique;//是否惟一  int maxLength;  bool primaryKey;//是否主键  bool foreignKey;//是否外键  bool autoIncrement;//是否自增  String to;//关联外键表,以DBName_TableName命名  bool index;//是否有索引}复制代码

插入数据

单条插入

Map m = {
"name":"william", "class":"class1", "score":96.5}; FlutterOrmPlugin.saveOrm("Student", m);复制代码

批量插入

List orms = new List(); for(int i = 0 ; i < 100 ; i++) {      Map m = {
"name":name, "class":className, "score":score}; orms.add(m); } FlutterOrmPlugin.batchSaveOrms("Student", orms); 复制代码

查询数据

全部查询

Query("Student").all().then((List l) {  });  复制代码

查询第一条

Query("Student").first().then((Map m) {       });  复制代码

根据主键查询

Query("Student").primaryKey([1,3,5]).all().then((List l) {        });     复制代码

where条件查询

Query("Student").whereByColumFilters([WhereCondiction("score", WhereCondictionType.EQ_OR_MORE_THEN, 90)]).all().then((List l) {        });     复制代码

where sql 语句查询

Query("Student").whereBySql("class in (?,?) and score > ?", ["class1","class2",90]).all().then((List l) {        });     复制代码

where 查询并排序

Query("Student").orderBy(["score desc",]).all().then((List l) {        }); 复制代码

查询指定列

Query("Student").needColums(["studentId","name"]).all().then((List l) {        }); 复制代码

group by 、having 查询

Query("Student").needColums(["class"]).groupBy(["class"]).havingByBindings("avg(score) > ?", [40]).orderBy(["avg(score)"]).all().then((List l) {      }); 复制代码

更新数据

全部更新

Query("Student").update({
"name":"test all update"}); 复制代码

根据主键更新

Query("Student").primaryKey([11]).update({
"name":"test update by primary key"}); 复制代码

根据特定条件更新

Query("Student").whereByColumFilters([WhereCondiction("studentId", WhereCondictionType.LESS_THEN, 5),WhereCondiction("score", WhereCondictionType.EQ_OR_MORE_THEN, 0)]).update({
"score":100}); 复制代码

根据自定义where sql更新

Query("Student").whereBySql("studentId <= ? and score <= ?", [5,100]).update({
"score":0}); 复制代码

删除数据

全部删除

Query("Student").delete(); 复制代码

根据主键删除

Query("Student").primaryKey([1,3,5]).delete(); 复制代码

根据条件删除

Query("Student").whereByColumFilters([WhereCondiction("studentId", WhereCondictionType.IN, [1,3,5])]).delete();复制代码

根据自定义where sql删除

Query("Student").whereBySql("studentId in (?,?,?)", [1,3,5]).delete();复制代码

联表查询

inner join

JoinCondiction c = new JoinCondiction("Match");  c.type = JoinType.INNER;  c.matchColumns = {
"studentId": "winnerId"}; Query("Student").join(c).all().then((List l) { }); 复制代码

left join

JoinCondiction c = new JoinCondiction("Match");  c.type = JoinType.LEFT;  c.matchColumns = {
"studentId": "winnerId"}; Query("Student").join(c).all().then((List l) { }); 复制代码

利用外键联表

JoinCondiction c = new JoinCondiction("Class");  c.type = JoinType.INNER;  Query("Student").join(c).all().then((List l) {  });  复制代码

where sql 联表

JoinCondiction c = new JoinCondiction("Match");  c.type = JoinType.INNER;  c.matchColumns = {
"studentId": "winnerId"}; Query("Student").join(c).whereBySql("Student.score > ?",[60]).all().then((List l) { }); 复制代码

部分column 联表查询

JoinCondiction c = new JoinCondiction("Match");  c.type = JoinType.INNER;  c.matchColumns = {
"studentId": "winnerId"}; Query("Student").join(c).needColums(["name","score"]).all().then((List l) { }); 复制代码

group by 、having 联表查询

JoinCondiction c = new JoinCondiction("Class");  c.type = JoinType.INNER;  Query("Student").join(c).needColums(["class"]).groupBy(["Student.class"]).havingByBindings("avg(Student.score) > ?", [40]).all().then((List l) {              });    复制代码

order by 联表查询

JoinCondiction c = new JoinCondiction("Class");  c.type = JoinType.INNER;  Query("Student").join(c).orderBy(["Student.score desc"]).all().then((List l) {              });   复制代码

使用介绍

flutter_orm_plugin 已经发布到flutter 插件仓库。只要简单配置即可使用,在yaml文件中加上flutter_orm_plugin依赖以及orm框架所需要的lua源文件,flutter_orm_plugin会对所有lua代码进行封装,最终使用只需要关心dart接口,对lua是无感的。

flutter_orm_plugin: ^1.0.0    .  .  .    assets:    - packages/flutter_orm_plugin/lua/DB.lua    - packages/flutter_orm_plugin/lua/orm/model.lua    - packages/flutter_orm_plugin/lua/orm/cache.lua    - packages/flutter_orm_plugin/lua/orm/dbData.lua    - packages/flutter_orm_plugin/lua/orm/tools/fields.lua    - packages/flutter_orm_plugin/lua/orm/tools/func.lua    - packages/flutter_orm_plugin/lua/orm/class/fields.lua    - packages/flutter_orm_plugin/lua/orm/class/global.lua    - packages/flutter_orm_plugin/lua/orm/class/property.lua    - packages/flutter_orm_plugin/lua/orm/class/query.lua    - packages/flutter_orm_plugin/lua/orm/class/query_list.lua    - packages/flutter_orm_plugin/lua/orm/class/select.lua    - packages/flutter_orm_plugin/lua/orm/class/table.lua    - packages/flutter_orm_plugin/lua/orm/class/type.lua  复制代码

在ios项目podfile加上luakit 依赖

source 'https://github.com/williamwen1986/LuakitPod.git'source 'https://github.com/williamwen1986/curl.git'...pod 'curl', '~> 1.0.0'pod 'LuakitPod', '~> 1.0.17'复制代码

在android项目app的build.gradle加上luakit依赖

repositories {    maven { url "https://jitpack.io" }}...implementation 'com.github.williamwen1986:LuakitJitpack:1.0.9'复制代码

完成配置即可使用。

转载于:https://juejin.im/post/5cb453616fb9a0689c7ed6a4

你可能感兴趣的文章
我今天为了理想而开始了新的页面
查看>>
Linux Bash Shell日期格式化和计算
查看>>
微软:俄罗斯APT28间谍组织袭击欧洲政治机构
查看>>
企业如何克服混合云存储问题
查看>>
mysql和oracle用法的区别
查看>>
亿级日志平台之——ELK Stack实践
查看>>
前台传参到后台中文乱码解决方法
查看>>
Confluence 6 大致的用户规模示例
查看>>
静态路由原理和实验
查看>>
Docker架构、镜像和容器
查看>>
LNMP架构搭建(脚本)
查看>>
作为一名Java程序员的必修课+java_框架面试题(含答案)
查看>>
图片文字转word文档文字的方法
查看>>
idea在线生成注册码地址2018已经验证可用
查看>>
AJPFX关于StringBuffer,StringBuilder类总结(二)
查看>>
keepalived+lvs实现lvs的高可用
查看>>
linux下压缩与解压缩以及打包命令详解
查看>>
深入浅出JDBC(二) - Dbutils
查看>>
elasticsearch5.0 环境搭建
查看>>
redis pipe管道
查看>>