專案地址
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類后,編譯才生成我們想要操作的類
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/239071.html
標籤:其他
