GreenDao 介紹:
greenDAO是一個(gè)對象關(guān)系映射(ORM)的框架,能夠提供一個(gè)接口通過操作對象的方式去操作關(guān)系型數(shù)據(jù)庫饼煞,它能夠讓你操作數(shù)據(jù)庫時(shí)更簡單浦旱、更方便。
官網(wǎng)地址:http://greenrobot.org/greendao/
github:https://github.com/greenrobot/greenDAO
GreenDao3.2使用方法
1.在項(xiàng)目的build.gradle添加如下配置
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'//greendao
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
2.在app/build.gradle添加如下代碼
apply plugin: 'org.greenrobot.greendao'//greendao
greendao {
schemaVersion 1//數(shù)據(jù)庫schema版本號无畔,通過*OpenHelpers遷移數(shù)據(jù),schema改變值增加吠冤。默認(rèn)為1
daoPackage 'com.bupt.greeddaotest.dao'//生成DAOs浑彰、DaoMaster、DaoSession的包名拯辙。默認(rèn)為entities所在包名郭变。
targetGenDir 'src/main/java'//生成DAOs、DaoMaster涯保、DaoSession的目錄诉濒。默認(rèn)為build/generated/source/greendao
}
dependencies {
compile 'org.greenrobot:greendao:3.2.0'
}
3.新建實(shí)體
@Entity
public class User {
@Id(autoincrement = true)
private Long id;
@NotNull
private String username;
private int age;
private String sex;
private String address;
}
其中
@Entity 用于標(biāo)識這是一個(gè)需要Greendao幫我們生成代碼的bean
@Id 標(biāo)明主鍵,括號里可以指定是否自增
@NotNull 非空
此時(shí)編譯一下自動生成DaoMaster 夕春、DaoSession未荒、Dao,如圖所示 :
4.數(shù)據(jù)庫版本升級
數(shù)據(jù)庫版本升級使用了輔助庫GreenDaoUpgradeHelper
github:https://github.com/yuweiguocn/GreenDaoUpgradeHelper
1)在項(xiàng)目的build.gradle添加
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }//使用數(shù)據(jù)庫升級輔助GreenDaoUpgradeHelper時(shí)添加
}
}
2)在app/build.gradle添加
dependencies {
// 使用數(shù)據(jù)庫升級輔助GreenDaoUpgradeHelper時(shí)添加
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.3.0'
}
3)新建如下類
public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
MigrationHelper.migrate(db,UserDao.class);//版本升級
}
}
一個(gè)簡單的實(shí)例
1)在自定義application里面設(shè)置數(shù)據(jù)庫
public class MeApplication extends Application {
private static MeApplication application;
private MySQLiteOpenHelper openHelper;
private Database db;
private DaoMaster daoMaster;
private DaoSession daoSession;
public static String database_name = "record-db";
@Override
public void onCreate() {
super.onCreate();
application = this;
setDatabase();
}
private void setDatabase() {
openHelper = new MySQLiteOpenHelper(this, database_name, null);
db = openHelper.getWritableDatabase();//獲取可寫數(shù)據(jù)庫
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
public static MeApplication getMeApplication(){
return application;
}
public DaoSession getDaoSession() {
return daoSession;
}
public Database getDb() {
return db;
}
}
2)插入數(shù)據(jù)
/**
* 插入一條記錄
*
*/
public void insertUser(UserDao userDao,User user) {;
userDao.insert(user);
}
/**
* 插入用戶集合
*
*/
public void insertUsers(UserDao userDao,List<User> users) {
if (users == null || users.isEmpty()) {
return;
}
userDao.insertInTx(users);
}
3)刪除數(shù)據(jù)
/**
* 刪除一條記錄
*
*/
public void deleteUser(UserDao userDao,User user) {
userDao.delete(user);
}
/**
* 刪除用戶集合
*
*/
public void deleteUsers(UserDao userDao,List<User> users) {
if (users == null || users.isEmpty()) {
return;
}
userDao.deleteInTx(users);
}
4)更新數(shù)據(jù)
/**
* 更新一條記錄
*
*/
public void updateUser(UserDao userDao,User user) {
userDao.update(user);
}
/**
* 更新用戶集合
*
*/
public void updateUsers(UserDao userDao,List<User> users) {
if (users == null || users.isEmpty()) {
return;
}
userDao.updateInTx(users);
}
5)查詢數(shù)據(jù)
/**
* 查詢用戶列表
*/
public List<User> queryUsers(UserDao userDao) {
List<User> list = userDao.queryBuilder().list();
return list;
}
/**
* 查詢用戶列表
*/
public List<User> queryUsers(UserDao userDao,int age) {
//按照年齡從小到大排列
List<User> list = userDao.queryBuilder().where(UserDao.Properties.Age.gt(age)).orderAsc(UserDao.Properties.Age).list();
return list;
}