鸿蒙应用开发—ZDbUtil高效使用数据库
文章目录
- 介绍
- 下载安装
- 基本使用
- 注解
- Table
- Id
- Column
- OneToOne
- 使用方法
- 定义实体类
- 初始化数据库并根据被@Table注解的类创建表
- 创建表
- 查数据
- 插入数据
- 删除数据
- 清空数据
- 参考
介绍
ZDbUtil是一款基于SQLite的鸿蒙数据库框架,通过注解标注实体类与属性,让数据更能抽象化简化原生RdbStore的使用。同时支持V1和V2状态管理管理。
SQLite在鸿蒙上的基本使用可以参考:鸿蒙应用开发—数据持久化之SQLite
下载安装
在每个har/hsp模块中,通过ohpm工具下载安装库:
ohpm install @hzw/zdb
基本使用
注解
Table
表名为类名。
Id
- 参数
primaryKey
: 是否主键autoIncrement
: 是否自增
Column
- 参数:
type
: 列类型,查看ColumnTypenotNull
: 是否为空unique
: 是否唯一
OneToOne
- 参数:
joinTable
: 关联表的类relatedIdProperty
: 关联id属性名
使用方法
定义实体类
只要添加了@Table
注解,@Id
注解,@Column
注解
在初始化数据库时,会自动创建表,并添加列。
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注解的类创建表
ZDbUtil.initDatabase({
context: this.context
})
创建表
如果类不被@Table注解,则需要手动
// 定义表的数据结构
const student: StudentInfo2 = {
"id": 0,
"title": "",
"createTime": 0,
"isManager": false,
"classifyId": 0,
}
// 创建表
// 第一个参数是表的数据结构
// 第二个参数是表名
// 第三个参数是id名
ZDbUtil.initTableByName(student, "Student", "id").then(() => {
promptAction.showToast({ message: "创建成功" });
})
查数据
如果查询的数据类型有被@Table注解,则通过这种方式查询数据
ZDbUtil.querySqlBuilder<StudentClassify>(StudentClassify)
.limitAs(this.pageSize)
.offsetAs(this.page * this.pageSize)
// ...
.query()
.then((data) => {
})
否则通过这种方式查询数据
// 定义表的数据结构
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注解,直接插入数据
ZDbUtil.insertOrReplace(data).then(() => {
promptAction.showToast({ message: "添加成功" });
})
data类型是Object,通过传入被@Table注解的类,插入数据
// 插入数据
// 第一个参数为插入的数据
// 第二个参数为被@Table注解的类,数据将会被插入到与注解类关联的表中
ZDbUtil.insertOrReplaceByCls(data, StudentClassify).then(() => {
promptAction.showToast({ message: "添加成功" });
})
data类型是Object,通过传入表名插入数据
// 插入数据到数据库
// 第一个参数是插入的数据
// 第二个参数是表名
ZDbUtil.insertOrReplaceByName(data, "Student").then(() => {
promptAction.showToast({ message: "添加成功" });
this.loadData()
})
删除数据
data类型确定,且被@Table注解,直接删除数据
ZDbUtil.delete(item).then(() => {
promptAction.showToast({ message: "删除成功" });
})
data类型是Object,通过传入被@Table注解的类,删除数据
ZDbUtil.deleteByCls(data, StudentInfo).then(() => {
promptAction.showToast({ message: "删除成功" });
})
直接通过传入表名,id名,id的值删除数据
// 第一个参数是表名
// 第二个参数是id名
// 第三个参数是id的值
ZDbUtil.deleteByName("Student", "id", item.id).then(() => {
promptAction.showToast({ message: "删除成功" });
this.loadData()
})
清空数据
根据被@Table注解的类,清空与之关联的表的所有数据
ZDbUtil.clear(StudentInfo).then(() => {
promptAction.showToast({ message: "清空成功" });
})
根据表名,清空该表的所有数据
ZDbUtil.clearByName("Classify").then(() => {
promptAction.showToast({ message: "清空成功" });
this.selectClassify = undefined
this.loadClassify()
this.loadData()
})
参考
- https://blog.csdn.net/xiaolang555w_w/article/details/145677007
- 源码:https://gitee.com/HW-Commons/ZDbUtil
- OH仓库:https://ohpm.openharmony.cn/#/cn/detail/@hzw%2Fzdb