greenDAO之初使用

1.项目中引用GitHub地址

在app目录下的build.gradle的目录下:

这里直接复制了整个文件代码

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.greendaotest.www"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    greendao {
        schemaVersion 1//数据库版本号
        daoPackage 'com.greendaotest.www.greendao'//设置DaoMaster、DaoSession、Dao包名
        targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录
        //targetGenDirTest:设置生成单元测试目录
        //generateTests:设置自动生成单元测试用例
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //greendao
    implementation 'org.greenrobot:greendao:3.2.2' // add library
    implementation 'org.greenrobot:greendao-generator:3.2.2'
}

在此下图所示文件中添加

greenDAO之初使用_第1张图片

dependencies {
     classpath 'com.android.tools.build:gradle:3.4.0-rc02'
     classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
        
     // NOTE: Do not place your application dependencies here; they belong
     // in the individual module build.gradle files
}

2.新建 GreenDaoTest.java类

package com.greendaotest.www;

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

@Entity
public class GreenDaoTest {

    @Id(autoincrement = true)
    private Long id;
    private String name;
    private int age;
}

选择build工具栏下的Make Project编译项目或者快捷键Ctrl + F9(Windows系统)

生成此目录

这是编译之后的GreenDaoTest.java文件代码
package com.greendaotest.www;

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

@Entity
public class GreenDaoTest {

    @Id(autoincrement = true)
    private Long id;
    private String name;
    private int age;
    @Generated(hash = 2035792945)
    public GreenDaoTest(Long id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    @Generated(hash = 456386966)
    public GreenDaoTest() {
    }
    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 getAge() {
        return this.age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

3.新建 MyApplication类继承自Application

在AndroidManifest.xml文件中使用




    
        
            
                

                
            
        
    

MyApplication.java文件

在onCreate方法中初始化

package com.greendaotest.www;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;

import com.greendaotest.www.greendao.DaoMaster;
import com.greendaotest.www.greendao.DaoSession;

public class MyApplication extends Application {

    private static final String DB_NAME = "greendaotest.db";

    private DaoMaster.DevOpenHelper mHelper;

    private SQLiteDatabase db;

    private DaoMaster mDaoMaster;

    private static DaoSession mDaoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        initGreenDao();
    }

    private void initGreenDao() {
        mHelper = new DaoMaster.DevOpenHelper(this, DB_NAME);
        db = mHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
    }

    public static DaoSession getmDaoSession() {
        return mDaoSession;
    }
}

4.activity_main.xml布局文件




    

        
MainActivity.java文件
package com.greendaotest.www;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private GreenDaoTest greenDaoTest;

    private int currentNum = 0;

    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.activity_main_tv);
        findViewById(R.id.activity_main_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert();
            }
        });
        findViewById(R.id.activity_main_get_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search();
            }
        });
        findViewById(R.id.activity_main_delete_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete();
            }
        });
    }

    private void insert() {
        currentNum++;
        greenDaoTest = new GreenDaoTest(null, "00" + currentNum, 12 + currentNum);
        MyApplication.getmDaoSession().getGreenDaoTestDao().insert(greenDaoTest);
        Log.e("eee", String.valueOf(currentNum));
    }

    private void search() {
        List greenDaoTests = MyApplication.getmDaoSession().getGreenDaoTestDao().loadAll();
        StringBuffer data = new StringBuffer();
        for (int i = 0; i < greenDaoTests.size(); i++) {
            data.append(greenDaoTests.get(i).getId() + "--" + greenDaoTests.get(i).getName() + "--" + greenDaoTests.get(i).getAge() + "" + "\n");
        }
        tv.setText(data);
        Log.e("eee", data.toString());
    }

    private void delete() {
        MyApplication.getmDaoSession().getGreenDaoTestDao().deleteAll();
        Toast.makeText(this.getApplication(), "删除成功", Toast.LENGTH_SHORT).show();
    }
}

5.运行

greenDAO之初使用_第2张图片

greenDAO之初使用_第3张图片

greenDAO之初使用_第4张图片

OVER

你可能感兴趣的:(Android)