写点什么

Android GreenDao 使用全面讲解 (1),2021Android 最新大厂面试真题总结

用户头像
Android架构
关注
发布于: 2021 年 11 月 05 日

5. 注解讲解

从 GreenDao 3 使用注解来定义模型和实体,前面也讲过,通过注解的使用可以快速构建数据库表,包括设置主键,自增,值是否唯一等等等……


下面我们来看下注解的简单使用:


@Entitypublic class Student {@Id(autoincrement = true)Long id;@Uniqueint studentNo;//学号 int age; //年龄 String telPhone;//手机号 String sex; //性别 String name;//姓名 String address;//家庭住址 String schoolName;//学校名字 String grade;//几年级……getter and setter and constructor method……}

1. @Entity 注解

@Entity 是 GreenDao 必不可少的注解,只有在实体类中使用了 @Entity 注解 GreenDao 才会创建对应的表。当然我们也可以使用 @Entity 配置一些细节:


  • schema:如果你有多个架构,你可以告诉 GreenDao 当前属于哪个架构。

  • active:标记一个实体处于活跃状态,活动实体有更新、删除和刷新方法。

  • nameInDb:在数据中使用的别名,默认使用的是实体的类名。

  • indexes:标记如果 DAO 应该创建数据库表(默认为 true),如果您有多个实体映射到一个表,或者表的创建是在 greenDAO 之外进行的,那么将其设置为 false。

  • createInDb:标记创建数据库表。

  • generateGettersSetters:如果缺少,是否应生成属性的 getter 和 setter 方法。


@Entity(


schema = "myschema",active = true,nameInDb = "AWESOME_USERS",indexes = {@Index(value = "message DESC", unique = true)},createInDb = false,generateConstructors = true,generateGettersSetters = true)public class Student{ ……}

2. 基础属性注解(@Id,@Property,@NotNull,@Transient)

@Id @Id 注解选择 long / Long 属性作为实体 ID。在数据库方面,它是主键。参数 autoincrement = true 表示自增,id 不给赋值或者为赋值为 null 即可(这里需要注意,如果要实现自增,id 必须是 Long,为 long 不行!)。


@Entitypublic class Student {@Id(autoincrement = true)Long id;……}


@Property 允许您定义属性映射到的非默认列名。如果不存在,GreenDAO 将以 SQL-ish 方式使用字段名称(大写,下划线而不是 camel 情况,例如 name 将成为 NAME)。注意:您当前只能使用内联常量来指定列名。


@Entitypublic class Student {@Id(autoincrement = true)Long id;@Property (nameInDb="name") //设置了,数据库中的表格属性名为"name",如果不设置,数据库中表格属性名为"NAME"String name;……}


@NotNull :设置数据库表当前列不能为空 。


@Transient :添加次标记之后不会生成数据库表的列。标记要从持久性中排除的属性。将它们用于临时状态等。或者,您也可以使用 Java 中的 transient 关键字。

3. 索引注解

  • @Index:使用 @Index 作为一个属性来创建一个索引,通过 name 设置索引别名,也可以通过 unique 给索引添加约束。

  • @Unique:向索引添加 UNIQUE 约束,强制所有值都是唯一的。


@Entitypublic class Student {@Id(autoincrement = true)Long id;@Property(nameInDb="name")@Index(unique = true)String name;……}


注意: 上面这种情况,约定 name 为唯一值,向数据库中通过 insert 方法继续添加已存在的 name 数据,会抛异常:


10-08 20:59:46.274 31939-31939/com.example.aserbao.aserbaosandroid E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.example.aserbao.aserbaosandroid, PID: 31939android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: STUDENT.name (Sqlite code 2067), (OS error - 2:No such file or directory)……


若使用 insertOrReplace()方法添加数据,当前数据库中不会有重复的数据,但是重复的这条数据的 id 会被修改!若项目中有用到 id 字段进行排序的话,这一点需要特别注意。

4. 关系注解

关系型注解 GreenDao 中主要就两个:


  • @ToOne:定义与另一个实体(一个实体对象)的关系

  • @ToMany:定义与多个实体对象的关系 至于如何使用,我们马上就讲。

6. 一对一,一对多,多对多关系表的创建

平常项目中,我们经常会使用到多表关联,如文章开头所说的数据库表结构设置的那样!接下来我们来讲如何通过 GreenDao 实现多表关联。

1. 一对一

一个学生对应一个身份证号: 做法:


