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类,编译生成我们想要的代码
@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;
}
还有这三个:
序列化一下:
就这样了:
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(主)
DaoMaster构造方法要传入db属性,是可以写啦,还是怎样,这里一般设置可以写(数据库不能写,那要干啥)
这个属性由DaoMaster的DaoMaster.DevOpenHelper提供,里面可以传递上下文,数据库名,还有工厂(这里不用)
DaoSession的话,用DaoMaster的newSession()来获取(里面都帮你搞好了)
BeanTouchDao用DaoSession的getBeanTouchDao获取即可:
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 来删除
mBeanTouchDao.deleteByKey((long) 3);//根据Long来查询
改:也是根据_id 来改
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.下载数据库插件:
2.保存db数据库(这个路径要记住,等下要打开它)
3.添加
4.查看
2.总结反思:
1.编译错误
这个编译时在编写完Bean类的时候,编译的。其实很多都是这样的,比如AIDL的使用,它也是写完AIDL类后,编译才生成我们想要操作的类