GreenDAO的简单使用

项目地址

1.步骤:

1.注意

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

2.依赖

1.Build.gradle(Project)

全部:

1// Top-level build file where you can add configuration options common to all sub-projects/modules. 2buildscript { 3 4 5 repositories { 6 7 8 9 maven { 10 11 url 'https://maven.aliyun.com/repository/google' } 12 13 maven { 14 15 url 'https://maven.aliyun.com/repository/gradle-plugin' } 16 17 maven { 18 19 url 'https://maven.aliyun.com/repository/public' } 20 21 maven { 22 23 url 'https://maven.aliyun.com/repository/jcenter' } 24 maven { 25 26 url 'https://dl.bintray.com/umsdk/release' } 27 maven { 28 29 url 'https://jitpack.io' } 30 //添加 31 mavenCentral() // add repository 32 } 33 dependencies { 34 35 36 classpath "com.android.tools.build:gradle:4.0.0" 37 //greenDAO数据库 38 classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // add plugin 39 // NOTE: Do not place your application dependencies here; they belong 40 // in the individual module build.gradle files 41 } 42} 43 44allprojects { 45 46 47 repositories { 48 49 50 maven { 51 52 url 'https://maven.aliyun.com/repository/google' } 53 54 maven { 55 56 url 'https://maven.aliyun.com/repository/gradle-plugin' } 57 58 maven { 59 60 url 'https://maven.aliyun.com/repository/public' } 61 62 maven { 63 64 url 'https://maven.aliyun.com/repository/jcenter' } 65 maven { 66 67 url 'https://dl.bintray.com/umsdk/release' } 68 maven { 69 70 url 'https://jitpack.io' } 71 mavenCentral() 72 } 73} 74 75task clean(type: Delete) { 76 77 78 delete rootProject.buildDir 79}

关键:

1 //添加 2 mavenCentral() // add repository 3 4 5//greenDAO数据库 6 classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // add plugin
2.Build.gradle(APP)

全部:

1apply plugin: 'com.android.application' 2apply plugin: 'org.greenrobot.greendao' // apply plugin 3android { 4 5 6 compileSdkVersion 28 7 buildToolsVersion "28.0.1" 8 compileOptions { 9 10 11 sourceCompatibility 1.8 12 targetCompatibility 1.8 13 } 14 defaultConfig { 15 16 17 applicationId "com.kunminx.examplegreendao" 18 minSdkVersion 16 19 targetSdkVersion 28 20 versionCode 1 21 versionName "1.0" 22 23 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 24 } 25 26 buildTypes { 27 28 29 release { 30 31 32 minifyEnabled false 33 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 34 } 35 } 36 //数据库 37 greendao{ 38 39 40 schemaVersion 1 //数据库版本 41 daoPackage 'com.kunminx.examplegreendao.green_dao' 42 targetGenDir 'src/main/java' 43 } 44} 45dependencies { 46 47 48 implementation fileTree(dir: "libs", include: ["*.jar"]) 49 implementation 'androidx.appcompat:appcompat:1.2.0' 50 implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 51 testImplementation 'junit:junit:4.12' 52 androidTestImplementation 'androidx.test.ext:junit:1.1.2' 53 androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 54 implementation 'org.greenrobot:greendao:3.3.0' // add library 55}

关键:

1apply plugin: 'org.greenrobot.greendao' // apply plugin 2 3 4//数据库 5 greendao{ 6 7 8 schemaVersion 1 //数据库版本 9 daoPackage 'com.kunminx.examplegreendao.green_dao'//这里是编译时生成类的路径 10 targetGenDir 'src/main/java' 11 } 12 13 14implementation 'org.greenrobot:greendao:3.3.0' // add library

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

在这里插入图片描述

1@Id 2 private Long id; 3 @Property 4 private String name; 5 @Property 6 private int size; 7 @Property 8 private String listTouch;

生成对应的数据库表的样子:
在这里插入图片描述
它帮你生成这一坨:

