鸿蒙应用开发—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()
})

参考

相关推荐
程序新视界17 分钟前
三种常见的MySQL数据库设计最佳实践
数据库·后端·mysql
寒士obj21 分钟前
MyCat实现分库分表
数据库
Savvy..1 小时前
Redis 黑马点评-商户查询缓存
数据库·redis·缓存
可DRAK鸦|・ω・`)1 小时前
ArcGIS数据迁移问题汇总(postgresql)
数据库·postgresql
奶糖 肥晨1 小时前
批量重命名技巧:使用PowerShell一键整理图片文件命名规范
android·服务器·数据库
安卓开发者1 小时前
鸿蒙NEXT Wi-Fi扫描开发指南:从基础到实战
华为·harmonyos
数据与人1 小时前
MySQL 8.0 InnoDB ReplicaSet 完整配置指南与切换
数据库·mysql·adb
JIngJaneIL2 小时前
图书馆自习室|基于SSM的图书馆自习室座位预约小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·图书馆自习室
不要再敲了2 小时前
SSM框架下的redis使用以及token认证
数据库·spring boot·redis·缓存·mybatis
神的孩子都在歌唱3 小时前
PostgreSQL向量检索:pgvector入门指南
数据库·postgresql