DBFlow 使用概要
来源:互联网 发布:河北11选五任6遗漏数据 编辑:程序博客网 时间:2024/06/05 13:33
安装
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
阅读全文
0 0
- DBFlow 使用概要
- DBFLOW的初使用
- DBflow的使用
- DBFlow的初步使用
- DBFlow框架的使用
- Android数据库框架DbFlow使用
- Android学习之数据库DBFlow的使用
- DBFlow学习
- SubVersion使用概要
- iptables 使用概要
- 概要文件的使用
- db2 概要文件使用
- Gson简要使用概要
- BeanUtils使用概要
- 日志使用概要
- DBFlow 学习笔记(一)
- DBFlow for eclipse 移植
- DBFlow 浅显应用
- 全球人才的纳斯达克SelfSell潜入英法、瑞士、新加坡“偷人”?
- Linux克隆后的网卡设置
- 23种设计模式系列之单例模式
- 设置元素disabled属性
- 跳出双重循环--转自易咯
- DBFlow 使用概要
- Linux 显示系统信息的命令
- 卷积神经网络的特点
- Activiti工作流引擎的官方实例配置详细步骤
- python爬虫(一)
- 5个步骤,简单说下我的产品设计方法论
- AI 工程师进阶路线
- 《Java编程技巧1001条》 第345条:使用ceil方法
- 项目中的jdcp改为druid并添加监控