1. ActiveOhos功能介紹
1.1. 組件介紹
基於HarmonyOS據庫進行sqlite數據庫操作,創建連接時比較繁瑣,本組件簡化了sqlite數據庫的連接,並且對HarmonyOS原生的API進行封裝加強,使sqlite數據庫的讀寫更加方便。
1.2. 手機模擬器上運行效果
插入數據成功
2. ActiveOhos使用方法
2.1. 爲應用添加sqlitelibrary-debug.har包依賴
在應用模塊中調用HAR,常用的添加依賴爲:依賴本地HAR
第一步:將sqlitelibrary-debug.har複製到entry\libs目錄下即可(由於build.gradle中已經依賴的libs目錄下的*.har,因此不需要再做修改)。
查看工程目錄中build.gradle下的*.har是否存在
第二步:除了依賴har之外還需要添加外部依賴用來實現類的引入,引入方式如下,引入完之後同步即可使用。
-
如果使用註解處理器的模塊爲“com.huawei.ohos.hap”,則需要在模塊 “build.gradle”文件的“ohos”節點中添加以下配置:
1
2
3
|
compileOptions{
annotationEnabled true
}
|
-
如果使用註解處理器的模塊爲“com.huawei.ohos.library”,則需要在模塊“build.gradle”文件的“dependencies”節點中配置註解處理器。查看“orm_annotations_java.jar”、“orm_annotations_processor_java.jar” 、“javapoet_java.jar” 3個jar包在HUAWEI SDK中的對應目錄,並將這三個jar包導入項目中。
dependencies { compile files("orm_annotations_java.jar的路徑
,orm_annotations_processor_java.jar的路徑,javapoet_java.jar的路徑)
annotationProcessor files("orm_annotations_java.jar的路徑
,orm_annotations_processor_java.jar的路徑,javapoet_java.jar的路徑)}
-
如果使用註解處理器的模塊爲“java-library”,則需要在模塊 “build.gradle”文件的“dependencies”節點中配置註解處理器,並導入“ohos.jar”。
-
dependencies { compile files("ohos.jar的路徑","orm_annotations_java.jar的路徑
-
","orm_annotations_processor_java.jar的路徑","javapoet_java.jar的路徑")
-
annotationProcessor files("orm_annotations_java.jar的路徑
-
","orm_annotations_processor_java.jar的路徑","javapoet_java.jar的路徑")}
-
比如:
-
以上操作無誤 之後就可以進行編碼了!
-
3. ActiveOhos開發實現
-
3.1. 主頁面的佈局文件
-
定義四個按鈕分別實現增刪改查,定義四個Button實現請求點擊事件
-
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Button
ohos:id="$+id:btn_insert"
ohos:height="match_content"
ohos:width="80fp"
ohos:text_color="red"
ohos:text="插入"
ohos:text_size="20fp"
ohos:weight="100fp"/>
<Button
ohos:id="$+id:btn_query"
ohos:height="match_content"
ohos:width="100fp"
ohos:text_color="blue"
ohos:text="查詢"
ohos:text_size="20fp"
ohos:weight="100fp"/>
<Button
ohos:id="$+id:btn_update"
ohos:height="match_content"
ohos:width="100fp"
ohos:text_color="green"
ohos:text="更新"
ohos:text_size="20fp"
ohos:weight="100fp"/>
<Button
ohos:id="$+id:btn_delete"
ohos:height="match_content"
ohos:width="100fp"
ohos:text_color="black"
ohos:text="刪除"
ohos:text_size="20fp"
ohos:weight="100fp"/>
<ListContainer
ohos:id="$+id:listText"
ohos:height="match_parent"
ohos:width="match_parent"/>
</DirectionalLayout>
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="get請求"
ohos:text_size="50"
ohos:top_margin="80vp"
/>
</DirectionalLayout>
3.2. 例子代碼如下
組件中有兩種連接數據的方式,分別是OrmContext,RdbStore ,其中使用OrmContext連接方式時,需要定義一個實體類(User)來和數據庫對應表名及字段,一個數據庫類 BookStore 來配合開發,代碼如下:
MainAbilitySlice
import com.example.myapplication.BookStore;
import com.example.myapplication.ResourceTable;
import com.example.myapplication.User;
import com.example.sqlitelibrary.DBManage;
import com.example.sqlitelibrary.DBOrmContext;
import com.example.sqlitelibrary.utils.Log;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.ValuesBucket;
import java.util.ArrayList;
import java.util.List;
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
private DatabaseHelper helper;
private RdbStore store;
private OrmContext context;
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
helper = new DatabaseHelper(this);
DBManage dbManger = new DBManage("user.db","user");
context = dbManger.getConnectionContext(helper, BookStore.class);
// DBManage dbManger = new DBManage("user.db");
// store = dbManger.getConnectionStore(helper,"user");
Button btnInsert = (Button) findComponentById(ResourceTable.Id_btn_insert);
Button btnQuery = (Button) findComponentById(ResourceTable.Id_btn_query);
Button btnDelete = (Button) findComponentById(ResourceTable.Id_btn_delete);
Button btnUpdate = (Button) findComponentById(ResourceTable.Id_btn_update);
btnInsert.setClickedListener(this::onClick);
btnQuery.setClickedListener(this::onClick);
btnDelete.setClickedListener(this::onClick);
btnUpdate.setClickedListener(this::onClick);
}
public void onActive() {
super.onActive();
}
public void onForeground(Intent intent) {
super.onForeground(intent);
}
public void onClick(Component component) {
// RdbStoreManage rdbStoreMange = new RdbStoreManage();
// ValuesBucket values = new ValuesBucket();
// values.putInteger("id", 1);
// values.putString("name", "zhangsan");
// values.putInteger("age", 18);
// values.putDouble("salary", 100.5);
// values.putByteArray("blobType", new byte[] {1, 2, 3});
// rdbStoreMange.setSql(store, "insert into user values(zhangsan, 18, 100.5, byte[1,2,3])");
// long id = rdbStoreMange.insert(store,"user", values);
// System.out.println(id);
DBOrmContext dbOrmContext = new DBOrmContext();
switch (component.getId()) {
case ResourceTable.Id_btn_insert: //插入數據
//第一次使用user對應的表的時候,如果有這張表就直接使用,沒有就創建表
User user = new User();
user.setFirstName("Zhang");
user.setLastName("San");
user.setAge(29);
user.setBalance(100.51);
boolean b = dbOrmContext.insert(context, user);
Log.i("插入成功");
System.out.println(b);
break;
case ResourceTable.Id_btn_query: //條件查詢
List<User> users = new ArrayList<>();
OrmPredicates query = context.where(User.class).equalTo("lastName", "San");
users = dbOrmContext.query(context, query);
break;
case ResourceTable.Id_btn_delete: //條件刪除
OrmPredicates delete = context.where(User.class).equalTo("lastName", "San");
int delete1 = dbOrmContext.delete(context, delete);
System.out.println(delete1);
break;
case ResourceTable.Id_btn_update: //條件更新
ValuesBucket valuesBucket = new ValuesBucket();
valuesBucket.putInteger("age", 31);
valuesBucket.putString("firstName", "Zhang");
valuesBucket.putString("lastName", "San");
valuesBucket.putDouble("balance", 300.51);
OrmPredicates update = context.where(User.class).equalTo("userId", 1);
int update1 = dbOrmContext.update(context, valuesBucket, update);
System.out.println(update1);
break;
}
dbOrmContext.flush(context);
}
}
user.java
@Entity(tableName = "user", ignoredColumns = {"ignoreColumn1", "ignoreColumn2"},
indices = {@Index(value = {"firstName", "lastName"}, name = "name_index", unique = true)})
public class User extends OrmObject {
// 此處將userId設爲了自增的主鍵。注意只有在數據類型爲包裝類型時,自增主鍵才能生效。
@PrimaryKey(autoGenerate = true)
private Integer userId;
private String firstName;
private String lastName;
private int age;
private double balance;
private int ignoreColumn1;
private int ignoreColumn2;
// 開發者自行添加字段的getter和setter 方法
項目源代碼地址:https://github.com/isoftstone-dev/Active_HarmonyOS
歡迎交流:[email protected]
原文鏈接:https://developer.huawei.com/consumer/cn/forum/topic/0202558662106980609?fid=0101303901040230869
原作者:軟通動力HOS