android中使用greedDao

发布时间 2023-09-14 11:38:04作者: 不卷轮子锅

 android中使用greedDao

一 工具说明

Greendao是一个在android中快速生成数据库操作的orm工具,最近在项目中使用到,具体操作记录如下,供以后使用快速集成。
该项目的源码在gitee的镜像地址为:【https://gitee.com/freewsf/greenDAO_1#add-greendao-to-your-project】

二 使用方式


1.在project的buildgrade中添加依赖仓库地址

buildscript {
    repositories {
        jcenter()
        google()
        mavenCentral()  //greendao plugin托管在中央仓库,要添加这一个
    }
    dependencies {
        classpath  'com.android.tools.build:gradle:3.5.3'
        classpath  'org.greenrobot:greendao-gradle-plugin:3.3.0' // 添加插件

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

 

2. 在app的buildgradle中添加插件的应用规则及依赖

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'   //应用插件

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    defaultConfig {
        applicationId "com.xxx.xxx"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 20210820
        versionName "2.0"
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

        ndk {
            // 设置支持的SO库架构,'armeabi' , 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
            abiFilters  'armeabi','armeabi-v7a', 'arm64-v8a', 'x86'
        }

        sourceSets {
            main {
                jniLibs.srcDirs = ['libs']
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    //so 库目录加入
    sourceSets.main.jniLibs.srcDirs = ['libs']

    //定义greendao 自动生成dao代码的路径和规则
    greendao {
        //指定数据库schema版本号,迁移等操作会用到;
        schemaVersion 1
        //dao的包名,包名默认是entity所在的包daoPackage 'com.xxx.yyy.dbutil.dao'
        //生成数据库文件的目录;
        targetGenDir 'src/main/java'
    }

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'], exclude: [])
androidTestCompile('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.zxing:core:3.3.0'
testImplementation 'junit:junit:4.12'

//腾讯的键值对存储mmkv
implementation 'com.tencent:mmkv:1.0.22'

//添加lombok包支持
compile 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'


//greenDao
implementation 'org.greenrobot:greendao:3.3.0'
//gson
implementation 'com.google.code.gson:gson:2.8.5'

}
 

3. 添加一个实体类,比如

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;

import java.io.Serializable;
import java.util.Date;

import lombok.Builder;
import lombok.Data;
import org.greenrobot.greendao.annotation.Generated;
1
2
3
4
5
6
7
8
9
10
11
12
13
<br>@Entity
@Data
@Builder
public class MachineOrder implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id(autoincrement = true)
    private Long id;
    private String machineOrderNo;
    private String uploadAlready;
    private String rawQrCodeInfo;
    private Date createTime;
    private Date uploadTime;
}

 

4. 准备完毕,点击  Build->make project.

这时候可以看到,在我们配置的  daoPackage 'com.xxx.yyy.dbutil.dao'  这个目录下面,已经自动生成了