1@Generated(hash = 53676501) 2 public BeanTouch(Long id, String name, int size, String listTouch) { 3 4 5 this.id = id; 6 this.name = name; 7 this.size = size; 8 this.listTouch = listTouch; 9 } 10 @Generated(hash = 1046643494) 11 public BeanTouch() { 12 13 14 } 15 public Long getId() { 16 17 18 return this.id; 19 } 20 public void setId(Long id) { 21 22 23 this.id = id; 24 } 25 public String getName() { 26 27 28 return this.name; 29 } 30 public void setName(String name) { 31 32 33 this.name = name; 34 } 35 public int getSize() { 36 37 38 return this.size; 39 } 40 public void setSize(int size) { 41 42 43 this.size = size; 44 } 45 public String getListTouch() { 46 47 48 return this.listTouch; 49 } 50 public void setListTouch(String listTouch) { 51 52 53 this.listTouch = listTouch; 54 }

还有这三个:
在这里插入图片描述

序列化一下:
在这里插入图片描述
就这样了:

1package com.kunminx.examplegreendao; 2 3import android.os.Parcel; 4import android.os.Parcelable; 5 6import org.greenrobot.greendao.annotation.Entity; 7import org.greenrobot.greendao.annotation.Id; 8import org.greenrobot.greendao.annotation.Property; 9import org.greenrobot.greendao.annotation.Generated; 10 11/** 12 * @ClassName User 13 * @Description TODO 14 * @Author ${孙伟豪} 15 * @Date 2020/12/17 17:49 16 * @Version 1.0 17 */ 18@Entity 19public class BeanTouch implements Parcelable { 20 21 22 @Id 23 private Long id; 24 @Property 25 private String name; 26 @Property 27 private int size; 28 @Property 29 private String listTouch; 30 @Generated(hash = 53676501) 31 public BeanTouch(Long id, String name, int size, String listTouch) { 32 33 34 this.id = id; 35 this.name = name; 36 this.size = size; 37 this.listTouch = listTouch; 38 } 39 @Generated(hash = 1046643494) 40 public BeanTouch() { 41 42 43 } 44 public Long getId() { 45 46 47 return this.id; 48 } 49 public void setId(Long id) { 50 51 52 this.id = id; 53 } 54 public String getName() { 55 56 57 return this.name; 58 } 59 public void setName(String name) { 60 61 62 this.name = name; 63 } 64 public int getSize() { 65 66 67 return this.size; 68 } 69 public void setSize(int size) { 70 71 72 this.size = size; 73 } 74 public String getListTouch() { 75 76 77 return this.listTouch; 78 } 79 public void setListTouch(String listTouch) { 80 81 82 this.listTouch = listTouch; 83 } 84 85 @Override 86 public int describeContents() { 87 88 89 return 0; 90 } 91 92 @Override 93 public void writeToParcel(Parcel dest, int flags) { 94 95 96 dest.writeValue(this.id); 97 dest.writeString(this.name); 98 dest.writeInt(this.size); 99 dest.writeString(this.listTouch); 100 } 101 102 protected BeanTouch(Parcel in) { 103 104 105 this.id = (Long) in.readValue(Long.class.getClassLoader()); 106 this.name = in.readString(); 107 this.size = in.readInt(); 108 this.listTouch = in.readString(); 109 } 110 111 public static final Parcelable.Creator<BeanTouch> CREATOR = new Parcelable.Creator<BeanTouch>() { 112 113 114 @Override 115 public BeanTouch createFromParcel(Parcel source) { 116 117 118 return new BeanTouch(source); 119 } 120 121 @Override 122 public BeanTouch[] newArray(int size) { 123 124 125 return new BeanTouch[size]; 126 } 127 }; 128} 129

4.封装使用数据库类

我们的目的是获取BeanTouchDao,它里面就有增删改查了
DaoSession(Dao学级)可以获取BeanTouchDao。
那个可以获取DaoSession呢,就是DaoMaster(主)
在这里插入图片描述

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

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

1public class MyApplication extends Application { 2 3 4 @Override 5 public void onCreate() { 6 7 8 super.onCreate(); 9 DaoTouchManager.getInstance().init(this); 10// DaoTouchManager.getInstance().getDaoMaster(); 11 } 12} 13

6.增删改查:

先获取BeanTouch类:

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

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

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

删除:
根据_id 来删除
在这里插入图片描述

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

改:也是根据_id 来改
在这里插入图片描述

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

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

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

7.查看储存的数据

1.下载数据库插件:

在这里插入图片描述

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

在这里插入图片描述

3.添加

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.查看

在这里插入图片描述
在这里插入图片描述

2.总结反思:

1.编译错误

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

点赞
收藏

评论区

加载中...

相关推荐

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

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )