HarmonyOS基础技术赋能之对象关系映射数据库的使用

数据库 其他数据库 OpenHarmony
HarmonyOS对象关系映射(Object Relational Mapping,ORM)数据库是一款基于SQLite的数据库框架,屏蔽了底层SQLite数据库的SQL操作,针对实体和关系提供了增删改查等一系列的面向对象接口。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

引言

HarmonyOS对象关系映射(Object Relational Mapping,ORM)数据库是一款基于SQLite的数据库框架,屏蔽了底层SQLite数据库的SQL操作,针对实体和关系提供了增删改查等一系列的面向对象接口。应用开发者不必再去编写复杂的SQL语句, 以操作对象的形式来操作数据库,提升效率的同时也能聚焦于业务开发。

功能介绍

对象关系映射数据库目前可以支持数据库和表的创建,对象数据的增删改查、对象数据变化回调、数据库升降级和备份等功能。

开发指南

1. 配置“build.gradle”文件。

//在ohos节点中添加以下配置:

  1. compileOptions{         
  2.  
  3.     annotationEnabled true     
  4.  
  5. }  

2. 构造数据库。

//例如,定义了一个数据库类BookStore.java,数据库包含了“User”一张表,版本号为“1”。数据库类的getVersion方法和getHelper方法不需要实现,直接将数据库类设为虚类即可。

  1. @Database(entities = {User.class}, version = 1) 
  2. public abstract class BookStore extends OrmDatabase { 
  3.  

3.构造数据表。

//创建数据库实体类并配置对应的属性(如对应表的主键,外键等

  1.  @Entity(tableName = "user"
  2. public class User extends OrmObject { 
  3. // 此处将userId设为了自增的主键。注意只有在数据类型为包装类型时,自增主键才能生效。 
  4.   @PrimaryKey(autoGenerate = true
  5.   private Integer userId; 
  6.   private String userName; 
  7.   private int userAge; 
  8.   private String userRole; 
  9.  
  10.   public User() { 
  11.   } 
  12.  
  13.   // 需添加各字段的getter和setter方法。 
  14.   …… 
  15.   …… 

4. 使用对象数据操作接口OrmContext创建数据库。

  1. // context入参类型为ohos.app.Context,注意不要使用slice.getContext()来获取context,请直接传入slice,否则会出现找不到类的报错。 
  2. DatabaseHelper helper = new DatabaseHelper(this);  
  3. OrmContext context = helper.getOrmContext("BookStore""BookStore.db", BookStore.class);  

5. 使用对象数据操作接口OrmContext对数据库进行增删改查、注册观察者、备份数据库等。

1)首先需要创建一个DataAbility. 右键项目包名→New→Ability→Empty Data Ability,点击进入configure Ability,然后自己定义Data Name,例如:UserDataAbilty。

HarmonyOS基础技术赋能之对象关系映射数据库的使用-鸿蒙HarmonyOS技术社区
HarmonyOS基础技术赋能之对象关系映射数据库的使用-鸿蒙HarmonyOS技术社区

2)然后在UserDataAbilty类中重写增删改查的方法:

  1. //先在onstart()方法中创建OrmContext对象,context入参类型为ohos.app.Context,注意不要使用slice.getContext()来获取context,请直接传入slice,否则会出现找不到类的报错。 
  2. DatabaseHelper helper = new DatabaseHelper(this);  
  3. OrmContext context = helper.getOrmContext("BookStore""BookStore.db", BookStore.class);  
  4. // 然后重新增删改查方法 query()/insert()/update()/delete() 
  5. @Override 
  6. public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) { 
  7.   if(ormContext == null){ 
  8.     HiLog.error(LABEL_LOG,"failed to query, ormContext is null"); 
  9.     return null
  10.   } 
  11.   //查询数据库 
  12.   OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, User.class); 
  13.   ResultSet resultSet = ormContext.query(ormPredicates,columns); 
  14.   if (resultSet == null){ 
  15.     HiLog.info(LABEL_LOG,"resultSet is null"); 
  16.   } 
  17.   return resultSet; 
  18.  
  19. @Override 
  20. public int insert(Uri uri, ValuesBucket value) { 
  21.   // 参数校验 
  22.   if (ormContext == null) { 
  23.     HiLog.error(LABEL_LOG, "failed to insert, ormContext is null"); 
  24.     return -1; 
  25.   } 
  26.  
  27.   // 构造插入数据 
  28.   User user = new User(); 
  29.   user.setUserId(value.getInteger("userId")); 
  30.   user.setUserName(value.getString("userName")); 
  31.   user.setUserAge(value.getInteger("userAge")); 
  32.   user.setUserRole(value.getString("userRole")); 
  33.  
  34.   // 插入数据库 
  35.   boolean isSuccessed; 
  36.   isSuccessed = ormContext.insert(user); 
  37.   if (!isSuccessed) { 
  38.     HiLog.error(LABEL_LOG, "failed to insert"); 
  39.     return -1; 
  40.   } 
  41.   isSuccessed = ormContext.flush(); 
  42.   if (!isSuccessed) { 
  43.     HiLog.error(LABEL_LOG, "failed to insert flush"); 
  44.     return -1; 
  45.   } 
  46.   DataAbilityHelper.creator(this, uri).notifyChange(uri); 
  47.   //返回的id,为数据表自增主键id 
  48.   int id = Math.toIntExact(user.getRowId()); 
  49.   HiLog.debug(LABEL_LOG, "success to insert id="+id); 
  50.   return id; 
  51.  
  52. @Override 
  53. public int delete(Uri uri, DataAbilityPredicates predicates) { 
  54.   if (ormContext == null) { 
  55.     HiLog.error(LABEL_LOG, "failed to delete, ormContext is null"); 
  56.     return -1; 
  57.   } 
  58.  
  59.   OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates,User.class); 
  60.   int result = ormContext.delete(ormPredicates); 
  61.   DataAbilityHelper.creator(this, uri).notifyChange(uri); 
  62.   // result>0 表示删除成功,result<=0 表示删除失败 
  63.   if(result>0){ 
  64.     HiLog.debug(LABEL_LOG, "UserDataAbility  success to delete value="+result); 
  65.   }else { 
  66.     HiLog.debug(LABEL_LOG, "UserDataAbility  failed to delete value="+result); 
  67.   } 
  68.  
  69.   return result; 
  70.  
  71. @Override 
  72. public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) { 
  73.   if (ormContext == null) { 
  74.     HiLog.error(LABEL_LOG, "failed to update, ormContext is null"); 
  75.     return -1; 
  76.   } 
  77.  
  78.   OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates,User.class); 
  79.   int result = ormContext.update(ormPredicates, value); 
  80.   // result>0 表示更新成功,result<=0 表示更新失败 
  81.   if(result>0){ 
  82.     HiLog.info(LABEL_LOG, "UserDataAbility success to update value:" + result); 
  83.   }else { 
  84.     HiLog.info(LABEL_LOG, "UserDataAbility failed to update value:" + result); 
  85.  
  86.   } 
  87.   DataAbilityHelper.creator(this, uri).notifyChange(uri); 
  88.   return result; 

6.在需要调用的地方,例如MainAbilitySlice中,先创建DataAbilityHelper 对象,然后调用DataAbilityHelper的增、删、改、查方法。

  1. //创建DataAbilityHelper 
  2. DataAbilityHelper helper = DataAbilityHelper.creator(this); 
  3.  
  4. //插入数据 
  5. //其中Uri uri = Uri.parse(String Uri);里面的配置字符串Uri就是用于读取关系映射数据库的路径,来自于创建DataAbility后,在config.json中的Uri标签,本案例中的String Uri= “dataability://com.isoftstone.ormdatebase.UserDataAbility” 
  6.  
  7. helper.insert(uri, valuesBucket); 
  8.  
  9. //删除数据 
  10. helper.delete(uri,predicates); 
  11.  
  12. //修改数据 
  13. helper.update(uri, valuesBucket, predicates) 
  14.  
  15. //查询数据 
  16. ResultSet resultSet = helper.query(uri, columns, predicates) 

操作步骤

App打开之后:

HarmonyOS基础技术赋能之对象关系映射数据库的使用-鸿蒙HarmonyOS技术社区

1.点击QueryAll时,打印的log。(查询插入的“ZhangSan”、“LiSi”)

  1. 08-25 15:50:00.966 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: userId=1; userName=ZhangSan; userAge=22; userRole=teacher 
  2.  
  3. 08-25 15:50:00.967 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: userId=2; userName=LiSi; userAge=23; userRole=programmer 

2.点击QueryByCondition时,打印的log。(条件查询“userRole=programmer”)

  1. 08-25 15:51:29.031 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: userId=2; userName=LiSi; userAge=23; userRole=programmer 

3.点击Insert后,再点击QueryAll时,打印的log。(插入“WangWu”)

  1. 08-25 15:52:26.426 10496-10496/com.isoftstone.ormdatebase D 01100/Demo: success to insert id=3 
  2.  
  3. 08-25 15:52:31.466 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: userId=1; userName=ZhangSan; userAge=22; userRole=teacher 
  4.  
  5. 08-25 15:52:31.466 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: userId=2; userName=LiSi; userAge=23; userRole=programmer 
  6.  
  7. 08-25 15:52:31.467 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: userId=3; userName=WangWu; userAge=18; userRole=police 

4.点击Update后,再点击QueryAll时,打印的log。(把“WangWu”修改为“ZhaoLiu”)

  1. 08-25 15:53:42.641 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: UserDataAbility success to update value:1 
  2.  
  3. 08-25 15:53:42.643 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: 1 
  4.  
  5. 08-25 15:54:35.062 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: userId=1; userName=ZhangSan; userAge=22; userRole=teacher 
  6.  
  7. 08-25 15:54:35.063 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: userId=2; userName=LiSi; userAge=23; userRole=programmer 
  8.  
  9. 08-25 15:54:35.063 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: userId=3; userName=ZhaoLiu; userAge=26; userRole=doctor 

5.点击Delete后,在点击QueryAll时,打印的log。(删除“ZhaoLiu”)

  1. 08-25 15:55:26.885 10496-10496/com.isoftstone.ormdatebase D 01100/Demo: UserDataAbility success to delete value=1 
  2.  
  3. 08-25 15:55:29.157 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: userId=1; userName=ZhangSan; userAge=22; userRole=teacher 
  4.  
  5. 08-25 15:55:29.158 10496-10496/com.isoftstone.ormdatebase I 01100/Demo: userId=2; userName=LiSi; userAge=23; userRole=programmer 

提供源码

1. BookStore

  1. @Database(entities = {User.class}, version = 1) 
  2. public abstract class BookStore extends OrmDatabase { 
  3.  

2. User

  1. @Entity(tableName = "user"
  2. public class User extends OrmObject { 
  3.   @PrimaryKey(autoGenerate = true
  4.   private Integer userId; 
  5.   private String userName; 
  6.   private int userAge; 
  7.   private String userRole; 
  8.  
  9.   public User() { 
  10.   } 
  11.  
  12.   public Integer getUserId() { 
  13.     return userId; 
  14.   } 
  15.  
  16.   public void setUserId(Integer userId) { 
  17.     this.userId = userId; 
  18.   } 
  19.  
  20.   public String getUserName() { 
  21.     return userName; 
  22.   } 
  23.  
  24.   public void setUserName(String userName) { 
  25.     this.userName = userName; 
  26.   } 
  27.  
  28.   public int getUserAge() { 
  29.     return userAge; 
  30.   } 
  31.  
  32.   public void setUserAge(int userAge) { 
  33.     this.userAge = userAge; 
  34.   } 
  35.  
  36.   public String getUserRole() { 
  37.     return userRole; 
  38.   } 
  39.  
  40.   public void setUserRole(String userRole) { 
  41.     this.userRole = userRole; 
  42.   } 
  43.  
  44.   @Override 
  45.   public String toString() { 
  46.     return "User{" + 
  47.         "userId=" + userId + 
  48.         ", userName='" + userName + '\'' + 
  49.         ", userAge=" + userAge + 
  50.         ", userRole='" + userRole + '\'' + 
  51.         '}'
  52.   } 

3. UserDataAbility

  1. public class UserDataAbility extends Ability { 
  2.   private static final String DATABASE_NAME ="BookStore.db"
  3.   private static final String DATABASE_NAME_ALIAS = "BookStore"
  4.   private static OrmContext ormContext = null
  5.   private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo"); 
  6.  
  7.   @Override 
  8.   public void onStart(Intent intent) { 
  9.     super.onStart(intent); 
  10.  
  11.     DatabaseHelper helper = new DatabaseHelper(this); 
  12.     ormContext = helper.getOrmContext(DATABASE_NAME_ALIAS, DATABASE_NAME, BookStore.class); 
  13.     User user1 = new User(); 
  14.     user1.setUserName("ZhangSan"); 
  15.     user1.setUserAge(22); 
  16.     user1.setUserRole("teacher"); 
  17.     ormContext.insert(user1); 
  18.     User user2 = new User(); 
  19.     user2.setUserName("LiSi"); 
  20.     user2.setUserAge(23); 
  21.     user2.setUserRole("programmer"); 
  22.     ormContext.insert(user2); 
  23.     ormContext.flush(); 
  24.   } 
  25.  
  26.   @Override 
  27.   public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) { 
  28.     if(ormContext == null){ 
  29.       HiLog.error(LABEL_LOG,"failed to query, ormContext is null"); 
  30.       return null
  31.     } 
  32.     //查询数据库 
  33.     OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, User.class); 
  34.     ResultSet resultSet = ormContext.query(ormPredicates,columns); 
  35.     if (resultSet == null){ 
  36.       HiLog.info(LABEL_LOG,"resultSet is null"); 
  37.     } 
  38.     return resultSet; 
  39.   } 
  40.  
  41.   @Override 
  42.   public int insert(Uri uri, ValuesBucket value) { 
  43.     // 参数校验 
  44.     if (ormContext == null) { 
  45.       HiLog.error(LABEL_LOG, "failed to insert, ormContext is null"); 
  46.       return -1; 
  47.     } 
  48.  
  49.     // 构造插入数据 
  50.     User user = new User(); 
  51.     user.setUserId(value.getInteger("userId")); 
  52.     user.setUserName(value.getString("userName")); 
  53.     user.setUserAge(value.getInteger("userAge")); 
  54.     user.setUserRole(value.getString("userRole")); 
  55.  
  56.     // 插入数据库 
  57.     boolean isSuccessed; 
  58.     isSuccessed = ormContext.insert(user); 
  59.     if (!isSuccessed) { 
  60.       HiLog.error(LABEL_LOG, "failed to insert"); 
  61.       return -1; 
  62.     } 
  63.     isSuccessed = ormContext.flush(); 
  64.     if (!isSuccessed) { 
  65.       HiLog.error(LABEL_LOG, "failed to insert flush"); 
  66.       return -1; 
  67.     } 
  68.     DataAbilityHelper.creator(this, uri).notifyChange(uri); 
  69.     //返回的id,为数据表自增主键id 
  70.     int id = Math.toIntExact(user.getRowId()); 
  71.     HiLog.debug(LABEL_LOG, "success to insert id="+id); 
  72.     return id; 
  73.   } 
  74.  
  75.   @Override 
  76.   public int delete(Uri uri, DataAbilityPredicates predicates) { 
  77.     if (ormContext == null) { 
  78.       HiLog.error(LABEL_LOG, "failed to delete, ormContext is null"); 
  79.       return -1; 
  80.     } 
  81.  
  82.     OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates,User.class); 
  83.     int result = ormContext.delete(ormPredicates); 
  84.     DataAbilityHelper.creator(this, uri).notifyChange(uri); 
  85.     // result>0 表示删除成功,result<=0 表示删除失败 
  86.     if(result>0){ 
  87.       HiLog.debug(LABEL_LOG, "UserDataAbility  success to delete value="+result); 
  88.     }else { 
  89.       HiLog.debug(LABEL_LOG, "UserDataAbility  failed to delete value="+result); 
  90.     } 
  91.  
  92.     return result; 
  93.   } 
  94.  
  95.   @Override 
  96.   public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) { 
  97.     if (ormContext == null) { 
  98.       HiLog.error(LABEL_LOG, "failed to update, ormContext is null"); 
  99.       return -1; 
  100.     } 
  101.  
  102.     OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates,User.class); 
  103.     int result = ormContext.update(ormPredicates, value); 
  104.     // result>0 表示更新成功,result<=0 表示更新失败 
  105.     if(result>0){ 
  106.       HiLog.info(LABEL_LOG, "UserDataAbility success to update value:" + result); 
  107.     }else { 
  108.       HiLog.info(LABEL_LOG, "UserDataAbility failed to update value:" + result); 
  109.  
  110.     } 
  111.     DataAbilityHelper.creator(this, uri).notifyChange(uri); 
  112.     return result; 
  113.   } 
  114.  
  115.  

4. MainAbilitySlice

  1. public class MainAbilitySlice extends AbilitySlice implements ClickedListener { 
  2.   private String uriString = "dataability:///com.isoftstone.ormdatebase.UserDataAbility"
  3.   private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo"); 
  4.   private DataAbilityHelper helper; 
  5.   @Override 
  6.   public void onStart(Intent intent) { 
  7.     super.onStart(intent); 
  8.     super.setUIContent(ResourceTable.Layout_ability_main); 
  9.     helper = DataAbilityHelper.creator(this); 
  10.     Button queryAll = (Button) findComponentById(ResourceTable.Id_query_all); 
  11.     Button queryByCondition = (Button) findComponentById(ResourceTable.Id_query_by_condition); 
  12.     Button insert = (Button) findComponentById(ResourceTable.Id_insert); 
  13.     Button update = (Button) findComponentById(ResourceTable.Id_update); 
  14.     Button delete = (Button) findComponentById(ResourceTable.Id_delete); 
  15.     queryAll.setClickedListener(this); 
  16.     queryByCondition.setClickedListener(this); 
  17.     insert.setClickedListener(this); 
  18.     update.setClickedListener(this); 
  19.     delete.setClickedListener(this); 
  20.   } 
  21.   @Override 
  22.   public void onClick(Component component) { 
  23.     switch (component.getId()){ 
  24.       case ResourceTable.Id_query_all: 
  25.         try { 
  26.           queryAll(); 
  27.         } catch (Exception e) { 
  28.           e.printStackTrace(); 
  29.         } 
  30.         break; 
  31.       case ResourceTable.Id_query_by_condition: 
  32.         try { 
  33.           queryByCondition(); 
  34.         } catch (Exception e) { 
  35.           e.printStackTrace(); 
  36.         } 
  37.         break; 
  38.       case ResourceTable.Id_insert: 
  39.         try { 
  40.           insert(); 
  41.         } catch (Exception e) { 
  42.           e.printStackTrace(); 
  43.         } 
  44.         break; 
  45.       case ResourceTable.Id_update: 
  46.         try { 
  47.           update(); 
  48.         } catch (Exception e) { 
  49.           e.printStackTrace(); 
  50.         } 
  51.         break; 
  52.       case ResourceTable.Id_delete: 
  53.         try { 
  54.           delete(); 
  55.         } catch (Exception e) { 
  56.           e.printStackTrace(); 
  57.         } 
  58.     } 
  59.   } 
  60.   public void queryAll() throws DataAbilityRemoteException { 
  61.     // 构造查询条件 
  62.     DataAbilityPredicates predicates = new DataAbilityPredicates(); 
  63.     String[] columns = new String[]{"userId""userName""userAge""userRole"}; 
  64.     Uri uri = Uri.parse(uriString); 
  65.     ResultSet resultSet = helper.query(uri, columns, predicates); 
  66.     // 处理结果 
  67.     resultSet.goToFirstRow(); 
  68.     do { 
  69.       // 在此处理ResultSet中的记录; 
  70.       HiLog.info(LABEL_LOG, 
  71.           "userId=" + resultSet.getString(0) 
  72.               + "; userName=" + resultSet.getString(1) 
  73.               + "; userAge=" + resultSet.getString(2) 
  74.               + "; userRole=" + resultSet.getString(3)); 
  75.     } while (resultSet.goToNextRow()); 
  76.   } 
  77.   public void queryByCondition() throws DataAbilityRemoteException { 
  78.     // 构造查询条件 
  79.     DataAbilityPredicates predicates = new DataAbilityPredicates(); 
  80.     predicates.equalTo("userRole""programmer"); 
  81.     String[] columns = new String[]{"userId""userName""userAge""userRole"}; 
  82.     Uri uri = Uri.parse(uriString); 
  83.     ResultSet resultSet = helper.query(uri, columns, predicates); 
  84.     // 处理结果 
  85.     resultSet.goToFirstRow(); 
  86.     do { 
  87.       // 在此处理ResultSet中的记录; 
  88.       HiLog.info(LABEL_LOG, 
  89.           "userId=" + resultSet.getString(0) 
  90.               + "; userName=" + resultSet.getString(1) 
  91.               + "; userAge=" + resultSet.getString(2) 
  92.               + "; userRole=" + resultSet.getString(3)); 
  93.     } while (resultSet.goToNextRow()); 
  94.   } 
  95.   public void insert() throws DataAbilityRemoteException { 
  96.  
  97.     DataAbilityHelper helper = DataAbilityHelper.creator(this); 
  98.     Uri uri = Uri.parse(uriString); 
  99.     ValuesBucket valuesBucket = new ValuesBucket(); 
  100.     valuesBucket.putString("userName""WangWu"); 
  101.     valuesBucket.putInteger("userAge", 18); 
  102.     valuesBucket.putString("userRole""police"); 
  103.     helper.insert(uri, valuesBucket); 
  104.   } 
  105.   public void update() throws DataAbilityRemoteException { 
  106.     // 构造插入数据 
  107.     DataAbilityHelper helper = DataAbilityHelper.creator(this); 
  108.  
  109.     // 构造更新条件 
  110.     DataAbilityPredicates predicates = new DataAbilityPredicates(); 
  111.     predicates.equalTo("userName""WangWu"); 
  112.  
  113.     // 构造更新数据 
  114.     ValuesBucket valuesBucket = new ValuesBucket(); 
  115.     valuesBucket.putString("userName""ZhaoLiu"); 
  116.     valuesBucket.putInteger("UserAge", 26); 
  117.     valuesBucket.putString("userRole""doctor"); 
  118.     Uri uri = Uri.parse(uriString); 
  119.     int update = helper.update(uri, valuesBucket, predicates); 
  120.   } 
  121.   public void delete() throws DataAbilityRemoteException { 
  122.     DataAbilityHelper helper = DataAbilityHelper.creator(this); 
  123.     Uri uri = Uri.parse(uriString); 
  124.     // 构造删除条件 
  125.     DataAbilityPredicates predicates = new DataAbilityPredicates(); 
  126.     predicates.equalTo("userName""ZhaoLiu"); 
  127.     helper.delete(uri,predicates); 
  128.   } 

5. 页面布局ability_main.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.   xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.   ohos:height="match_parent" 
  5.   ohos:width="match_parent" 
  6.   ohos:orientation="vertical" > 
  7.  
  8.   <Button 
  9.     ohos:id="$+id:query_all" 
  10.     ohos:width="match_content" 
  11.     ohos:height="match_content" 
  12.     ohos:text="QueryAll" 
  13.     ohos:text_size="19fp" 
  14.     ohos:text_color="#FFFFFF" 
  15.     ohos:top_margin="30vp" 
  16.     ohos:top_padding="8vp" 
  17.     ohos:bottom_padding="8vp" 
  18.     ohos:right_padding="70vp" 
  19.     ohos:left_padding="70vp" 
  20.     ohos:background_element="$graphic:background_button" 
  21.     ohos:center_in_parent="true" 
  22.     ohos:align_parent_bottom="true" 
  23.     ohos:bottom_margin="40vp" 
  24.     ohos:layout_alignment="center" 
  25.     /> 
  26.   <Button 
  27.     ohos:id="$+id:query_by_condition" 
  28.     ohos:width="match_content" 
  29.     ohos:height="match_content" 
  30.     ohos:text="queryByCondition" 
  31.     ohos:text_size="19fp" 
  32.     ohos:text_color="#FFFFFF" 
  33.     ohos:top_padding="8vp" 
  34.     ohos:bottom_padding="8vp" 
  35.     ohos:right_padding="70vp" 
  36.     ohos:left_padding="70vp" 
  37.     ohos:background_element="$graphic:background_button" 
  38.     ohos:center_in_parent="true" 
  39.     ohos:align_parent_bottom="true" 
  40.     ohos:bottom_margin="40vp" 
  41.     ohos:layout_alignment="center" 
  42.     /> 
  43.   <Button 
  44.     ohos:id="$+id:insert" 
  45.     ohos:width="match_content" 
  46.     ohos:height="match_content" 
  47.     ohos:text="Insert" 
  48.     ohos:text_size="19fp" 
  49.     ohos:text_color="#FFFFFF" 
  50.     ohos:top_padding="8vp" 
  51.     ohos:bottom_padding="8vp" 
  52.     ohos:right_padding="70vp" 
  53.     ohos:left_padding="70vp" 
  54.     ohos:background_element="$graphic:background_button" 
  55.     ohos:center_in_parent="true" 
  56.     ohos:align_parent_bottom="true" 
  57.     ohos:bottom_margin="40vp" 
  58.     ohos:layout_alignment="center" 
  59.     /> 
  60.   <Button 
  61.     ohos:id="$+id:update" 
  62.     ohos:width="match_content" 
  63.     ohos:height="match_content" 
  64.     ohos:text="Update" 
  65.     ohos:text_size="19fp" 
  66.     ohos:text_color="#FFFFFF" 
  67.     ohos:top_padding="8vp" 
  68.     ohos:bottom_padding="8vp" 
  69.     ohos:right_padding="70vp" 
  70.     ohos:left_padding="70vp" 
  71.     ohos:background_element="$graphic:background_button" 
  72.     ohos:center_in_parent="true" 
  73.     ohos:align_parent_bottom="true" 
  74.     ohos:bottom_margin="40vp" 
  75.     ohos:layout_alignment="center" 
  76.     /> 
  77.   <Button 
  78.     ohos:id="$+id:delete" 
  79.     ohos:width="match_content" 
  80.     ohos:height="match_content" 
  81.     ohos:text="Delete" 
  82.     ohos:text_size="19fp" 
  83.     ohos:text_color="#FFFFFF" 
  84.     ohos:top_padding="8vp" 
  85.     ohos:bottom_padding="8vp" 
  86.     ohos:right_padding="70vp" 
  87.     ohos:left_padding="70vp" 
  88.     ohos:background_element="$graphic:background_button" 
  89.     ohos:center_in_parent="true" 
  90.     ohos:align_parent_bottom="true" 
  91.     ohos:bottom_margin="40vp" 
  92.     ohos:layout_alignment="center" 
  93.     /> 
  94.  
  95.  
  96. </DirectionalLayout> 

6.圆角背景图形background_button.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:shape="rectangle"
  5.     <corners 
  6.         ohos:radius="100"/> 
  7.     <solid 
  8.         ohos:color="#007DFF"/> 
  9. </shape> 

7.config.json

  1.   "app": { 
  2.     "bundleName""com.isoftstone.ormdatebase"
  3.     "vendor""isoftstone"
  4.     "version": { 
  5.       "code": 1000000, 
  6.       "name""1.0" 
  7.     }, 
  8.     "apiVersion": { 
  9.       "compatible": 4, 
  10.       "target": 5, 
  11.       "releaseType""Release" 
  12.     } 
  13.   }, 
  14.   "deviceConfig": {}, 
  15.   "module": { 
  16.     "package""com.isoftstone.ormdatebase"
  17.     "name"".MyApplication"
  18.     "deviceType": [ 
  19.       "phone" 
  20.     ], 
  21.     "distro": { 
  22.       "deliveryWithInstall"true
  23.       "moduleName""entry"
  24.       "moduleType""entry" 
  25.     }, 
  26.     "abilities": [ 
  27.       { 
  28.         "skills": [ 
  29.           { 
  30.             "entities": [ 
  31.               "entity.system.home" 
  32.             ], 
  33.             "actions": [ 
  34.               "action.system.home" 
  35.             ] 
  36.           } 
  37.         ], 
  38.         "orientation""unspecified"
  39.         "name""com.isoftstone.ormdatebase.MainAbility"
  40.         "icon""$media:icon"
  41.         "description""$string:mainability_description"
  42.         "label""$string:app_name"
  43.         "type""page"
  44.         "launchType""standard" 
  45.       }, 
  46.       { 
  47.         "visible"true
  48.         "permissions": [ 
  49.           "com.isoftstone.ormdatebase.DataAbilityShellProvider.PROVIDER" 
  50.         ], 
  51.         "name""com.isoftstone.ormdatebase.UserDataAbility"
  52.         "icon""$media:icon"
  53.         "description""$string:userdataability_description"
  54.         "type""data"
  55.         "uri""dataability://com.isoftstone.ormdatebase.UserDataAbility" 
  56.       } 
  57.     ] 
  58.   } 

8.build.gradle

  1. apply plugin: 'com.huawei.ohos.hap' 
  2. ohos { 
  3.     compileSdkVersion 5 
  4.     defaultConfig { 
  5.         compatibleSdkVersion 4 
  6.     } 
  7.     compileOptions{ 
  8.         annotationEnabled true 
  9.     } 
  10.  
  11. dependencies { 
  12.     implementation fileTree(dir: 'libs', include: ['*.jar''*.har']) 
  13.     testCompile 'junit:junit:4.12' 

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-09-03 15:41:00

鸿蒙HarmonyOS应用

2021-08-31 14:58:52

鸿蒙HarmonyOS应用

2021-09-23 10:00:57

鸿蒙HarmonyOS应用

2021-08-26 09:50:06

鸿蒙HarmonyOS应用

2021-09-13 15:14:01

鸿蒙HarmonyOS应用

2021-09-06 10:24:12

鸿蒙HarmonyOS应用

2021-08-27 09:57:18

鸿蒙HarmonyOS应用

2021-11-11 17:36:07

鸿蒙HarmonyOS应用

2012-02-08 12:17:38

HibernateJava

2020-09-10 18:14:51

人工智能 IBM

2020-09-11 10:59:05

数据库

2020-12-11 16:37:46

数据库/新基建/全栈

2009-07-10 09:28:41

NoSQL关系数据库

2020-12-29 11:26:22

鸿蒙HarmonyOS数据库

2012-05-30 15:03:43

ibmdw

2011-06-07 17:14:15

关系型数据库压缩技术

2009-09-23 13:26:10

Hibernate对象

2021-07-29 14:03:35

鸿蒙HarmonyOS应用

2011-05-19 10:29:40

对象数据库关系数据库

2019-01-16 14:20:42

点赞
收藏

51CTO技术栈公众号