其他分享
首页 > 其他分享> > Android GreenDao 使用全面讲解,字节跳动面试

Android GreenDao 使用全面讲解,字节跳动面试

作者:互联网

GreenDao中SQL语句的缩写,我们也了解下,源码在Property中,使用的时候可以自己点进去查询即可:

1. 使用QueryBuilder进行查询操作

1. 简单条件查询

查询当前Student表的所有的数据:

public List queryAllList(){
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
QueryBuilder qb = daoSession.queryBuilder(Student.class);
List list = qb.list(); // 查出所有的数据
return list;
}

查询Name为“一”的所有Student:

public List queryListByMessage(String name){
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
QueryBuilder qb = daoSession.queryBuilder(Student.class);
QueryBuilder studentQueryBuilder = qb.where(StudentDao.Properties.Name.eq(“一”)).orderAsc(StudentDao.Properties.Name);
List studentList = studentQueryBuilder.list(); //查出当前对应的数据
return list;
}

2. 原始查询

通过原始的SQL查询语句进行查询!其实上面有提到QueryBuilder的目的就是方便快捷的编写SQL查询语句,避免我们自己在编写过程中出错!简单介绍下通过QueryBuilder编写数据库,方式方法如下 :

public List queryListBySqL(){
// 查询ID大于5的所有学生
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
Query query = daoSession.queryBuilder(Student.class).where(
new WhereCondition.StringCondition("_ID IN " +
“(SELECT _ID FROM STUDENT WHERE _ID > 5)”)
).build();
List list = query.list();
return list;
}

3. 嵌套条件查询

查询Id大于5小于10,且Name值为"一"的数据:

public List queryList(){
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
QueryBuilder qb = daoSession.queryBuilder(Student.class);
qb = daoSession.queryBuilder(Student.class);
List list2 = qb.where(StudentDao.Properties.Name.eq(“一”),
qb.and(StudentDao.Properties.Id.gt(5),
StudentDao.Properties.Id.le(50))).list();
return list2;
}

取10条Id大于1的数据,且偏移2条

public List queryListByOther(){
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
QueryBuilder qb = daoSession.queryBuilder(Student.class);

//搜索条件为Id值大于1,即结果为[2,3,4,5,6,7,8,9,10,11];
// offset(2)表示往后偏移2个,结果为[4,5,6,7,8,9,10,11,12,13];
List list = qb.where(StudentDao.Properties.Id.gt(1)).limit(10).offset(2).list();
return list;
}

4. 多次执行查找

使用QueryBuilder构建查询后,可以重用 Query对象以便稍后执行查询。这比始终创建新的Query对象更有效。如果查询参数没有更改,您可以再次调用list / unique方法。可以通过setParameter方法来修改条件参数值:

public List queryListByMoreTime(){
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
QueryBuilder qb = daoSession.queryBuilder(Student.class);

//搜索条件为Id值大于1,即结果为[2,3,4,5,6,7,8,9,10,11];
// offset(2)表示往后偏移2个,结果为[4,5,6,7,8,9,10,11,12,13];
Query query = qb.where(StudentDao.Properties.Id.gt(1)).limit(10).offset(2).build();
List list = query.list();

//通过SetParameter来修改上面的查询条件,比如我们将上面条件修改取10条Id值大于5,往后偏移两位的数据,方法如下!
query.setParameter(0,5);
List list1 = query.list();
return list1;
}

5. 在多个线程中使用QueryBuilder

如果在多个线程中使用查询,则必须调用 forCurrentThread ()以获取当前线程的Query实例。Query的对象实例绑定到构建查询的拥有线程。

这使您可以安全地在Query对象上设置参数,而其他线程不会干扰。如果其他线程尝试在查询上设置参数或执行绑定到另一个线程的查询,则会抛出异常。像这样,您不需要同步语句。实际上,您应该避免锁定,因为如果并发事务使用相同的Query对象,这可能会导致死锁。

每次调用forCurrentThread ()时, 参数都会在使用其构建器构建查询时设置为初始参数。

2. 使用QueryBuilder进行批量删除操作

使用QueryBuilder进行批量删除操作,不会删除单个实体,但会删除符合某些条件的所有实体。要执行批量删除,请创建QueryBuilder,调用其 buildDelete ()方法,然后执行返回的 DeleteQuery。

例子:删除数据库中id大于5的所有其他数据

public boolean deleteItem(){
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
QueryBuilder where = daoSession.queryBuilder(Student.class).where(StudentDao.Properties.Id.gt(5));
DeleteQuery deleteQuery = where.buildDelete();
deleteQuery.executeDeleteWithoutDetachingEntities();
return false;
}

5. 注解讲解

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

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

@Entity
public class Student {
@Id(autoincrement = true)
Long id;
@Unique
int 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配置一些细节:

@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不行!)。

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

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

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

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

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

3. 索引注解

@Entity
public 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: main
Process: com.example.aserbao.aserbaosandroid, PID: 31939
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: STUDENT.name (Sqlite code 2067), (OS error - 2:No such file or directory)
……

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

4. 关系注解

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

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

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

1. 一对一

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

  1. 我们在Student中设置一个注解@ToOne(joinProperty = “name”)
  2. 在创建Student的时候,将对应的数据传递给IdCard; 代码部分:

学生Student代码:

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

身份证IdCard代码:

@Entity
public class IdCard {
@Id
String userName;//用户名
@Unique
String 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的代码:

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

@Unique
int studentNo;//学号

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

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

CreditCard的代码:

@Entity
public class CreditCard {
@Id
Long 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代码:

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

Student 代码:

@Entity
public class Student {
@Id(autoincrement = true)
Long id;
@Unique
int 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 teacherList;
……getter and setter ……
}

Teacher代码:

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

@ToMany
@JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = “teacherId”,targetProperty = “studentId”)
List 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);
}

@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
@Override
public void onCreateAllTables(Database db, boolean ifNotExists) {
DaoMaster.createAllTables(db, ifNotExists);
}
@Override
public 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 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 = ne
w 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(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】");
}

/**

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;
{
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;

标签:字节,GreenDao,db,class,student,Student,Android,public,String
来源: https://blog.csdn.net/m0_66145060/article/details/122758967