什么是DBFlow?

dbflow是一款android高性的ORM数据库.可以使用在进行项目中有关数据库的操作。github下载源码

1、环境配置

  1. 先导入 apt plugin库到你的classpath,以启用AnnotationProcessing(注解处理器):在工程的根目录下build.gradle代码如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    buildscript {
    repositories {
    jcenter()
    }
    dependencies {
    classpath 'com.android.tools.build:gradle:2.2.2'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
    }
    allprojects {
    repositories {
    jcenter()
    maven { url "https://www.jitpack.io" }
    }
    }

添加 classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’
和 maven { url “https://www.jitpack.io“ }

  1. 添加库到项目级别的build.gradle文件中

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    apply plugin: 'com.android.application'
    apply plugin: 'com.neenbedankt.android-apt'
    def dbflow_version = "3.0.0-beta4"
    android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
    applicationId "com.soildog.testdbflow"
    minSdkVersion 21
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }
    }
    dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    //support
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.android.support:design:25.0.1'
    // dbflow
    apt "com.github.Raizlabs.DBFlow:dbflow-processor:${dbflow_version}"
    compile "com.github.Raizlabs.DBFlow:dbflow-core:${dbflow_version}"
    compile "com.github.Raizlabs.DBFlow:dbflow:${dbflow_version}"
    }

2.初始化DBFlow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class extends Application {
private static App mContext;
public void onCreate() {
super.onCreate();
FlowManager.init(this);
}
//单例模式
public static App getInstance(){
if (mContext == null) {
mContext = new App();
}
return mContext;
}
}

记得添加到AndroidManifest.xml中,name = “App”


3、创建数据库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import com.raizlabs.android.dbflow.annotation.Database;
/**
* Created by 24277 on 2016/12/6.
*/
//以注释的方式创建
@Database(name = AppDatabase.NAME,version = AppDatabase.VERSION)
public class AppDatabase {
//数据库名
public static final String NAME = "AppDatabase";
//数据库的版本号
public static final int VERSION = 1;
}

4、创建数据库表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import com.raizlabs.android.dbflow.annotation.Column;
import com.raizlabs.android.dbflow.annotation.ModelContainer;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.structure.BaseModel;
/**
* Created by 24277 on 2016/12/6.
*/
//这个表从属于AppDatabase数据库
@ModelContainer
@Table(database = AppDatabase.class)
public class Student extends BaseModel{
//注释表示创建一列 名叫id
@Column
//主键,自增长
@PrimaryKey(autoincrement = true)
public long id;
@Column
public String name;
@Column
大专栏  DBFlow框架的学习笔记之入门lass="line"> public String sex;
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
", sex='" + sex + ''' +
'}';
}
}

(1)对类添加@Table注解
(2) 声明所连接的数据库类,这里是DBFlowDatabase。
(3)定义至少一个主键。
(4)这个类和这个类中数据库相关列的修饰符必须是包内私有或者public,这样生成的_Adapter类能够访问到它。


创建完成后,需要编译一下,点击编译按钮,或者Build->Make Project即可,它会自动生成一些数据库文件,也会提示你创建是否有误!

如果编译通过会生成一些类,位置:TestDBFLowappbuildgeneratedsourceaptdebugcomsoildogtestdbflowentity
如:Student_Table(在下文中很重要)


5、创建一个管理类用于对数据的增删该查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.soildog.testdbflow.manager;
import android.content.Context;
import android.widget.Toast;
import com.raizlabs.android.dbflow.sql.language.Select;
import com.soildog.testdbflow.App;
import com.soildog.testdbflow.entity.Student;
import com.soildog.testdbflow.entity.Student_Table;
import java.util.List;
/**
* Created by 24277 on 2016/12/6.
*/
public class DBFLowManager {
private Context mContext;
private Student mStudent;
public DBFLowManager() {
mContext = App.getInstance();
mStudent = new Student();
}
public Student queryData(String name) {
Student record = new Select()
.from(Student.class)
.where(Student_Table.name.eq(name))
.querySingle();
return record;
}
public void insert(int id,String name,String sex){
mStudent = queryData(name);
if (mStudent == null) {
mStudent = new Student();
mStudent.name = name;
mStudent.sex = sex;
}
mStudent.save();
//people.update();//更新对象
//people.delete();//删除对象
//people.insert();//插入对象;
}
public List<Student> getData() {
List<Student> record = new Select()
.from(Student.class)
.queryList();
return record;
}
public void deletAllData(String name) {
List<Student> record = new Select()
.from(Student.class)
//.where("name = ?",new String[]{name})
.queryList();
for (Student student : record) {
student.delete();
}
}
public void upData(int id,String name,String sex){
Student record = new Select()
.from(Student.class)
//.where("id = ?",new String[]{""+id})
.querySingle();
if (record == null){
Toast.makeText(mContext, "没有更改的数据", Toast.LENGTH_SHORT).show();
return;
}else{
record.name = name;
record.sex = sex;
record.save();
Toast.makeText(mContext, "更新成功", Toast.LENGTH_SHORT).show();
}
}
public List<Student> queryAll() {
List<Student> record = new Select()
.from(Student.class)
.queryList();
return record;
}
}

ps:Student_Table是如何出现的那?->>是自动编译生成的。

6、最后就是写一个测试类去测试一下,这里就不多说了。。。

参考:

  1. dbflow的学习
  2. DBFLOW的初使用

android篇 未完待续。。。

上一篇:recycerlerView大小图片适配问题

下一篇:Hello World

最新文章

  1. Android 内存管理 &amp;Memory Leak &amp; OOM 分析
  2. 解读SDN的东西、南北向接口
  3. 2015 MVP OpenDay 及 Community Camp 演讲PPT分享
  4. 使用 CSS3 伪元素实现立体的照片堆叠效
  5. Intent学习笔记
  6. hdu 2053 Switch Game 水题一枚,鉴定完毕
  7. 丢掉 WinPE,使用 DISKPART 来分区吧
  8. linux下查看磁盘空间
  9. 把WinXP装进内存 性能飚升秒杀固态硬盘
  10. ios runtime部分事例方法说明
  11. 201521123052 《Java程序设计》 第14周学习总结
  12. Troubleshooting tips for using Java on Windows 8
  13. 第三次Java作
  14. udp套接字及利用socketserver模块实现并发以及并发编程
  15. C++学习札记(1)
  16. myBatis插件(plugins)
  17. C# RichTextBox的用法
  18. sqler sql 转rest api 的docker image
  19. mongodb命令(1)
  20. C#中简单的文件操作实例

热门文章

  1. Vue插件配置和 后台交互
  2. "finally block does not complete normally"警告解决
  3. ANT下载和配置 IDEA
  4. 使用OkHttp上传图片到服务器
  5. Java--java中的基本数据类型以及对应的包装类
  6. C#面向对象---对象成员、方法加载、引用类库
  7. svn http
  8. rsync备份(一)
  9. erp和crm的区别
  10. arduino驱动oled