最近需要開始一個新的項目了,考慮到既然是新項目了助隧,那么一些常用的框架肯定也要用當(dāng)下最火的臂外!這次的新項目中涉及到了本地數(shù)據(jù)存儲,很早前有個項目的本地數(shù)據(jù)庫框架用的是ActiveAndroid喇颁,github找了下這個框架漏健,發(fā)現(xiàn)已經(jīng)兩年多已經(jīng)沒有更新了。然后就想到了一直沒有時間去涉及到的greenDAO橘霎,github一搜索蔫浆,哦呦?star有5000+姐叁,并且依然保持著很高的更新頻率瓦盛,并且性能遠(yuǎn)遠(yuǎn)的高于activeAndroid(見下圖),果斷選用外潜。
剛開始想偷偷懶原环,大致瀏覽了下greenDAO官網(wǎng)后就開始百度一些相關(guān)教程(畢竟看中文的速度快啊4V雎稹!)滔驾。找教程中也看到了很多質(zhì)量很高的文章谒麦,但是看上去都有點怪怪的,感覺和官網(wǎng)的最新版本介紹的完全不是一個東西哆致,主要是在導(dǎo)包以及生成代碼的方式绕德。最后發(fā)現(xiàn)是因為greenDAO2和3的構(gòu)建和生成代碼的方式變化了不少,所以最后還是老老實實的回去看官文了摊阀,廢話了這么多耻蛇,下面開始正題!胞此!
【一】greenDAO3基本介紹
greenDAO3開始使用注解的方式定義實體類(entity)臣咖,并且是通過安裝gradle插件來生成代碼。之前的版本則是通過建立一個獨立的java工程來存放生成的文件豌鹤。
【二】導(dǎo)入相關(guān)的包
compile 'org.greenrobot:greendao:3.0.1'
compile 'org.greenrobot:greendao-generator:3.0.0'
這一步很簡單亡哄,在gradle的dependencies中導(dǎo)入最新版本的依賴就可以了
【三】配置gradle
這一步非常關(guān)鍵,這是整個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的插件灵临,當(dāng)然了截型,這個插件就是為greenDAO服務(wù)的,用來生成數(shù)據(jù)庫相關(guān)的代碼儒溉。
簡單的介紹下通過gradle插件生成數(shù)據(jù)庫代碼的步驟:每次在make project之前宦焦,它會掃描項目中所有的@Entity文件(greenDAO中數(shù)據(jù)庫的實體類),根據(jù)實體類生成DaoSession、DaoMaster以及所有實體類的dao類波闹,生成的文件默認(rèn)目錄為:build/generated/source/greendao酝豪,若不想修改生成的路徑,可以將此路徑設(shè)置為資源目錄精堕。我們也可以自定義這個路徑孵淘,下面就來介紹如何在gradle中配置greenDAO的相關(guān)屬性:
greendao {
schemaVersion 1
daoPackage 'com.wyk.greendaodemo.greendao.gen'
targetGenDir 'src/main/java'
}
在gradle的根模塊中加入上述代碼,就完成了我們的基本配置了歹篓。
schemaVersion---->指定數(shù)據(jù)庫schema版本號瘫证,遷移等操作會用到
daoPackage-------->通過gradle插件生成的數(shù)據(jù)庫相關(guān)文件的包名,默認(rèn)為你的entity所在的包名
targetGenDir-------->這就是我們上面說到的自定義生成數(shù)據(jù)庫文件的目錄了庄撮,可以將生成的文件放到我們的java目錄中背捌,而不是build中,這樣就不用額外的設(shè)置資源目錄了
貼上完整代碼圖:
【四】編寫entity類
通過greenDAO3注解的語法來定義我們的一個測試數(shù)據(jù)庫實體類:
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普通類變?yōu)橐粋€能夠被greenDAO識別的數(shù)據(jù)庫類型的實體類
@Id:通過這個注解標(biāo)記的字段必須是Long類型的洞斯,這個字段在數(shù)據(jù)庫中表示它就是主鍵毡庆,并且它默認(rèn)就是自增的
@Transient:表明這個字段不會被寫入數(shù)據(jù)庫,只是作為一個普通的java類字段巡扇,用來臨時存儲數(shù)據(jù)的扭仁,不會被持久化
接下來讓我們點擊as中Build菜單欄中的Make Project,make完成之后會發(fā)現(xiàn)我們的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() {
}
}
上述生成的代碼中包含了構(gòu)造方法以及每個屬性的getter setter方法。除了這個變化刀闷,還會發(fā)現(xiàn)在我們之前設(shè)置的targetGenDir這個目錄中多了三個文件熊泵,之后所有相關(guān)的數(shù)據(jù)庫操作都依靠這三個文件了。
【五】增刪改查操作
鋪墊了這么多甸昏,終于能見到我們數(shù)據(jù)庫的核心功能了~
1.第一步肯定是初始化數(shù)據(jù)庫
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”是我們自定的數(shù)據(jù)庫名字顽分,應(yīng)為我們之前創(chuàng)建了一個Entity叫做User,所以greenDAO自定幫我們生成的UserDao施蜜,拿到了這個UserDao卒蘸,我們就可以操作User這張表了。
一個DaoMaster就代表著一個數(shù)據(jù)庫的連接翻默;DaoSession可以讓我們使用一些Entity的基本操作和獲取Dao操作類缸沃,DaoSession可以創(chuàng)建多個,每一個都是屬于同一個數(shù)據(jù)庫連接的修械。
2.插入數(shù)據(jù)
User user = new User(null, "wyk");
userDao.insert(user);
非常簡單趾牧,實例化一個User對象,然后調(diào)用userDao的insert方法就可以了肯污。將User對象的id設(shè)置為null的時候翘单,數(shù)據(jù)庫會自動為其分配自增的id吨枉。
3.查找數(shù)據(jù)
List<User> userList = userDao.queryBuilder()
.where(UserDao.Properties.Id.notEq(999))
.orderAsc(UserDao.Properties.Id)
.limit(5)
.build().list();
通過userDao的queryBuilder()方法,生成一個查找構(gòu)造器哄芜,可以給構(gòu)造器添加where條件判斷貌亭、按照某某字段排序以及查詢的條數(shù)等基本的數(shù)據(jù)庫操作。list()方法表示查詢的結(jié)果為一個集合.
4.修改數(shù)據(jù)
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();
}
修改數(shù)據(jù)的第一步是把需要修改的條目給查詢出來忠烛,然后修改該條目属提,最后調(diào)用userDao的update方法即可。unique()表示查詢結(jié)果為一條數(shù)據(jù)美尸,若數(shù)據(jù)不存在,findUser為null斟薇。
5.刪除數(shù)據(jù)
User findUser = userDao.queryBuilder().where(UserDao.Properties.Name.eq("wyk")).build().unique();
if(findUser != null){
userDao.deleteByKey(findUser.getId());
}
刪除數(shù)據(jù)和更新數(shù)據(jù)基本相似师坎,先查詢出需要刪除的條目,然后調(diào)用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等復(fù)雜的語句胯陋,可以調(diào)用android原生的sqlite去進(jìn)行查詢。
7.數(shù)據(jù)庫升級
如果某張表修改了字段袱箱,或者新增了一張表遏乔,必須要修改build.gradle中的schemaVersion,否則當(dāng)你升級app的時候发笔,如果進(jìn)行了數(shù)據(jù)庫操作盟萨,會發(fā)現(xiàn)列不匹配或者表不存在等問題,直接會導(dǎo)致app閃退了讨。但是如果僅僅是將schemaVersion加1捻激,雖然程序不會崩潰,并且數(shù)據(jù)表的結(jié)構(gòu)也會更新成功前计,但是之前表中的數(shù)據(jù)會全部清空胞谭。我們需要進(jìn)行手動操作來進(jìn)行數(shù)據(jù)庫里面的數(shù)據(jù)遷移,大致的思路是:創(chuàng)建臨時表(結(jié)構(gòu)與上一版本的表結(jié)構(gòu)相同)男杈,將舊數(shù)據(jù)移到臨時表中丈屹,刪除舊版本的表,創(chuàng)建新版本的表伶棒,將臨時表中的數(shù)據(jù)轉(zhuǎn)移到新表中旺垒,最后再刪除臨時表。詳細(xì)方法見鏈接:
http://stackoverflow.com/a/30334668/5995409
【結(jié)尾】greenDAO還有很多高級的功能苞冯,比如說給表設(shè)置一對一袖牙、一對多、多對多的關(guān)系以及復(fù)用查詢語句等等等太多功能舅锄,本片文章僅僅做一個入門教學(xué)鞭达,其余的功能小伙伴們可以去官網(wǎng)教程中自行查閱咯~最后貼上整個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);
}
/**
* 根據(jù)名字更新某條數(shù)據(jù)的名字
* @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();
}
}
/**
* 根據(jù)名字刪除某用戶
* @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());
}
}
/**
* 本地數(shù)據(jù)里添加一個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;
}
}
項目結(jié)構(gòu)
文章如果有沒能解釋清楚的地方以及講錯的地方司忱,請及時提出,謝謝畴蹭!
作者:nj物是人非
來源:CSDN
原文:https://blog.csdn.net/njweiyukun/article/details/51893092
版權(quán)聲明:本文為博主原創(chuàng)文章坦仍,轉(zhuǎn)載請附上博文鏈接!