原文:http://blog.csdn.net/ldld1717/article/details/72818917
greenDAO是一款面向Android的輕便快捷的ORM,可將對象映射到SQLite數據庫。 針對android進行了高度優化,greenDAO提供了出色的性能並消耗了最少的內存。
GitHub地址:點擊打開鏈接
GreenDao的優點如下:
應用廣泛:greenDAO自2011年以來一直在使用,被無數着名的應用程序所使用
超簡單:簡潔直觀的API,在V3中帶有註釋
小:小於150K,它只是純Java jar(沒有CPU依賴的本機部分)
快速:可能是由智能代碼生成驅動的Android中最快的ORM
安全和表達性的查詢API:QueryBuilder使用屬性常量來避免打字錯誤
強大的連接:跨實體查詢,甚至連接連接以進行復雜關係
靈活的屬性類型:使用自定義類或枚舉來表示實體中的數據
加密:支持SQLCipher加密數據庫
一.使用準備
1.在build.gradle(Module:app)中加入
compile 'org.greenrobot:greendao:3.0.1'
compile 'org.greenrobot:greendao-generator:3.0.0'
2.在build.gradle(Module:app)中引入插件並自定義路徑
apply plugin: 'org.greenrobot.greendao'
greendao {
schemaVersion 1
daoPackage 'com.example.leidong.greendaotest.gen'
targetGenDir 'src/main/java'
}
3.在build.gradle(Project:XXX)的dependencies中加入
classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'
4.創建一個User的Bean
- package com.example.leidong.greendaotest.beans;
- import org.greenrobot.greendao.annotation.Entity;
- import org.greenrobot.greendao.annotation.Id;
-
- import org.greenrobot.greendao.annotation.Generated;
- /**
- * Created by leidong on 2017/5/30
- */
- @Entity
- public class User {
-
- @Id(autoincrement = true)
-
- private Long id;
-
- private String name;
- private int age;
-
- public int getAge() {
-
- return this.age;
- }
-
- public void setAge(int age) {
-
- this.age = age;
- }
- 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 = 1309193360)
-
- public User(Long id, String name, int age) {
-
- this.id = id;
-
- this.name = name;
-
- this.age = age;
- }
-
- @Generated(hash = 586692638)
- public User() {
-
- }
- }
5.編譯項目,生成新的文件,結構如下
二.GreenDao的簡單使用
1.新建一個MyApplication類繼承自MyApplication。
- package com.example.leidong.greendaotest;
- import android.app.Application;
-
- import android.database.sqlite.SQLiteDatabase;
- import com.example.leidong.greendaotest.gen.DaoMaster;
- import com.example.leidong.greendaotest.gen.DaoSession;
- /**
- * Created by leidong on 2017/5/30
- */
- public class MyApplication extends Application {
- private DaoMaster.DevOpenHelper mHelper;
- private SQLiteDatabase db;
-
- private DaoMaster mDaoMaster;
-
- private DaoSession mDaoSession;
-
- public static MyApplication instances;
- @Override
- public void onCreate() {
-
- super.onCreate();
- instances = this;
- setDatabase();
- }
- public static MyApplication getInstances(){
-
- return instances;
- }
- /**
- * 設置greenDao
- */
- private void setDatabase() {
-
- mHelper = new DaoMaster.DevOpenHelper(this, "leidong.db", null);
-
- db = mHelper.getWritableDatabase();
-
- mDaoMaster = new DaoMaster(db);
-
- mDaoSession = mDaoMaster.newSession();
- }
-
- public DaoSession getDaoSession() {
-
- return mDaoSession;
- }
-
- public SQLiteDatabase getDb() {
- return db;
- }
- }
在AndroidManifest.xml的application中寫入:
android:name=".MyApplication"
2.MainActivity,Java
- package com.example.leidong.greendaotest;
- 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.TextView;
- import com.example.leidong.greendaotest.beans.User;
-
- import com.example.leidong.greendaotest.gen.UserDao;
- import java.util.List;
- public class MainActivity extends AppCompatActivity implements View
-
- .OnClickListener {
-
- private UserDao userDao;
- private EditText editText1, editText2, editText3;
- private TextView textView;
-
- private Button button1, button2, button3, button4;
- @Override
-
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initWidgets();
-
- initActions();
- }
- /**
- * 獲取組件
- */
- private void initWidgets() {
-
- editText1 = (EditText) findViewById(R.id.editText1);
- editText2 = (EditText) findViewById(R.id.editText2);
-
- editText3 = (EditText) findViewById(R.id.editText3);
- textView = (TextView) findViewById(R.id.textView);
- button1 = (Button) findViewById(R.id.button1);
- button2 = (Button) findViewById(R.id.button2);
- button3 = (Button) findViewById(R.id.button3);
- button4 = (Button) findViewById(R.id.button4);
- }
- /**
- * 初始化動作
- */
- private void initActions() {
-
- button1.setOnClickListener(this);
- button2.setOnClickListener(this);
-
- button3.setOnClickListener(this);
- button4.setOnClickListener(this);
- }
- /**
- * 按鈕點擊事件
- * @param v 視圖
- */
- @Override
- public void onClick(View v) {
-
- switch (v.getId()){
-
- case R.id.button1:
- addUser();
-
- break;
- case R.id.button2:
- deleteUser();
- break;
-
- case R.id.button3:
-
- modifyUser();
-
- break;
-
- case R.id.button4:
-
- queryUser();
-
- break;
-
- default:
- break;
- }
- }
- /**
- * 查
- */
- private void queryUser() {
-
- StringBuilder sb = new StringBuilder();
- userDao = MyApplication.getInstances().getDaoSession().getUserDao();
-
- List<User> userList = userDao.loadAll();
- for(int i = 0; i < userList.size(); i++){
- String temp = "";
-
- temp += "ID = " + userList.get(i).getId() + " ";
-
- temp += "Name = " + userList.get(i).getName() + " ";
- temp += "Age = " + userList.get(i).getAge() + "\n";
-
- sb.append(temp);
- }
-
- textView.setText(sb.toString());
- }
- /**
- * 改
- */
- private void modifyUser() {
-
- long id = Long.parseLong(editText1.getText().toString().trim());
-
- String name = editText2.getText().toString().trim();
- int age = Integer.parseInt(editText3.getText().toString().trim());
-
- userDao = MyApplication.getInstances().getDaoSession().getUserDao();
-
- User user = new User();
- user.setId(id);
-
- user.setName(name);
- user.setAge(age);
- userDao.update(user);
- }
- /**
- * 刪
- */
- private void deleteUser() {
- long id = Long.parseLong(editText1.getText().toString().trim());
-
- userDao = MyApplication.getInstances().getDaoSession().getUserDao();
-
- userDao.deleteByKey(id);
- }
- /**
- * 增
- */
- private void addUser() {
-
- String name = editText2.getText().toString().trim();
- int age = Integer.parseInt(editText3.getText().toString().trim());
-
- userDao = MyApplication.getInstances().getDaoSession().getUserDao();
-
- User user = new User();
-
- user.setName(name);
- user.setAge(age);
- userDao.insert(user);
- }
- }
3.activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
-
- <RelativeLayout
-
- 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:layout_margin="16dp"
-
- tools:context="com.example.leidong.greendaotest.MainActivity">
- <android.support.design.widget.TextInputLayout
- android:layout_width="match_parent"
-
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_alignParentLeft="true"
-
- android:layout_alignParentStart="true"
- android:id="@+id/textInputLayout">
- <EditText
- android:id="@+id/editText1"
-
- android:layout_width="match_parent"
-
- android:layout_height="wrap_content"
- android:hint="ID" />
- </android.support.design.widget.TextInputLayout>
- <android.support.design.widget.TextInputLayout
-
- android:layout_width="match_parent"
-
- android:layout_height="wrap_content"
-
- android:layout_below="@+id/textInputLayout"
- android:layout_centerHorizontal="true"
-
- android:layout_marginTop="16dp"
-
- android:id="@+id/textInputLayout2">
- <EditText
- android:id="@+id/editText2"
-
- android:layout_width="match_parent"
-
- android:layout_height="wrap_content"
-
- android:hint="Name" />
-
- </android.support.design.widget.TextInputLayout>
- <android.support.design.widget.TextInputLayout
-
- android:layout_width="match_parent"
-
- android:layout_height="wrap_content"
-
- android:layout_below="@+id/textInputLayout2"
-
- android:layout_centerHorizontal="true"
-
- android:layout_marginTop="16dp"
-
- android:id="@+id/textInputLayout3">
- <EditText
- android:id="@+id/editText3"
- android:layout_width="match_parent"
-
- android:layout_height="wrap_content"
- android:hint="Age" />
- </android.support.design.widget.TextInputLayout>
-
- <TextView
-
- android:id="@+id/textView"
-
- android:background="@drawable/fillet"
-
- android:textSize="16sp"
-
- android:textColor="#ffffff"
-
- android:padding="16dp"
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:layout_alignParentEnd="true"
-
- android:layout_alignParentLeft="true"
-
- android:layout_alignParentRight="true"
- android:layout_alignParentStart="true"
-
- android:layout_below="@+id/textInputLayout3"
- android:layout_above="@+id/linearLayout" />
- <LinearLayout
-
- android:layout_width="match_parent"
-
- android:layout_height="wrap_content"
-
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
-
- android:layout_alignParentStart="true"
- android:orientation="horizontal"
-
- android:weightSum="4"
-
- android:layout_marginTop="8dp"
-
- android:id="@+id/linearLayout">
- <Button
- android:id="@+id/button1"
-
- android:layout_margin="1dp"
-
- android:layout_weight="1"
-
- android:layout_width="match_parent"
-
- android:layout_height="36dp"
-
- android:text="Add"
-
- android:textAllCaps="false"
- android:background="@drawable/fillet"/>
- <Button
- android:id="@+id/button2"
-
- android:layout_margin="1dp"
-
- android:layout_weight="1"
-
- android:layout_width="match_parent"
-
- android:layout_height="36dp"
-
- android:text="Delete"
-
- android:textAllCaps="false"
-
- android:background="@drawable/fillet"/>
- <Button
- android:id="@+id/button3"
-
- android:layout_margin="1dp"
-
- android:layout_weight="1"
-
- android:layout_width="match_parent"
-
- android:layout_height="36dp"
-
- android:text="Modify"
-
- android:textAllCaps="false"
-
- android:background="@drawable/fillet"/>
- <Button
- android:id="@+id/button4"
-
- android:layout_margin="1dp"
-
- android:layout_weight="1"
-
- android:layout_width="match_parent"
-
- android:layout_height="36dp"
-
- android:text="Query"
-
- android:textAllCaps="false"
-
- android:background="@drawable/fillet"/>
- </LinearLayout>
-
- </RelativeLayout>
其中需要在build.gradle中引入:
compile 'com.android.support:design:23.4.0'
三.運行結果
工程下載:https://github.com/leidongld/GreenDaoTest