Browse Source

新增mapper维护单表数据

chenritian 1 tháng trước cách đây
mục cha
commit
f82f78bc3d

+ 6 - 2
commons/basic/Index.ets

@@ -1,11 +1,15 @@
-export { RelationalStoreUtils } from './src/main/ets/rdb/utils/RelationalStoreUtis'
-export { Test } from './src/main/ets/rdb/tables/Test'
+export { Pet } from './src/main/ets/rdb/tables/Pet'
 
+export { RDBMapper } from './src/main/ets/rdb/utils/RDBMapper'
 
+export { RelationalStoreUtils } from './src/main/ets/rdb/utils/RelationalStoreUtis'
 
+export { Test } from './src/main/ets/rdb/tables/Test'
 
 export { registerFontUtil } from './src/main/ets/utils/RegisterFontUtil'
+
 export { RouterPage } from './src/main/ets/ts'
+
 export { YTToast } from './src/main/ets/utils/YTToast'
 
 export { ContextHelper } from './src/main/ets/utils/ContextHelper'

+ 1 - 0
commons/basic/src/main/ets/rdb/sqls/RDBSql.ts

@@ -1,3 +1,4 @@
 export const rDbSql = {
+  CREATE_PET_TABLE: 'CREATE TABLE IF NOT EXISTS Pet (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, tag TEXT)',
   CREATE_TEST_TABLE: 'CREATE TABLE IF NOT EXISTS Test (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)'
 }

+ 5 - 0
commons/basic/src/main/ets/rdb/tables/Pet.ets

@@ -0,0 +1,5 @@
+export class Pet{
+  id?: number;
+  name?: string;
+  tag?: string;
+}

+ 80 - 0
commons/basic/src/main/ets/rdb/utils/RDBMapper.ts

@@ -0,0 +1,80 @@
+import { RelationalStoreUtils } from './RelationalStoreUtis';
+
+export class RDBMapper<T> {
+  table: new () => T;
+
+  constructor(table: new () => T) {
+    this.table = table;
+  }
+
+  /**
+   * 插入数据 --id不用传,数据库自增
+   * @param param
+   * @param success
+   */
+  insert(param: ESObject, success?: () => void) {
+    RelationalStoreUtils.insert(this.table, param, success)
+  }
+  /**
+   * 插入数据 --id不用传,数据库自增
+   * @param param
+   * @param success
+   */
+  insertSync(param: ESObject) {
+    return RelationalStoreUtils.insertSync(this.table, param)
+  }
+
+  /**
+   * 更新数据
+   * @param param param 要有id
+   * @param success
+   */
+  updateItemById(param: ESObject, success?: () => void) {
+    RelationalStoreUtils.updateItemById(this.table, param, success)
+  }
+
+  /**
+   * 更新数据
+   * @param param param 要有id
+   * @param success
+   */
+  updateItemByIdSync(param: ESObject) {
+    RelationalStoreUtils.updateItemByIdSync(this.table, param)
+  }
+
+  /**
+   * 删除数据
+   * @param id
+   * @param success
+   */
+  deleteItemById(id: number, success?: () => void) {
+    RelationalStoreUtils.deleteItemById(this.table, id, success)
+  }
+
+  /**
+   * 删除数据
+   * @param id
+   * @param success
+   */
+  deleteItemByIdSync(id: number) {
+    return RelationalStoreUtils.deleteItemByIdSync(this.table, id)
+  }
+
+  /**
+   * 根据id查询数据
+   * @param queryParam
+   * @returns
+   */
+  getItemByIdSync(queryParam: ESObject) {
+    return RelationalStoreUtils.getItemByIdSync(this.table, queryParam)
+  }
+
+  /**
+   * 查询数据
+   * @param queryParam
+   * @returns
+   */
+  getListSync(queryParam?: ESObject): T[] {
+    return RelationalStoreUtils.getListSync(this.table, queryParam)
+  }
+}

+ 1 - 1
commons/basic/src/main/ets/rdb/utils/RelationalStoreUtis.ts

@@ -44,8 +44,8 @@ export class RelationalStoreUtils {
         }
         // 设置数据库的版本,入参为大于0的整数
         store.version = 3;
-        RelationalStoreUtils.cloudLoadStore = store;
       }
+      RelationalStoreUtils.cloudLoadStore = store;
       success?.()
     })
   }

+ 40 - 1
features/feature/src/main/ets/view/ThirdView.ets

@@ -1,8 +1,10 @@
-import { RelationalStoreUtils, Test } from "basic";
+import { Pet, RDBMapper, RelationalStoreUtils, Test } from "basic";
 
 @Component
 export struct ThirdView {
   @State list: Test[] = []
+  @State list2: Pet[] = []
+  testMapper:RDBMapper<Pet> = new RDBMapper(Pet)
   build() {
     Column() {
       Row() {
@@ -42,6 +44,43 @@ export struct ThirdView {
         .font({ size: 20 })
         .margin({ bottom: 20 })
 
+      Row() {
+        Button('测试新增2')
+          .onClick(() => {
+            this.testMapper.insert( { name: 'test1', tag: '标签' }, () => {
+              console.log('insert success');
+              let list = this.testMapper.getListSync()
+              this.list2 = list
+              console.log('list:', JSON.stringify(list))
+            })
+          })
+
+        Button('测试修改2')
+          .onClick(() => {
+            this.testMapper.updateItemById({ id: 1, name: '测试1修改',tag: '标签2' }, () => {
+              console.log('insert success');
+              let list = this.testMapper.getListSync()
+              this.list2= list
+              console.log('list:', JSON.stringify(list))
+            })
+          })
+        Button('测试删除2')
+          .onClick(() => {
+            this.testMapper.deleteItemByIdSync(1)
+            console.log('insert success');
+            let list = this.testMapper.getListSync()
+            this.list2 = list
+            console.log('list:', JSON.stringify(list))
+          })
+      }
+      .width('100%')
+      .justifyContent(FlexAlign.SpaceAround)
+      .margin({ bottom: 30 })
+
+      Text(JSON.stringify(this.list2))
+        .font({ size: 20 })
+        .margin({ bottom: 20 })
+
     }
     .justifyContent(FlexAlign.Center)
     .alignItems(HorizontalAlign.Center)