安卓数据库之DBFlow


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"))
                .querySingle();
        if(student != null){
            student.name = "xiaohong";
            student.update();
            System.out.println("修改数据成功!");
        }
        
    }

查询全部:

//查询全部
    public static List<Student> selectAll(){
        List<Student> studnets = SQLite.select()
                .from(Student.class)
                .where()
                .queryList();//返回的list不为空,可能为empty
        return studnets;
    }

删除数据:

//删除数据
    public static void delete(){
        Student student = SQLite.select()
                .from(Student.class)
                .where(Student_Table.name.eq("xiaomin"))
                .querySingle();
        if(student != null){
            student.delete();
            System.out.println("删除数据成功!");
        }
    }
④最后在MainActivity中初始化和运行。
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FlowManager.init(this);
        insert();
        List<Student> students = selectAll();
        for (int i=0; i< students.size(); i++){
            Student student = students.get(i);
            System.out.println("姓名:"+student.name
                                +"年龄"+student.age);
        }
        //update();
        //selectAll();
        // delete();
        //selectAll();
    }
}
参考博客: https://blog.csdn.net/yangchanghong1995/article/details/80635392 

插入运行结果

项目源码地址:https://github.com/yourslai/DBFlowTest

作者:赖伟潮 116052017179

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