1 新建项目,选择ability
2 在entry目录下的build.gradle文件中加入如下配置,此配置的位置非常重要,要放在buildTypes下面,然后文件会提示sync,点击一下sync
buildTypes { release { proguardOpt { proguardEnabled false rulesFiles 'proguard-rules.pro' } } }
注意:如果不加下面这个配置,使用注解@Database,会出现找不到包的情况
compileOptions{ annotationEnabled true }
xml布局文件如下:
<?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:alignment="center" ohos:orientation="vertical"> <DirectionalLayout ohos:height="match_content" ohos:width="match_parent" ohos:orientation="horizontal"> <Text ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="ID" ohos:text_size="30fp" /> <TextField ohos:id="$+id:userId" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:hint="请输入修改的ID" ohos:text_size="30fp" /> </DirectionalLayout> <DirectionalLayout ohos:height="match_content" ohos:width="match_parent" ohos:orientation="horizontal"> <Text ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="account" ohos:text_size="30fp" /> <TextField ohos:id="$+id:account" ohos:height="match_parent" ohos:width="match_parent" ohos:hint="请输入用户名" ohos:text_size="20fp"> </TextField> </DirectionalLayout> <DirectionalLayout ohos:top_margin="10vp" ohos:height="match_content" ohos:width="match_parent" ohos:orientation="horizontal"> <Text ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="password" ohos:text_size="30fp" /> <TextField ohos:id="$+id:password" ohos:height="match_parent" ohos:width="match_parent" ohos:text_size="20fp" ohos:hint="请输入密码"> </TextField> </DirectionalLayout> <Text ohos:id="$+id:add" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="add" ohos:text_size="40vp" /> <Text ohos:id="$+id:delete" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="delete" ohos:text_size="40vp" /> <Text ohos:id="$+id:update" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="update" ohos:text_size="40vp" /> <Text ohos:id="$+id:find" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="find" ohos:text_size="40vp" /> </DirectionalLayout>
我们现在用的是关系型数据库,所以需要建立字段和实体类之间的关系
package com.gulixiong.dataabilitydemo02.entity; import ohos.data.orm.OrmObject; import ohos.data.orm.annotation.Entity; import ohos.data.orm.annotation.PrimaryKey; /** * 声明一张学生表 */ @Entity(tableName = "student") public class Student extends OrmObject { @PrimaryKey(autoGenerate = true) private Integer id; private String userName; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", userName='" + userName + '\'' + ", password='" + password + '\'' + '}'; } }
package com.gulixiong.dataabilitydemo02.entity; import ohos.data.orm.OrmDatabase; import ohos.data.orm.annotation.Database; /** * 声明一个数据库 */ @Database( entities = {Student.class}, version = 1 ) public abstract class School extends OrmDatabase { }
MainAbilitySlice类如下:
package com.gulixiong.dataabilitydemo02.slice; import com.gulixiong.dataabilitydemo02.ResourceTable; import com.gulixiong.dataabilitydemo02.entity.School; import com.gulixiong.dataabilitydemo02.entity.Student; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Component; import ohos.agp.components.Text; import ohos.agp.components.TextField; import ohos.data.DatabaseHelper; import ohos.data.orm.OrmContext; import ohos.data.orm.OrmObject; import ohos.data.orm.OrmPredicates; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; import java.util.List; public class MainAbilitySlice extends AbilitySlice { private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, "data_alility_demo2"); TextField account; TextField password; TextField userId; Component add=null; Text delete=null; Text update=null; Text find=null; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); initUI(); HiLog.info(LABEL_LOG,"实例化组件success"); setListeners(); HiLog.info(LABEL_LOG,"设置监听事件success"); } private void initUI() { account= (TextField) findComponentById(ResourceTable.Id_account); password= (TextField) findComponentById(ResourceTable.Id_password); userId= (TextField) findComponentById(ResourceTable.Id_userId); add=findComponentById(ResourceTable.Id_add); delete= (Text) findComponentById(ResourceTable.Id_delete); update= (Text) findComponentById(ResourceTable.Id_update); find= (Text) findComponentById(ResourceTable.Id_find); } private void setListeners(){ add.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { add(); } }); delete.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { delete(); } }); update.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { update(); } }); find.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { find(); } }); } private OrmContext initDB(){ DatabaseHelper db=new DatabaseHelper(this); //第一个参数:数据库名称, //第二个参数:数据库文件名称 //第三个参数 java对象 return db.getOrmContext("School","School.db", School.class); } private void add(){ OrmContext db = initDB(); String userName=account.getText(); String passwd=password.getText(); Student stu=new Student(); stu.setUserName(userName); stu.setPassword(passwd); db.insert(stu); //修改操作需要调用flush方法 db.flush(); HiLog.info(LABEL_LOG,"add success"); } private void find(){ OrmContext ormContext = initDB(); String userName=account.getText(); //查询条件 OrmPredicates where=ormContext.where(Student.class); if(!"".equals(userName)){ where.equalTo("userName",userName); } //执行查询 List<OrmObject> list = ormContext.query(where); HiLog.info(LABEL_LOG,"总记录数:"+list.size()); list.forEach(item -> { HiLog.info(LABEL_LOG,item.toString()); }); } private void delete(){ String userName=account.getText(); OrmContext context = initDB(); //where条件 OrmPredicates where=context.where(Student.class); if(!"".equals(userName)){ where.equalTo("userName",userName); List<OrmObject> list = context.query(where); //执行删除 context.delete(list.get(0)); context.flush(); HiLog.info(LABEL_LOG,"delete success"); }else{ HiLog.info(LABEL_LOG,userName+" is not exists"); } } private void update(){ String id=userId.getText(); String userName=account.getText(); String passwd=password.getText(); if(!"".equals(id)){ OrmContext context = initDB(); //where条件 OrmPredicates where =new OrmPredicates(Student.class); where.equalTo("id",id); List<Student> list = context.query(where); list.forEach(item -> { Student stu = list.get(0); stu.setPassword(passwd); stu.setUserName(userName); context.flush(); HiLog.info(LABEL_LOG,"update success"); }); }else{ HiLog.info(LABEL_LOG,"要更新的数据不存在"); } } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
输入用户名和密码,先点击文本"add",然后在测试修改,删除,查询, 使用HiLog过滤日志,如果打印相关的日志,说明增删改查没有问题