1. 程式人生 > >【greenDAO3】 專案搭建與增刪改查操作

【greenDAO3】 專案搭建與增刪改查操作

    最近需要開始一個新的專案了,考慮到既然是新專案了,那麼一些常用的框架肯定也要用當下最火的!這次的新專案中涉及到了本地資料儲存,很早前有個專案的本地資料庫框架用的是ActiveAndroid,github找了下這個框架,發現已經兩年多已經沒有更新了。然後就想到了一直沒有時間去涉及到的greenDAO,github一搜索,哦呦?star有5000+,並且依然保持著很高的更新頻率,並且效能遠遠的高於activeAndroid(見下圖),果斷選用。


    剛開始想偷偷懶,大致瀏覽了下greenDAO官網後就開始百度一些相關教程(畢竟看中文的速度快啊!!!)。找教程中也看到了很多質量很高的文章,但是看上去都有點怪怪的,感覺和官網的最新版本介紹的完全不是一個東西,主要是在導包以及生成程式碼的方式。最後發現是因為greenDAO2和3的構建和生成程式碼的方式變化了不少,所以最後還是老老實實的回去看官文了,廢話了這麼多,下面開始正題!!

【一】greenDAO3基本介紹

    greenDAO3開始使用註解的方式定義實體類(entity),並且是通過安裝gradle外掛來生成程式碼。之前的版本則是通過建立一個獨立的java工程來存放生成的檔案。

【二】匯入相關的包

    compile 'org.greenrobot:greendao:3.0.1'
    compile 'org.greenrobot:greendao-generator:3.0.0'
  
  這一步很簡單,在gradle的dependencies中匯入最新版本的依賴就可以了

【三】配置gradle

    這一步非常關鍵,這是整個grennDAO3改變的核心所在了。先加入如下程式碼

apply plugin: 'org.greenrobot.greendao'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'
    }
}
    在gradle的根模組中加入上述程式碼後,sync project的時候,gradle會自動去maven倉庫下載一個gradle的外掛,當然了,這個外掛就是為greenDAO服務的,用來生成資料庫相關的程式碼。

    簡單的介紹下通過gradle外掛生成資料庫程式碼的步驟:每次在make project之前,它會掃描專案中所有的@Entity檔案(greenDAO中資料庫的實體類),根據實體類生成DaoSession、DaoMaster以及所有實體類的dao類,生成的檔案預設目錄為:build/generated/source/greendao,若不想修改生成的路徑,可以將此路徑設定為資源目錄。我們也可以自定義這個路徑,下面就來介紹如何在gradle中配置greenDAO的相關屬性:

greendao {
    schemaVersion 1
    daoPackage 'com.wyk.greendaodemo.greendao.gen'
    targetGenDir 'src/main/java'
}
    在gradle的根模組中加入上述程式碼,就完成了我們的基本配置了。

    schemaVersion---->指定資料庫schema版本號,遷移等操作會用到

    daoPackage-------->通過gradle外掛生成的資料庫相關檔案的包名,預設為你的entity所在的包名

    targetGenDir-------->這就是我們上面說到的自定義生成資料庫檔案的目錄了,可以將生成的檔案放到我們的java目錄中,而不是build中,這樣就不用額外的設定資源目錄了

    貼上完整程式碼圖:

  

【四】編寫entity類

通過greenDAO3註解的語法來定義我們的一個測試資料庫實體類:

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

@Entity
public class User {
    @Id
    private Long id;
    private String name;
    @Transient
    private int tempUsageCount; // not persisted
}
@Entity:將我們的java普通類變為一個能夠被greenDAO識別的資料庫型別的實體類

@Id:通過這個註解標記的欄位必須是Long型別的,這個欄位在資料庫中表示它就是主鍵,並且它預設就是自增的

@Transient:表明這個欄位不會被寫入資料庫,只是作為一個普通的java類欄位,用來臨時儲存資料的,不會被持久化

接下來讓我們點選as中Build選單欄中的Make Project,make完成之後會發現我們的User類中突然多了好多程式碼,這就是greenDAO自動為你生成的了,程式碼如下

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

@Entity
public class User {
    @Id
    private Long id;
    private String name;
    @Transient
    private int tempUsageCount; // not persisted
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @Generated(hash = 873297011)
    public User(Long id, String name) {
        this.id = id;
        this.name = name;
    }
    @Generated(hash = 586692638)
    public User() {
    }
}
    上述生成的程式碼中包含了構造方法以及每個屬性的getter setter方法。除了這個變化,還會發現在我們之前設定的targetGenDir這個目錄中多了三個檔案,之後所有相關的資料庫操作都依靠這三個檔案了。


【五】增刪改查操作

    鋪墊了這麼多,終於能見到我們資料庫的核心功能了~

    1.第一步肯定是初始化資料庫

DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MyApplication.getContext(), "notes-db", null);
DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
    “notes-db”是我們自定的資料庫名字,應為我們之前建立了一個Entity叫做User,所以greenDAO自定幫我們生成的UserDao,拿到了這個UserDao,我們就可以操作User這張表了。

    一個DaoMaster就代表著一個數據庫的連線;DaoSession可以讓我們使用一些Entity的基本操作和獲取Dao操作類,DaoSession可以建立多個,每一個都是屬於同一個資料庫連線的。

    2.插入資料

User user = new User(null, "wyk");
userDao.insert(user);
    非常簡單,例項化一個User物件,然後呼叫userDao的insert方法就可以了。將User物件的id設定為null的時候,資料庫會自動為其分配自增的id。

    3.查詢資料

List<User> userList = userDao.queryBuilder()
       .where(UserDao.Properties.Id.notEq(999))
       .orderAsc(UserDao.Properties.Id)
       .limit(5)
       .build().list();

    通過userDao的queryBuilder()方法,生成一個查詢構造器,可以給構造器新增where條件判斷、按照某某欄位排序以及查詢的條數等基本的資料庫操作。list()方法表示查詢的結果為一個集合.

    4.修改資料

User findUser = userDao.queryBuilder().where(UserDao.Properties.Name.eq("wyk")).build().unique();
if(findUser != null) {
    findUser.setName(newName);
    userDao.update(findUser);
    Toast.makeText(MyApplication.getContext(), "修改成功", Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(MyApplication.getContext(), "使用者不存在", Toast.LENGTH_SHORT).show();
}
    修改資料的第一步是把需要修改的條目給查詢出來,然後修改該條目,最後呼叫userDao的update方法即可。unique()表示查詢結果為一條資料,若資料不存在,findUser為null。

    5.刪除資料

User findUser = userDao.queryBuilder().where(UserDao.Properties.Name.eq("wyk")).build().unique();
if(findUser != null){
    userDao.deleteByKey(findUser.getId());
}

    刪除資料和更新資料基本相似,先查詢出需要刪除的條目,然後呼叫userDao的deleteByKey將該條目的主鍵傳入即可刪除。

    6.自定義sql語句

ChatHistoryDao dao = GreenDaoManager.getInstance().getSession().getChatHistoryDao();
Cursor cursor = dao.getDatabase().rawQuery("select t.sales_wx_nick_name,t.wx_nick_name,count(*),t.talker_id,t.sales_wx_account from chat_history t group by t.talker_id,t.sales_wx_account order by t.created_at desc", null);
while (cursor.moveToNext()) {
    String salesWxNickName = cursor.getString(0);
    String clientWxNickName = cursor.getString(1);
    int chatCount = cursor.getInt(2);
    int talkerId = cursor.getInt(3);
    String salesWxAccount = cursor.getString(4);
}
    有的時候需要用到group by或者left join等複雜的語句,可以呼叫android原生的sqlite去進行查詢。

    7.資料庫升級

如果某張表修改了欄位,或者新增了一張表,必須要修改build.gradle中的schemaVersion,否則當你升級app的時候,如果進行了資料庫操作,會發現列不匹配或者表不存在等問題,直接會導致app閃退。但是如果僅僅是將schemaVersion加1,雖然程式不會崩潰,並且資料表的結構也會更新成功,但是之前表中的資料會全部清空。我們需要進行手動操作來進行資料庫裡面的資料遷移,大致的思路是:建立臨時表(結構與上一版本的表結構相同),將舊資料移到臨時表中,刪除舊版本的表,建立新版本的表,將臨時表中的資料轉移到新表中,最後再刪除臨時表。詳細方法見連結:

【結尾】greenDAO還有很多高階的功能,比如說給表設定一對一、一對多、多對多的關係以及複用查詢語句等等等太多功能,本片文章僅僅做一個入門教學,其餘的功能小夥伴們可以去官網教程中自行查閱咯~最後貼上整個demo的主要程式碼

build.gradle

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

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'
    }
}

greendao {
    schemaVersion 1
    daoPackage 'com.wyk.greendaodemo.greendao.gen'
    targetGenDir 'src/main/java'
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.wyk.greendaodemo"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'org.greenrobot:greendao:3.0.1'
    compile 'org.greenrobot:greendao-generator:3.0.0'
}

User.java

package com.wyk.greendaodemo.greendao.entity;

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

@Entity
public class User {
    @Id
    private Long id;
    private String name;
    @Transient
    private int tempUsageCount; // not persisted
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @Generated(hash = 873297011)
    public User(Long id, String name) {
        this.id = id;
        this.name = name;
    }
    @Generated(hash = 586692638)
    public User() {
    }
}

