GreenDao配置步骤
Project下的build.gradle文件加入
buildscript {
repositories {
google()
jcenter()
mavenCentral() // 添加代码仓库 步骤1
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
//步骤2
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' //本地Greendao数据库
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
//greendao配置 步骤5 在buildTypes下面添加
greendao {
//数据库版本号,升级时修改
schemaVersion 1
//生成的DAO,DaoMaster和DaoSession的包路径。默认与表实体所在的包路径相同
daoPackage 'com.example.xts.greendaodemo.db'
//生成源文件的路径。默认源文件目录是在build目录中的(build/generated/source/greendao)
targetGenDir 'src/main/java'
}
Module下的build.gradle文件加入
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin 步骤3
android {
........
//greendao配置 步骤5
greendao {
//数据库版本号,升级时修改
schemaVersion 1
//生成的DAO,DaoMaster和DaoSession的包路径。把com.example.xts.greendaodemo修改为与表实体所在的包路径相同(自己的项目主包路径)
daoPackage 'com.mypro.db'
//生成源文件的路径。默认源文件目录是在build目录中的(build/generated/source/greendao)
targetGenDir 'src/main/java'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//greenDAO配置 步骤4
implementation 'org.greenrobot:greendao:3.2.2' // add library
implementation 'org.greenrobot:greendao-generator:3.2.2'
}
//第六步,
//建bean类 实体类和数据库对应,,添加相关注解,,然后编译项目生成相关文件 锤项目
@Entity
public class ColltionDbBean{
@Id(autoincrement = true)
private long id;
private String title;
private String urlPath;
}
//第七步,
//创建一个自己的application类,在application中完成DaoSession的初始化,避免以后重复初始化,便于使用 ,,,要配置到清单中
public class BaseApp extends Application {
private static BaseApp sInstance;
private DaoMaster.DevOpenHelper mHelper;
private DaoMaster mDaoMaster;
private DaoSession mDaoSession;
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
setDatabase();
}
/**
* 设置greenDao
*/
private void setDatabase() {
//通过DaoMaster内部类DevOpenHelper可以获取一个SQLiteOpenHelper 对象
// 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为 greenDAO 已经帮你做了。
// 注意:默认的 DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。
// 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。
// 此处MyDb表示数据库名称 可以任意填写
mHelper = new DaoMaster.DevOpenHelper(this, "MyDb", null); // MyDb是数据库的名字,更具自己的情况修改
SQLiteDatabase db = mHelper.getWritableDatabase();
mDaoMaster = new DaoMaster(db);
mDaoSession = mDaoMaster.newSession();
}
public static BaseApp getInstance(){
return sInstance;
}
public DaoSession getDaoSession(){
return mDaoSession;
}
}
使用方法
数据库的操作包括增删改改查,方法如下:
package com.jackie.greendao;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.List;publicclass MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mName, mAge;
private Button mAdd;
private ListView mListView;
private DaoMaster.DevOpenHelper mDevOpenHelper;
private DaoMaster mDaoMaster;
private DaoSession mDaoSession;
private PersonDao mPersonDao;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initEvent();
openDb();
}
privatevoid initView() {
mName = (EditText) findViewById(R.id.name);
mAge = (EditText) findViewById(R.id.age);
mAdd = (Button) findViewById(R.id.add);
mListView = (ListView) findViewById(R.id.list_view);
}
privatevoid initEvent() {
mAdd.setOnClickListener(this);
}
privatevoid openDb() {
mDevOpenHelper =newDaoMaster.DevOpenHelper(this,"person.db",null);
mDaoMaster =new DaoMaster(mDevOpenHelper.getWritableDb());
mDaoSession = mDaoMaster.newSession();
mPersonDao = mDaoSession.getPersonDao();
}
//插入publicvoid insert() {
Person person =newPerson(Long.valueOf(1), mName.getText().toString(),
Integer.parseInt(mAge.getText().toString()));
mPersonDao.insert(person);
mName.setText("");
mAge.setText("");
}
publicvoid delete(Person person) {
mPersonDao.delete(person);
}
//更新publicvoid update(Person person) {
mPersonDao.insertOrReplace(person);
}
//查询publicvoid query() {
List persons = mPersonDao.queryBuilder().list();
}
@Override
publicvoid onClick(View view) {
switch (view.getId()) {
case R.id.add:
insert();
break;
}
}
}