Explorar el Código

新增插入返回id

chenritian hace 1 mes
padre
commit
89ec3e0f3a

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

@@ -12,7 +12,7 @@ export class RDBMapper<T> {
    * @param param
    * @param success
    */
-  insert(param: ESObject, success?: () => void) {
+  insert(param: ESObject, success?: (id:number) => void) {
     RelationalStoreUtils.insert(this.table, param, success)
   }
   /**

+ 10 - 6
features/feature/src/main/ets/view/ThirdView.ets

@@ -4,14 +4,17 @@ import { Pet, RDBMapper, RelationalStoreUtils, Test } from "basic";
 export struct ThirdView {
   @State list: Test[] = []
   @State list2: Pet[] = []
+  id1:number = 0
+  id2:number = 0
   petMapper:RDBMapper<Pet> = new RDBMapper(Pet)
   build() {
     Column() {
       Row() {
         Button('测试新增')
           .onClick(() => {
-            RelationalStoreUtils.insert(Test, { name: 'test1', age: 18 }, () => {
+            RelationalStoreUtils.insert(Test, { name: 'test1', age: 18 }, (id) => {
               console.log('insert success');
+              this.id1 = id
               let list = RelationalStoreUtils.getListSync(Test)
               this.list = list
               console.log('list:', JSON.stringify(list))
@@ -20,7 +23,7 @@ export struct ThirdView {
 
         Button('测试修改')
           .onClick(() => {
-            RelationalStoreUtils.updateItemById(Test, { id: 1, name: '测试1修改', age: 20 }, () => {
+            RelationalStoreUtils.updateItemById(Test, { id: this.id1, name: '测试1修改', age: 20 }, () => {
               console.log('insert success');
               let list = RelationalStoreUtils.getListSync(Test)
               this.list = list
@@ -29,7 +32,7 @@ export struct ThirdView {
           })
         Button('测试删除')
           .onClick(() => {
-            RelationalStoreUtils.deleteItemById(Test, 1)
+            RelationalStoreUtils.deleteItemById(Test, this.id1)
             console.log('insert success');
             let list = RelationalStoreUtils.getListSync(Test)
             this.list = list
@@ -47,8 +50,9 @@ export struct ThirdView {
       Row() {
         Button('测试新增2')
           .onClick(() => {
-            this.petMapper.insert( { name: 'test1', tag: '标签' }, () => {
+            this.petMapper.insert( { name: 'test1', tag: '标签' }, (id:number) => {
               console.log('insert success');
+              this.id2 = id
               let list = this.petMapper.getListSync()
               this.list2 = list
               console.log('list:', JSON.stringify(list))
@@ -57,7 +61,7 @@ export struct ThirdView {
 
         Button('测试修改2')
           .onClick(() => {
-            this.petMapper.updateItemById({ id: 1, name: '测试1修改',tag: '标签2' }, () => {
+            this.petMapper.updateItemById({ id: this.id2, name: '测试1修改',tag: '标签2' }, () => {
               console.log('insert success');
               let list = this.petMapper.getListSync()
               this.list2= list
@@ -66,7 +70,7 @@ export struct ThirdView {
           })
         Button('测试删除2')
           .onClick(() => {
-            this.petMapper.deleteItemByIdSync(1)
+            this.petMapper.deleteItemByIdSync(this.id2)
             console.log('insert success');
             let list = this.petMapper.getListSync()
             this.list2 = list