GreenDAO的简单使用

Stella981
• 阅读 421

项目地址

1.步骤:

1.注意

这个是编译时技术,就是要过程中要记得编译,不然没有出来想要的代码

2.依赖

1.Build.gradle(Project)

全部:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
   
   
    repositories {
   
   

        maven {
   
    url 'https://maven.aliyun.com/repository/google' }

        maven {
   
    url 'https://maven.aliyun.com/repository/gradle-plugin' }

        maven {
   
    url 'https://maven.aliyun.com/repository/public' }

        maven {
   
    url 'https://maven.aliyun.com/repository/jcenter' }
        maven {
   
    url 'https://dl.bintray.com/umsdk/release' }
        maven {
   
    url 'https://jitpack.io' }
        //添加
        mavenCentral() // add repository
    }
    dependencies {
   
   
        classpath "com.android.tools.build:gradle:4.0.0"
        //greenDAO数据库
        classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // add plugin
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
   
   
    repositories {
   
   
        maven {
   
    url 'https://maven.aliyun.com/repository/google' }

        maven {
   
    url 'https://maven.aliyun.com/repository/gradle-plugin' }

        maven {
   
    url 'https://maven.aliyun.com/repository/public' }

        maven {
   
    url 'https://maven.aliyun.com/repository/jcenter' }
        maven {
   
    url 'https://dl.bintray.com/umsdk/release' }
        maven {
   
    url 'https://jitpack.io' }
        mavenCentral()
    }
}

task clean(type: Delete) {
   
   
    delete rootProject.buildDir
}

关键:

        //添加
        mavenCentral() // add repository


//greenDAO数据库
        classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // add plugin
2.Build.gradle(APP)

