0
点赞
收藏
分享

微信扫一扫

安卓数据库之DBFlow

胡桑_b06e 2022-04-27 阅读 30

DBFlow官方文档: https://legacy.gitbook.com/book/yumenokanata/dbflow-tutorials/details

[](()1、什么是DBFlow

​ DBFlow是一个基于AnnotationProcessing(注解处理器)的强大、健壮同时又简单的 ORM框架。此框架设计为了速度、性能和可用性。消除了大量死板的数据库代码,取而代之的 是强大而又简介的API接口。

DBFlow使数据库的操作变得简洁而稳定,让您能够更专注地编写令人惊讶的 APP。

[](()2、为什么使用DBFlow

​ DBFlow的设计吸取了其他很多ORM框架中好的特征,并将之做得更好。它很灵 活,让你能更专注于App中真正需要关注的地方。不要让一个ORM库限制了你的思 维,而是让代码在你的App中工作得更好。

[](()3、DBFlow的简单入门使用(增删改查)

[](()将DBFLow引入到你的工程
[](()①添加DBFlow的托管仓库网址:

allprojects {

repositories {

google()

jcenter()

maven{url"https://jitpack.io"}//添加托管仓库网

}

}

[](()②添加DBFlow的库到App文件夹下的build.gradle文件

annotationProcessor “com.github.Raizlabs.DBFlow:dbflow-processor:4.1.2”

implementation “com.github.Raizlabs.DBFlow:dbflow-core:4.1.2”

implementation “com.github.Raizlabs.DBFlow:dbflow:4.1.2”

接下来就可以在安卓应用中使用DBFlow了

[](()使用DBFlow做简单的增删改查操作
[](()①首先我们要定义数据库类Database类,之后添加Getter和Setter方法

@Database(version = DataBase.VERSION,name = DataBase.NAME)

public class DataBase {

public static final String NAME = “Database”;

public static final int VERSION = 1;

}

[](()②定义Student表与数据库Database关联起来

@com.raizlabs.android.dbflow.annotation.Table(database = DataBase.class)

public class Student extends BaseModel {

@PrimaryKey

long id;

@Column

public String name;

@Column

int age;

public long getId() {

return id;

}

public void setId(long 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;

}

}

Tips:表建好之后要bulid project(快捷键Ctrl+F9)之后会生成对应的XX_Table,这个对后面的操作起到很重要的作用。

[](()③新建数据库操作类MyUtil.java里面进行增删改查操作。

添加数据:

//添加数据

public static void insert(){

Student student = new Student();

student.id = 1;

student.name = “xiaomin”;

student.age = 23;

FlowManager.getModelAdapter(Student.class).insert(student);

System.out.println(“添加数据成功!”);

}

修改数据:

//修改数据

public static void update(){

Student student = SQLite.select()

.from(Student.class)

.where(Student_Table.name.eq(“xiaomin”))

举报

相关推荐

0 条评论