  1. 我们在 Student 中设置一个注解 @ToOne(joinProperty = "name")

  2. 在创建 Student 的时候,将对应的数据传递给 IdCard; 代码部分:


学生 Student 代码:


@Entitypublic class Student {@Id(autoincrement = true)Long id;@Uniqueint studentNo;//学号 int age; //年龄 String telPhone;//手机号 String sex; //性别 String name;//姓名 String address;//家庭住址 String schoolName;//学校名字 String grade;//几年级 @ToOne(joinProperty = "name")IdCard student;……getter and setter ……}


身份证 IdCard 代码:


@Entitypublic class IdCard {@IdString userName;//用户名 @UniqueString idNo;//身份证号……getter and setter ……}


insert 一组数据:


public void addStudent(){DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();Student student = new Student();student.setStudentNo(i);int age = mRandom.nextInt(10) + 10;student.setAge(age);student.setTelPhone(RandomValue.getTel());String chineseName = RandomValue.getChineseName();student.setName(chineseName);if (i % 2 == 0) {student.setSex("男");} else {student.setSex("女");}student.setAddress(RandomValue.getRoad());student.setGrade(String.valueOf(age % 10) + "年纪");student.setSchoolName(RandomValue.getSchoolName());daoSession.insert(student);


//插入对应的 IdCard 数据 IdCard idCard = new IdCard();idCard.setUserName(userName);idCard.setIdNo(RandomValue.getRandomID());daoSession.insert(idCard);}


ok,数据可以了!现在数据库表插入完成了。

2. 一对多

一个人拥有多个信用卡 做法:


  1. 在我们在 Student 中设置 @ToMany(referencedJoinProperty = "studentId");

  2. 我们在 CreditCard 中设置编写对应的 id 主键;


Student 的代码:


@Entitypublic class Student {@Id(autoincrement = true)Long id;


@Uniqueint studentNo;//学号


int age; //年龄 String telPhone;//手机号 String sex; //性别 String name;//姓名 String address;//家庭住址 String schoolName;//学校名字 String grade;//几年级


@ToMany(referencedJoinProperty = "studentId) // 这个 studentId 是对应在 CreditCard 中的 studentIdList<CreditCard> creditCardsList;……getter and setter ……}


CreditCard 的代码:


@Entitypublic class CreditCard {@IdLong id;Long studentId;Long teacherId;String userName;//持有者名字 String cardNum;//卡号 String whichBank;//哪个银行的 int cardType;//卡等级,分类 0 ~ 5……getter and setter ……}


添加数据代码:


public void addStudent(){DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();Student student = new Student();student.setStudentNo(i);int age = mRandom.nextInt(10) + 10;student.setAge(age);student.setTelPhone(RandomValue.getTel());String chineseName = RandomValue.getChineseName();student.setName(chineseName);if (i % 2 == 0) {student.setSex("男");} else {student.setSex("女");}student.setAddress(RandomValue.getRoad());student.setGrade(String.valueOf(age % 10) + "年纪");student.setSchoolName(RandomValue.getSchoolName());daoSession.insert(student);


//插入对应的 CreditCard 数据 for (int j = 0; j < random.nextInt(5) + 1 ; j++) {CreditCard creditCard = new CreditCard();creditCard.setUserId(id);creditCard.setUserName(userName);creditCard.setCardNum(String.valueOf(random.nextInt(899999999) + 100000000) + String.valueOf(random.nextInt(899999999) + 100000000));creditCard.setWhichBank(RandomValue.getBankName());creditCard.setCardType(random.nextInt(10));daoSession.insert(creditCard);}}

3. 多对多

一个学生有多个老师,老师有多个学生。 做法:


  1. 我们需要创建一个学生老师管理器(StudentAndTeacherBean),用来对应学生和老师的 ID;

  2. 我们需要在学生对象中,添加注解:


@ToMany @JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "studentId",targetProperty = "teacherId") List teacherList;


  1. 我们需要在老师对象中,添加注解:@ToMany


@JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "teacherId",targetProperty = "studentId") List studentList;


StudentAndTeacherBean 代码:


@Entitypublic class StudentAndTeacherBean {@Id(autoincrement = true)Long id;Long studentId;//学生 IDLong teacherId;//老师 ID……getter and setter ……}


Student 代码:


@Entitypublic class Student {@Id(autoincrement = true)Long id;@Uniqueint studentNo;//学号 int age; //年龄 String telPhone;//手机号 String sex; //性别 String name;//姓名 String address;//家庭住址 String schoolName;//学校名字 String grade;//几年级 @ToMany@JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "studentId",targetProperty = "teacherId")List<Teacher> teacherList;……getter and setter ……}


Teacher 代码:


@Entitypublic class Teacher {@Id(autoincrement = true)Long id;@Uniqueint teacherNo;//职工号 int age; //年龄 String sex; //性别 String telPhone;String name;//姓名 String schoolName;//学校名字 String subject;//科目


@ToMany@JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "teacherId",targetProperty = "studentId")List<Student> studentList;……getter and setter ……}


数据添加:


public void addData(){Student student = new Student();student.setStudentNo(i);int age = mRandom.nextInt(10) + 10;student.setAge(age);student.setTelPhone(RandomValue.getTel());String chineseName = RandomValue.getChineseName();student.setName(chineseName);if (i % 2 == 0) {student.setSex("男");} else {student.setSex("女");}student.setAddress(RandomValue.getRoad());student.setGrade(String.valueOf(age % 10) + "年纪");student.setSchoolName(RandomValue.getSchoolName());daoSession.insert(student);


Collections.shuffle(teacherList);for (int j = 0; j < mRandom.nextInt(8) + 1; j++) {if(j < teacherList.size()){Teacher teacher = teacherList.get(j);StudentAndTeacherBean teacherBean = new StudentAndTeacherBean(student.getId(), teacher.getId());daoSession.insert(teacherBean);}}}


好了,成功;

7. 数据库的升级

GreenDao 的 OpenHelper 下有个 onUpgrade(Database db, int oldVersion, int newVersion)方法,当设置的数据库版本改变时,在数据库初始化的时候就会回调到这个方法,我们可以通过继承 OpenHelper 重写 onUpgrade 方法来实现数据库更新操作:


GreenDao 的升级思路:


  1. 创建临时表 TMP_,复制原来的数据库到临时表中;

  2. 删除之前的原表;

  3. 创建新表;

  4. 将临时表中的数据复制到新表中,最后将 TMP_表删除掉;


ok,思路就是这样, 总共两个类: 一个 MyDaoMaster(OpenHelper 继承类),一个 MigrationHelper(数据库操作类) 下面是代码编写:


修改 Application 中的 DaoMaster 的创建:


MyDaoMaster helper = new MyDaoMaster(this, "aserbaos.db");// DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db");SQLiteDatabase db = helper.getWritableDatabase();DaoMaster daoMaster = new DaoMaster(db);daoSession = daoMaster.newSession();


MyDaoMaster 代码:


public class MyDaoMaster extends OpenHelper {private static final String TAG = "MyDaoMaster";public MyDaoMaster(Context context, String name) {super(context, name);}


public MyDaoMaster(Context context, String name, SQLiteDatabase.CursorFactory factory) {super(context, name, factory);}


@Overridepublic void onUpgrade(Database db, int oldVersion, int newVersion) {super.onUpgrade(db, oldVersion, newVersion);MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {@Overridepublic void onCreateAllTables(Database db, boolean ifNotExists) {DaoMaster.createAllTables(db, ifNotExists);}@Overridepublic void onDropAllTables(Database db, boolean ifExists) {DaoMaster.dropAllTables(db, ifExists);}},ThingDao.class);Log.e(TAG, "onUpgrade: " + oldVersion + " newVersion = " + newVersion);}}


MigrationHelper 代码:


public final class MigrationHelper {


public static boolean DEBUG = false;private static String TAG = "MigrationHelper";private static final String SQLITE_MASTER = "sqlite_master";private static final String SQLITE_TEMP_MASTER = "sqlite_temp_master";


private static WeakReference<ReCreateAllTableListener> weakListener;


public interface ReCreateAllTableListener{void onCreateAllTables(Database db, boolean ifNotExists);void onDropAllTables(Database db, boolean ifExists);}


public static void migrate(SQLiteDatabase db, Class<? extends AbstractDao<?, ?>>... daoClasses) {printLog("【The Old Database Version】" + db.getVersion());Database database = new StandardDatabase(db);migrate(database, daoClasses);}


public static void migrate(SQLiteDatabase db, ReCreateAllTableListener listener, Class<? extends AbstractDao<?, ?>>... daoClasses) {weakListener = new WeakReference<>(listener);migrate(db, daoClasses);}


public static void migrate(Database database, ReCreateAllTableListener listener, Class<? extends AbstractDao<?, ?>>... daoClasses) {weakListener = new WeakReference<>(listener);migrate(database, daoClasses);}


public static void migrate(Database database, Class<? extends AbstractDao<?, ?>>... daoClasses) {printLog("【Generate temp table】start");generateTempTables(database, daoClasses);printLog("【Generate temp table】complete");


ReCreateAllTableListener listener = null;if (weakListener != null) {listener = weakListener.get();}


if (listener != null) {listener.onDropAllTables(database, true);printLog("【Drop all table by listener】");listener.onCreateAllTables(database, false);printLog("【Create all table by listener】");} else {dropAllTables(database, true, daoClasses);createAllTables


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


(database, false, daoClasses);}printLog("【Restore data】start");restoreData(database, daoClasses);printLog("【Restore data】complete");}


private static void generateTempTables(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) {for (int i = 0; i < daoClasses.length; i++) {String tempTableName = null;


DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);String tableName = daoConfig.tablename;if (!isTableExists(db, false, tableName)) {printLog("【New Table】" + tableName);continue;}try {tempTableName = daoConfig.tablename.concat("_TEMP");StringBuilder dropTableStringBuilder = new StringBuilder();dropTableStringBuilder.append("DROP TABLE IF EXISTS ").append(tempTableName).append(";");db.execSQL(dropTableStringBuilder.toString());


StringBuilder insertTableStringBuilder = new StringBuilder();insertTableStringBuilder.append("CREATE TEMPORARY TABLE ").append(tempTableName);insertTableStringBuilder.append(" AS SELECT * FROM ").append(tableName).append(";");db.execSQL(insertTableStringBuilder.toString());printLog("【Table】" + tableName +"\n ---Columns-->"+getColumnsStr(daoConfig));printLog("【Generate temp table】" + tempTableName);} catch (SQLException e) {Log.e(TAG, "【Failed to generate temp table】" + tempTableName, e);}}}


private static boolean isTableExists(Database db, boolean isTemp, String tableName) {if (db == null || TextUtils.isEmpty(tableName)) {return false;}String dbName = isTemp ? SQLITE_TEMP_MASTER : SQLITE_MASTER;String sql = "SELECT COUNT(*) FROM " + dbName + " WHERE type = ? AND name = ?";Cursor cursor=null;int count = 0;try {cursor = db.rawQuery(sql, new String[]{"table", tableName});if (cursor == null || !cursor.moveToFirst()) {return false;}count = cursor.getInt(0);} catch (Exception e) {e.printStackTrace();} finally {if (cursor != null)cursor.close();}return count > 0;}


private static String getColumnsStr(DaoConfig daoConfig) {if (daoConfig == null) {return "no columns";}StringBuilder builder = new StringBuilder();for (int i = 0; i < daoConfig.allColumns.length; i++) {builder.append(daoConfig.allColumns[i]);builder.append(",");}if (builder.length() > 0) {builder.deleteCharAt(builder.length() - 1);}return builder.toString();}


private static void dropAllTables(Database db, boolean ifExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) {reflectMethod(db, "dropTable", ifExists, daoClasses);printLog("【Drop all table by reflect】");}


private static void createAllTables(Database db, boolean ifNotExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) {reflectMethod(db, "createTable", ifNotExists, daoClasses);printLog("【Create all table by reflect】");}


/**


  • dao class already define the sql exec method, so just invoke it*/private static void reflectMethod(Database db, String methodName, boolean isExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) {if (daoClasses.length < 1) {return;}try {for (Class cls : daoClasses) {Method method = cls.getDeclaredMethod(methodName, Database.class, boolean.class);method.invoke(null, db, isExists);}} catch (NoSuchMethodException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}


private static void restoreData(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) {for (int i = 0; i < daoClasses.length; i++) {DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);String tableName = daoConfig.tablename;String tempTableName = daoConfig.tablename.concat("_TEMP");


if (!isTableExists(db, true, tempTableName)) {continue;}


try {// get all columns from tempTable, take careful to use the columns listList<TableInfo> newTableInfos = TableInfo.getTableInfo(db, tableName);List<TableInfo> tempTableInfos = TableInfo.getTableInfo(db, tempTableName);ArrayList<String> selectColumns = new ArrayList<>(newTableInfos.size());ArrayList<String> intoColumns = new ArrayList<>(newTableInfos.size());for (TableInfo tableInfo : tempTableInfos) {if (newTableInfos.contains(tableInfo)) {String column = '' + tableInfo.name + '';intoColumns.add(column);selectColumns.add(column);}}// NOT NULL columns listfor (TableInfo tableInfo : newTableInfos) {if (tableInfo.notnull && !tempTableInfos.contains(tableInfo)) {String column = '' + tableInfo.name + '';intoColumns.add(column);


String value;if (tableInfo.dfltValue != null) {value = "'" + tableInfo.dfltValue + "' AS ";} else {value = "'' AS ";}selectColumns.add(value + column);}}


if (intoColumns.size() != 0) {StringBuilder insertTableStringBuilder = new StringBuilder();insertTableStringBuilder.append("REPLACE INTO ").append(tableName).append(" (");insertTableStringBuilder.append(TextUtils.join(",", intoColumns));insertTableStringBuilder.append(") SELECT ");insertTableStringBuilder.append(TextUtils.join(",", selectColumns));insertTableStringBuilder.append(" FROM ").append(tempTableName).append(";");db.execSQL(insertTableStringBuilder.toString());printLog("【Restore data】 to " + tableName);}StringBuilder dropTableStringBuilder = new StringBuilder();dropTableStringBuilder.append("DROP TABLE ").append(tempTableName);db.execSQL(dropTableStringBuilder.toString());printLog("【Drop temp table】" + tempTableName);} catch (SQLException e) {Log.e(TAG, "【Failed to restore data from temp table 】" + tempTableName, e);}}}


private static List<String> getColumns(Database db, String tableName) {List<String> columns = null;Cursor cursor = null;try {cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 0", null);if (null != cursor && cursor.getColumnCount() > 0) {columns = Arrays.asList(cursor.getColumnNames());}} catch (Exception e) {e.printStackTrace();} finally {if (cursor != null)cursor.close();if (null == columns)columns = new ArrayList<>();}return columns;}


private static void printLog(String info){if(DEBUG){Log.d(TAG, info);}}


private static class TableInfo {int cid;String name;String type;boolean notnull;String dfltValue;boolean pk;


@Overridepublic boolean equals(Object o) {return this == o|| o != null&& getClass() == o.getClass()&& name.equals(((TableInfo) o).name);}


@Overridepublic String toString() {return "TableInfo{" +"cid=" + cid +", name='" + name + ''' +", type='" + type + ''' +", notnull=" + notnull +", dfltValue='" + dfltValue + ''' +", pk=" + pk +'}';}


private static List<TableInfo> getTableInfo(Database db, String tableName) {String sql = "PRAGMA table_info(" + tableName + ")";printLog(sql);Cursor cursor = db.rawQuery(sql, null);if (cursor == null)return new ArrayList<>();TableInfo tableInfo;List<TableInfo> tableInfos = new ArrayList<>();while (cursor.moveToNext()) {tableInfo = new TableInfo();tableInfo.cid = cursor.getInt(0);tableInfo.name = cursor.getString(1);tableInfo.type = cursor.getString(2);tableInfo.notnull = cursor.getInt(3) == 1;tableInfo.dfltValue = cursor.getString(4);tableInfo.pk = cursor.getInt(5) == 1;tableInfos.add(tableInfo);// printLog(tableName + ":" + tableInfo);}cursor.close();return tableInfos;}}}

8. GreenDao 数据库加密

开发中对于存储于数据库中的敏感数据,我们可以通过对数据库加密来进行保护。GreenDao 可以通过 SQLCipher 来进行加密处理。下面我们简单讲解下加密过程:


步骤:


  1. 导入加密库文件:


implementation 'net.zetetic:android-database-sqlcipher:3.5.6'

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
Android GreenDao  使用全面讲解(1),2021Android最新大厂面试真题总结