全部:

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin
android {
   
   
    compileSdkVersion 28
    buildToolsVersion "28.0.1"
    compileOptions {
   
   
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
    defaultConfig {
   
   
        applicationId "com.kunminx.examplegreendao"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
   
   
        release {
   
   
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    //数据库
    greendao{
   
   
        schemaVersion 1 //数据库版本
        daoPackage 'com.kunminx.examplegreendao.green_dao'
        targetGenDir 'src/main/java'
    }
}
dependencies {
   
   
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'org.greenrobot:greendao:3.3.0' // add library
}

关键:

apply plugin: 'org.greenrobot.greendao' // apply plugin


//数据库
    greendao{
   
   
        schemaVersion 1 //数据库版本
        daoPackage 'com.kunminx.examplegreendao.green_dao'//这里是编译时生成类的路径
        targetGenDir 'src/main/java'
    }


implementation 'org.greenrobot:greendao:3.3.0' // add library

3.写bean类,编译生成我们想要的代码

GreenDAO的简单使用

@Id
    private Long id;
    @Property
    private String name;
    @Property
    private int size;
    @Property
    private String listTouch;

生成对应的数据库表的样子:
GreenDAO的简单使用
它帮你生成这一坨:

@Generated(hash = 53676501)
    public BeanTouch(Long id, String name, int size, String listTouch) {
   
   
        this.id = id;
        this.name = name;
        this.size = size;
        this.listTouch = listTouch;
    }
    @Generated(hash = 1046643494)
    public BeanTouch() {
   
   
    }
    public Long getId() {
   
   
        return this.id;
    }
    public void setId(Long id) {
   
   
        this.id = id;
    }
    public String getName() {
   
   
        return this.name;
    }
    public void setName(String name) {
   
   
        this.name = name;
    }
    public int getSize() {
   
   
        return this.size;
    }
    public void setSize(int size) {
   
   
        this.size = size;
    }
    public String getListTouch() {
   
   
        return this.listTouch;
    }
    public void setListTouch(String listTouch) {
   
   
        this.listTouch = listTouch;
    }

还有这三个:
GreenDAO的简单使用

序列化一下:
GreenDAO的简单使用
就这样了:

package com.kunminx.examplegreendao;

import android.os.Parcel;
import android.os.Parcelable;

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

/**
 * @ClassName User
 * @Description TODO
 * @Author ${孙伟豪}
 * @Date 2020/12/17 17:49
 * @Version 1.0
 */
@Entity
public class BeanTouch implements Parcelable {
   
   
    @Id
    private Long id;
    @Property
    private String name;
    @Property
    private int size;
    @Property
    private String listTouch;
    @Generated(hash = 53676501)
    public BeanTouch(Long id, String name, int size, String listTouch) {
   
   
        this.id = id;
        this.name = name;
        this.size = size;
        this.listTouch = listTouch;
    }
    @Generated(hash = 1046643494)
    public BeanTouch() {
   
   
    }
    public Long getId() {
   
   
        return this.id;
    }
    public void setId(Long id) {
   
   
        this.id = id;
    }
    public String getName() {
   
   
        return this.name;
    }
    public void setName(String name) {
   
   
        this.name = name;
    }
    public int getSize() {
   
   
        return this.size;
    }
    public void setSize(int size) {
   
   
        this.size = size;
    }
    public String getListTouch() {
   
   
        return this.listTouch;
    }
    public void setListTouch(String listTouch) {
   
   
        this.listTouch = listTouch;
    }

    @Override
    public int describeContents() {
   
   
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
   
   
        dest.writeValue(this.id);
        dest.writeString(this.name);
        dest.writeInt(this.size);
        dest.writeString(this.listTouch);
    }

    protected BeanTouch(Parcel in) {
   
   
        this.id = (Long) in.readValue(Long.class.getClassLoader());
        this.name = in.readString();
        this.size = in.readInt();
        this.listTouch = in.readString();
    }

    public static final Parcelable.Creator<BeanTouch> CREATOR = new Parcelable.Creator<BeanTouch>() {
   
   
        @Override
        public BeanTouch createFromParcel(Parcel source) {
   
   
            return new BeanTouch(source);
        }

        @Override
        public BeanTouch[] newArray(int size) {
   
   
            return new BeanTouch[size];
        }
    };
}

4.封装使用数据库类

我们的目的是获取BeanTouchDao,它里面就有增删改查了
DaoSession(Dao学级)可以获取BeanTouchDao。
那个可以获取DaoSession呢,就是DaoMaster(主)
GreenDAO的简单使用

DaoMaster构造方法要传入db属性,是可以写啦,还是怎样,这里一般设置可以写(数据库不能写,那要干啥)
这个属性由DaoMaster的DaoMaster.DevOpenHelper提供,里面可以传递上下文,数据库名,还有工厂(这里不用)
GreenDAO的简单使用
DaoSession的话,用DaoMaster的newSession()来获取(里面都帮你搞好了)
GreenDAO的简单使用
BeanTouchDao用DaoSession的getBeanTouchDao获取即可:
GreenDAO的简单使用

5.初始化参数:这里用全局变量

public class MyApplication extends Application {
   
   
    @Override
    public void onCreate() {
   
   
        super.onCreate();
        DaoTouchManager.getInstance().init(this);
//        DaoTouchManager.getInstance().getDaoMaster();
    }
}

6.增删改查:

先获取BeanTouch类:

mBeanTouchDao = DaoTouchManager.getInstance().getDaoSession().getBeanTouchDao();

记得要给读写权限哦,不然你会很开心的
增:

String name="孙伟豪";
            int size=100;
            String listTouch="sdfsdfsdf";
            BeanTouch mBeanTouch=new BeanTouch(null,name,size,listTouch);
            mBeanTouchDao.insert(mBeanTouch);

删除:
根据_id 来删除
GreenDAO的简单使用

mBeanTouchDao.deleteByKey((long) 3);//根据Long来查询

改:也是根据_id 来改
GreenDAO的简单使用

BeanTouch beanTouch=new BeanTouch((long) 7,"孙伟豪改",200,"改变成功");
        mBeanTouchDao.update(beanTouch);

查:
这里是根据第几个来查:

BeanTouch beanTouch = mBeanTouchDao.queryBuilder().build().list().get(0);
        Log.d(TAG, "query: beanTouch:"+beanTouch.getName());

7.查看储存的数据

1.下载数据库插件:

GreenDAO的简单使用

2.保存db数据库(这个路径要记住,等下要打开它)

GreenDAO的简单使用

3.添加

GreenDAO的简单使用
GreenDAO的简单使用
GreenDAO的简单使用
GreenDAO的简单使用
GreenDAO的简单使用
GreenDAO的简单使用

4.查看

GreenDAO的简单使用
GreenDAO的简单使用

2.总结反思:

1.编译错误

这个编译时在编写完Bean类的时候,编译的。其实很多都是这样的,比如AIDL的使用,它也是写完AIDL类后,编译才生成我们想要操作的类

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
暗箭伤人 暗箭伤人
7个月前
【www.ithunter.club】 20230922下午
不容易的2023年,我们一起努力【www.ithunter.club】(2023092208:00:00.8872062023092216:00:00.887206)1.人事招聘专员数名(可选远程或入职)2.招聘向坐标东京Yahoo、Shift、L
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这