DBFlow 使用概要
Posted AAA啊哈
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DBFlow 使用概要相关的知识,希望对你有一定的参考价值。
安装
https://github.com/Raizlabs/DBFlow/blob/master/README.md
混淆
-keep class * extends com.raizlabs.android.dbflow.config.DatabaseHolder *;
初始化
在自定义 Application
public class MyApplication extends Application
@Override
public void onCreate()
super.onCreate();
FlowManager.init(this);
创建数据库
@Database(name = AppDatabase.NAME, version = AppDatabase.VERSION)
public class AppDatabase
public static final String NAME = "AppDatabase";
public static final int VERSION = 1;
创建表
下面举一个例子,包括 关联自定义 bean, 关联 List
表的注解
@Table(database = AppDatabase.class, allFields = true)
database 指表对应的数据库,allFields 指类中所有的属性都作为表的列来看待,除非加注解@ColumnIgnore
创建基类
import com.raizlabs.android.dbflow.structure.BaseModel;
import java.io.Serializable;
public class BaseBean extends BaseModel implements Serializable
创建 User 表
@Table(database = AppDatabase.class, allFields = true)
public class User extends BaseBean
@PrimaryKey // at least one primary key required
private int id;
private String name;
private int age;
/**
* 设置 @ForeignKey 作为外键,关联 Student
* saveForeignKeyModel: 为 true 指保存自动保存 student 到数据库中,
* false 的话得 自己调用 student.save() 保存
*/
@ForeignKey(saveForeignKeyModel = true)
private Student student;
@ColumnIgnore // 忽略这个属性,不作为列对待
private List<Father> fatherList; // 关联 List
/**
* 使用 @OneToMany 注解来关联列, variableName 是属性名
* @return List<Father>
*/
@OneToMany(methods = OneToMany.Method.ALL, variableName = "fatherList")
public List<Father> getFatherList()
if (fatherList == null || fatherList.isEmpty())
fatherList = SQLite.select()
.from(Father.class)
.where(Father_Table.userId.eq(id))
.queryList();
return fatherList;
public void setFatherList(List<Father> fatherList)
this.fatherList = fatherList;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
public Student getStudent()
return student;
public void setStudent(Student student)
this.student = student;
@Override
public String toString()
return "User" +
"id=" + id +
", name='" + name + '\\'' +
", age=" + age +
", student=" + student +
", fatherList=" + getFatherList() +
'';
创建 Student 表
@Table(database = AppDatabase.class, allFields = true)
public class Student extends BaseBean
@PrimaryKey
private int id;
private String name;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getName()
return name;
public void setName(String name)
this.name = name;
@Override
public String toString()
return "Student" +
"id=" + id +
", name='" + name + '\\'' +
'';
创建 Father 表
@Table(database = AppDatabase.class, allFields = true)
public class Father extends BaseBean
/**
* autoincrement 为 true 指自增
*/
@PrimaryKey(autoincrement = true)
private int id;
private int userId;
private String name;
private int age;
public int getUserId()
return userId;
public void setUserId(int userId)
this.userId = userId;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
@Override
public String toString()
return "Father" +
"id=" + id +
", name='" + name + '\\'' +
", age=" + age +
'';
使用示例
for (int i = 0; i < 25; i++)
User user = new User();
user.setId(i);
user.setName("Afra55 " + i);
Student student = new Student();
student.setId(i);
student.setName("Victor" + i);
user.setStudent(student);
List<Father> list = new ArrayList<>();
for (int j = 100; j < 105; j++)
Father father = new Father();
father.setUserId(user.getId());
father.setAge(j);
father.setName("F " + j);
list.add(father);
user.setFatherList(list);
if (!user.exists())
user.save();
SQLite.select()
.from(User.class)
.async()
.queryListResultCallback(new QueryTransaction.QueryResultListCallback<User>()
@Override
public void onListQueryResult(QueryTransaction transaction, @NonNull List<User> tResult)
List<User> users = tResult;
// do something with users
for (User u : users)
Log.i("U", u.toString());
).error(new Transaction.Error()
@Override
public void onError(@NonNull Transaction transaction, @NonNull Throwable error)
Log.i("U", "error");
).execute();
输出内容:略(好大一堆)
小结
本文只探讨如何关联
自定义类和List数据,
其他高逼格操作看原
Doc https://github.com/agrosner/DBFlowDocs
以上是关于DBFlow 使用概要的主要内容,如果未能解决你的问题,请参考以下文章