鸿蒙应用开发—ZDbUtil高效使用数据库

文章目录

介绍

ZDbUtil是一款基于SQLite的鸿蒙数据库框架,通过注解标注实体类与属性,让数据更能抽象化简化原生RdbStore的使用。同时支持V1和V2状态管理管理。

SQLite在鸿蒙上的基本使用可以参考:鸿蒙应用开发---数据持久化之SQLite

下载安装

在每个har/hsp模块中,通过ohpm工具下载安装库:

复制代码
ohpm install @hzw/zdb

基本使用

注解

Table

表名为类名。

Id
  • 参数
    • primaryKey: 是否主键
    • autoIncrement: 是否自增
Column
  • 参数 :
    • type: 列类型,查看ColumnType
    • notNull: 是否为空
    • unique: 是否唯一
OneToOne
  • 参数 :
    • joinTable: 关联表的类
    • relatedIdProperty: 关联id属性名

使用方法

定义实体类

只要添加了@Table注解,@Id注解,@Column注解

在初始化数据库时,会自动创建表,并添加列。

typescript 复制代码
import { Column, ColumnType, Id, OneToOne, Table } from '@hzw/zdb';

// 分类
@Table()
export class StudentClassify {
  // id
  @Id({ primaryKey: true, autoIncrement: true })
  @Column({ type: ColumnType.Integer })
  id?: number
  // 名称
  @Column({ type: ColumnType.Text })
  name?: string
  // 创建时间
  @Column({ type: ColumnType.Integer })
  createTime?: number
  // 更新时间
  @Column({ type: ColumnType.Integer })
  editTime?: number | undefined
}

// 学生
@Table()
export class StudentInfo {
  // id
  @Id({ primaryKey: true, autoIncrement: true })
  @Column({ type: ColumnType.Integer })
  id?: number | undefined
  // 标题
  @Column({ type: ColumnType.Text })
  title?: string | undefined
  // 创建时间
  @Column({ type: ColumnType.Integer })
  createTime?: number | undefined
  // 是否管理员
  @Column({ type: ColumnType.Boolean })
  isManager?: boolean | undefined
  // 分类id
  @Column({ type: ColumnType.Integer })
  classifyId?: number | undefined
  // 分类
  @OneToOne({ joinTable: StudentClassify, relatedIdProperty: "classifyId" })
  classify?: StudentClassify | undefined
}
初始化数据库并根据被@Table注解的类创建表
typescript 复制代码
ZDbUtil.initDatabase({
  context: this.context
})
创建表

如果类不被@Table注解,则需要手动

typescript 复制代码
// 定义表的数据结构
const student: StudentInfo2 = {
  "id": 0,
  "title": "",
  "createTime": 0,
  "isManager": false,
  "classifyId": 0,
}
// 创建表
// 第一个参数是表的数据结构
// 第二个参数是表名
// 第三个参数是id名
ZDbUtil.initTableByName(student, "Student", "id").then(() => {
  promptAction.showToast({ message: "创建成功" });
})
查数据

如果查询的数据类型有被@Table注解,则通过这种方式查询数据

typescript 复制代码
ZDbUtil.querySqlBuilder<StudentClassify>(StudentClassify)
  .limitAs(this.pageSize)
  .offsetAs(this.page * this.pageSize)
    // ...
  .query()
  .then((data) => {
  })

否则通过这种方式查询数据

