• DBFlow框架的学习笔记之入门



    什么是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篇 未完待续。。。

  • 相关阅读:
    vmvare桥接模式下无法连接网络
    netty LEAK: ByteBuf.release() was not called before it's garbage-collected
    FileOutputStream write与原文件md5不一致,文件变大了 或者 SpringMVC的 ResponseEntity 下载的文件与原文件md5值不一样
    小技巧:linux怎么查看连接的创建时间
    org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out after 300 millisecond(s)
    fastJson转换json字符串到对象中时类型为list的字段为空
    zookeeper集群搭建Exception when following the leader java.io.EOFException
    用ab测试代理程序
    centos7 配置java程序自启动
    log4j2配置模板
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12288803.html
Copyright © 2020-2023  润新知