MainActivity.java

package com.wyk.greendaodemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.wyk.greendaodemo.greendao.GreenDaoManager;
import com.wyk.greendaodemo.greendao.entity.User;
import com.wyk.greendaodemo.greendao.gen.UserDao;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener {
    private EditText mNameET;
    private Button mAddBtn;
    private ListView mUserLV;

    private UserAdapter mUserAdapter;
    private List<User> mUserList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initView() {
        mNameET = (EditText) findViewById(R.id.et_name);
        mAddBtn = (Button) findViewById(R.id.btn_add);
        mUserLV = (ListView) findViewById(R.id.lv_user);

        mAddBtn.setOnClickListener(this);
    }

    private void initData() {
        mUserList = GreenDaoManager.getInstance().getSession().getUserDao().queryBuilder().build().list();
        mUserAdapter = new UserAdapter(this, mUserList);
        mUserLV.setAdapter(mUserAdapter);
    }

    /**
     * 根據名字更新某條資料的名字
     * @param prevName  原名字
     * @param newName  新名字
     */
    private void updateUser(String prevName,String newName){
        User findUser = GreenDaoManager.getInstance().getSession().getUserDao().queryBuilder()
                .where(UserDao.Properties.Name.eq(prevName)).build().unique();
        if(findUser != null) {
            findUser.setName(newName);
            GreenDaoManager.getInstance().getSession().getUserDao().update(findUser);
            Toast.makeText(MyApplication.getContext(), "修改成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MyApplication.getContext(), "使用者不存在", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 根據名字刪除某使用者
     * @param name
     */
    private void deleteUser(String name){
        UserDao userDao = GreenDaoManager.getInstance().getSession().getUserDao();
        User findUser = userDao.queryBuilder().where(UserDao.Properties.Name.eq(name)).build().unique();
        if(findUser != null){
            userDao.deleteByKey(findUser.getId());
        }
    }

    /**
     * 本地資料裡新增一個User
     * @param id  id
     * @param name  名字
     */
    private void insertUser(Long id, String name) {
        UserDao userDao = GreenDaoManager.getInstance().getSession().getUserDao();
        User user = new User(id, name);
        userDao.insert(user);
        mNameET.setText("");

        mUserList.clear();
        mUserList.addAll(userDao.queryBuilder().build().list());
        mUserAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId){
            case R.id.btn_add:
                insertUser(null, mNameET.getText().toString());
                break;
            default:
                break;
        }
    }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.wyk.greendaodemo.MainActivity">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add one" />


    <ListView
        android:id="@+id/lv_user"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
GreenDaoManager.java(greenDao管理類)
package com.wyk.greendaodemo.greendao;

import com.wyk.greendaodemo.MyApplication;
import com.wyk.greendaodemo.greendao.gen.DaoMaster;
import com.wyk.greendaodemo.greendao.gen.DaoSession;

/**
 * Created by wyk on 2016/7/12.
 */
public class GreenDaoManager {
    private static GreenDaoManager mInstance;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;


    private GreenDaoManager() {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MyApplication.getContext(), "notes-db", null);
        DaoMaster mDaoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();
    }

    public static GreenDaoManager getInstance() {
        if (mInstance == null) {
            mInstance = new GreenDaoManager();
        }
        return mInstance;
    }

    public DaoMaster getMaster() {
        return mDaoMaster;
    }

    public DaoSession getSession() {
        return mDaoSession;
    }

    public DaoSession getNewSession() {
        mDaoSession = mDaoMaster.newSession();
        return mDaoSession;
    }
}
MyApplication.java
package com.wyk.greendaodemo;

import android.app.Application;
import android.content.Context;

import com.wyk.greendaodemo.greendao.GreenDaoManager;

/**
 * Created by wyk on 2016/7/12.
 */
public class MyApplication extends Application {
    private static Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        GreenDaoManager.getInstance();
    }

    public static Context getContext() {
        return mContext;
    }
}

UserAdapter.java
package com.wyk.greendaodemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wyk.greendaodemo.greendao.entity.User;

import java.util.List;

/**
 * Created by wyk on 2016/7/12.
 */
public class UserAdapter extends BaseAdapter {
    private List<User> mUserList;
    private Context mContext;

    public UserAdapter(Context mContext, List<User> mUserList) {
        this.mUserList = mUserList;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mUserList == null ? 0 : mUserList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_user, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        User user = mUserList.get(position);
        viewHolder.tv_id.setText(String.valueOf(user.getId()));
        viewHolder.tv_name.setText(user.getName());

        return convertView;
    }

    class ViewHolder {
        TextView tv_id;
        TextView tv_name;
    }
}
 

專案結構


文章如果有沒能解釋清楚的地方以及講錯的地方,請及時提出,謝謝!