typescript 复制代码
// 定义表的数据结构
const student: StudentInfo2 = {
  "id": 0,
  "title": "",
  "createTime": 0,
  "isManager": false,
  "classifyId": 0,
}
// 定义关联表的数据结构
const classify: StudentClassify2 = {
  "id": 0,
  "name": "",
  "createTime": 0,
  "editTime": 0,
}
// 查询表
// 第一个参数是表的数据结构
// 第二个参数是表名
// 第三个参数是id名
ZDbUtil.querySqlObjBuilder(student, "Student", "id")
  // 关联表
  // 第一个参数是关联表的数据结构
  // 第二个参数是关联表的表名
  // 第三个参数是关联表的id名
  // 第四个参数是与关联表关联的id字段名
  // 第五个参数是存放关联表数据的字段名
  .initJoinTableInfo(classify, "Classify", "id", "classifyId", "classify")
  .limitAs(this.pageSize)
  .offsetAs(this.page * this.pageSize)
  .query()
  .then((data) => {
    this.list = data;
  })
插入数据

data类型确定,且被@Table注解,直接插入数据

typescript 复制代码
ZDbUtil.insertOrReplace(data).then(() => {
  promptAction.showToast({ message: "添加成功" });
})

data类型是Object,通过传入被@Table注解的类,插入数据

typescript 复制代码
// 插入数据
// 第一个参数为插入的数据
// 第二个参数为被@Table注解的类,数据将会被插入到与注解类关联的表中
ZDbUtil.insertOrReplaceByCls(data, StudentClassify).then(() => {
  promptAction.showToast({ message: "添加成功" });
})

data类型是Object,通过传入表名插入数据

typescript 复制代码
// 插入数据到数据库
// 第一个参数是插入的数据
// 第二个参数是表名
ZDbUtil.insertOrReplaceByName(data, "Student").then(() => {
  promptAction.showToast({ message: "添加成功" });
  this.loadData()
})
删除数据

data类型确定,且被@Table注解,直接删除数据

typescript 复制代码
ZDbUtil.delete(item).then(() => {
  promptAction.showToast({ message: "删除成功" });
})

data类型是Object,通过传入被@Table注解的类,删除数据

typescript 复制代码
ZDbUtil.deleteByCls(data, StudentInfo).then(() => {
  promptAction.showToast({ message: "删除成功" });
})

直接通过传入表名,id名,id的值删除数据

typescript 复制代码
// 第一个参数是表名
// 第二个参数是id名
// 第三个参数是id的值
ZDbUtil.deleteByName("Student", "id", item.id).then(() => {
  promptAction.showToast({ message: "删除成功" });
  this.loadData()
})
清空数据

根据被@Table注解的类,清空与之关联的表的所有数据

typescript 复制代码
ZDbUtil.clear(StudentInfo).then(() => {
  promptAction.showToast({ message: "清空成功" });
})

根据表名,清空该表的所有数据

typescript 复制代码
ZDbUtil.clearByName("Classify").then(() => {
  promptAction.showToast({ message: "清空成功" });
  this.selectClassify = undefined
  this.loadClassify()
  this.loadData()
})

参考

相关推荐
cyforkk3 分钟前
MySQL锁机制详解:从乐观锁到悲观锁
数据库·mysql
ShiXZ21313 分钟前
指令集-SQL 常用指令速查手册
数据库·sql·mysql·oracle
阿昭L38 分钟前
数据库期末复习(三)
数据库
Dlrb12111 小时前
信息查询Web服务器-->电子商城信息查询项目
linux·服务器·数据库·html·嵌入式·epoll·数据查询
不会写DN1 小时前
MySQL 并发插入竞态问题:原子写入实践指南
数据库·mysql
霖霖总总1 小时前
[MongoDB小技巧24]MongoDB $lookup 聚合阶段深度解析:从入门到实战
数据库·mongodb
挨踢诗人1 小时前
京东企业购 × 企业微信 采购闭环集成解决方案
前端·数据库·企业微信
梦想的旅途21 小时前
企业微信外部群智能自动回复:基于RPA接口的关键词匹配方案
java·开发语言·数据库·机器人·自动化·企业微信
得物技术2 小时前
得物 OceanBase 落地实践
数据库·分布式·架构
程序员cxuan2 小时前
OceanBase 为什么要做 AI 数据库?
数据库·人工智能·大模型·llm·oceanbase