AndroidStudio 优秀的第三方数据库 GreenDao

来源:互联网 发布:java jar main class 编辑:程序博客网 时间:2024/05/20 07:50

原文: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

    

[java] view plain copy
  1. package com.example.leidong.greendaotest.beans;  
  2.   
  3. import org.greenrobot.greendao.annotation.Entity; 
  4.  
  5. import org.greenrobot.greendao.annotation.Id;  

  6. import org.greenrobot.greendao.annotation.Generated;  
  7.   
  8. /** 
  9.  * Created by leidong on 2017/5/30 
  10.  */  
  11. @Entity  
  12. public class User {  

  13.     @Id(autoincrement = true)  

  14.     private Long id;  

  15.     private String name; 
  16.  
  17.     private int age;  

  18.     public int getAge() {  

  19.         return this.age;  
  20.     }  

  21.     public void setAge(int age) {  

  22.         this.age = age;  
  23.     } 
  24.  
  25.     public String getName() {  

  26.         return this.name;  
  27.     }  
  28.     public void setName(String name) {  

  29.         this.name = name;  
  30.     }  

  31.     public Long getId() {  

  32.         return this.id;  
  33.     }  
  34.     public void setId(Long id) {  

  35.         this.id = id;  
  36.     }  

  37.     @Generated(hash = 1309193360)  

  38.     public User(Long id, String name, int age) {  

  39.         this.id = id;  

  40.         this.name = name;  

  41.         this.age = age;  
  42.     }  

  43.     @Generated(hash = 586692638
  44.  
  45.     public User() {  

  46.     }  
  47.   
  48. }   

    5.编译项目,生成新的文件,结构如下

   
    

二.GreenDao的简单使用

    1.新建一个MyApplication类继承自MyApplication。

    

[java] view plain copy
  1. package com.example.leidong.greendaotest;  
  2.   
  3. import android.app.Application;  

  4. import android.database.sqlite.SQLiteDatabase;  
  5.   
  6. import com.example.leidong.greendaotest.gen.DaoMaster;
  7.   
  8. import com.example.leidong.greendaotest.gen.DaoSession;  
  9.   
  10. /** 
  11.  * Created by leidong on 2017/5/30 
  12.  */  
  13.   
  14. public class MyApplication extends Application { 
  15.  
  16.     private DaoMaster.DevOpenHelper mHelper; 
  17.  
  18.     private SQLiteDatabase db;  

  19.     private DaoMaster mDaoMaster;  

  20.     private DaoSession mDaoSession;  

  21.     public static MyApplication instances;  
  22.   
  23.     @Override  
  24.     public void onCreate() {  

  25.         super.onCreate(); 
  26.  
  27.         instances = this
  28.  
  29.         setDatabase();  
  30.     }  
  31.   
  32.     public static MyApplication getInstances(){  

  33.         return instances;  
  34.     }  
  35.   
  36.     /** 
  37.      * 设置greenDao 
  38.      */  
  39.     private void setDatabase() {  

  40.         mHelper = new DaoMaster.DevOpenHelper(this"leidong.db"null);  

  41.         db = mHelper.getWritableDatabase();  

        // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的
数据库连接。  

  1.         mDaoMaster = new DaoMaster(db);  

  2.         mDaoSession = mDaoMaster.newSession();  
  3.     }  

  4.     public DaoSession getDaoSession() {  

  5.         return mDaoSession;  
  6.     }  

  7.     public SQLiteDatabase getDb() { 
  8.  
  9.         return db;  
  10.     }  
  11. }  

在AndroidManifest.xml的application中写入:

android:name=".MyApplication"


2.MainActivity,Java

[java] view plain copy
  1. package com.example.leidong.greendaotest;  
  2.   
  3. import android.os.Bundle; 
  4.  
  5. import android.support.v7.app.AppCompatActivity; 
  6.  
  7. import android.view.View;  

  8. import android.widget.Button;  

  9. import android.widget.EditText;
  10.   
  11. import android.widget.TextView;  
  12.   
  13. import com.example.leidong.greendaotest.beans.User;  

  14. import com.example.leidong.greendaotest.gen.UserDao;  
  15.   
  16. import java.util.List;  
  17.   
  18. public class MainActivity extends AppCompatActivity implements View

  19. .OnClickListener {  

  20.     private UserDao userDao;  
  21.   
  22.     private EditText editText1, editText2, editText3; 
  23.  
  24.     private TextView textView;  

  25.     private Button button1, button2, button3, button4;  
  26.   
  27.     @Override  

  28.     protected void onCreate(Bundle savedInstanceState) { 
  29.  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.activity_main);  
  32.   
  33.         initWidgets();  

  34.         initActions();  
  35.     }  
  36.   
  37.     /** 
  38.      * 获取组件 
  39.      */  
  40.     private void initWidgets() {  

  41.         editText1 = (EditText) findViewById(R.id.editText1); 
  42.  
  43.         editText2 = (EditText) findViewById(R.id.editText2);  

  44.         editText3 = (EditText) findViewById(R.id.editText3);  
  45.   
  46.         textView = (TextView) findViewById(R.id.textView);  
  47.   
  48.         button1 = (Button) findViewById(R.id.button1); 
  49.  
  50.         button2 = (Button) findViewById(R.id.button2); 
  51.  
  52.         button3 = (Button) findViewById(R.id.button3); 
  53.  
  54.         button4 = (Button) findViewById(R.id.button4);  
  55.     }  
  56.   
  57.     /** 
  58.      * 初始化动作 
  59.      */  
  60.     private void initActions() {  

  61.         button1.setOnClickListener(this); 
  62.  
  63.         button2.setOnClickListener(this);  

  64.         button3.setOnClickListener(this); 
  65.  
  66.         button4.setOnClickListener(this);  
  67.     }  
  68.   
  69.     /** 
  70.      * 按钮点击事件 
  71.      * @param v 视图 
  72.      */  
  73.     @Override  
  74.     public void onClick(View v) {  

  75.         switch (v.getId()){  

  76.             case R.id.button1: 
  77.  
  78.                 addUser();  

  79.                 break;  
  80.             case R.id.button2: 
  81.  
  82.                 deleteUser(); 
  83.  
  84.                 break;  

  85.             case R.id.button3:  

  86.                 modifyUser();  

  87.                 break;  

  88.             case R.id.button4:  

  89.                 queryUser(); 

  90.                 break;  

  91.             default:  
  92.                 break;  
  93.         }  
  94.     }  
  95.   
  96.     /** 
  97.      * 查 
  98.      */  
  99.     private void queryUser() {  

  100.         StringBuilder sb = new StringBuilder(); 
  101.  
  102.         userDao = MyApplication.getInstances().getDaoSession().getUserDao();  

  103.         List<User> userList = userDao.loadAll(); 
  104.  
  105.         for(int i = 0; i < userList.size(); i++){ 
  106.  
  107.             String temp = "";  

  108.             temp += "ID = " + userList.get(i).getId() + " ";  

  109.             temp += "Name = " + userList.get(i).getName() + " "
  110.  
  111.             temp += "Age = " + userList.get(i).getAge() + "\n";  

  112.             sb.append(temp);  
  113.         }  

  114.         textView.setText(sb.toString());  
  115.     }  
  116.   
  117.     /** 
  118.      * 改 
  119.      */  
  120.     private void modifyUser() {  

  121.         long id = Long.parseLong(editText1.getText().toString().trim());  

  122.         String name = editText2.getText().toString().trim(); 
  123.  
  124.         int age = Integer.parseInt(editText3.getText().toString().trim());  

  125.         userDao = MyApplication.getInstances().getDaoSession().getUserDao();  

  126.         User user = new User(); 
  127.  
  128.         user.setId(id);  

  129.         user.setName(name); 
  130.  
  131.         user.setAge(age); 
  132.  
  133.         userDao.update(user);  
  134.     }  
  135.   
  136.     /** 
  137.      * 删 
  138.      */  
  139.     private void deleteUser() {  
  140.         long id = Long.parseLong(editText1.getText().toString().trim());  

  141.         userDao = MyApplication.getInstances().getDaoSession().getUserDao();  

  142.         userDao.deleteByKey(id);  
  143.     }  
  144.   
  145.     /** 
  146.      * 增 
  147.      */  
  148.     private void addUser() {  

  149.         String name = editText2.getText().toString().trim(); 
  150.  
  151.         int age = Integer.parseInt(editText3.getText().toString().trim());  
  152.   

  153.         userDao = MyApplication.getInstances().getDaoSession().getUserDao();  

  154.         User user = new User();  

  155.         user.setName(name); 
  156.  
  157.         user.setAge(age); 
  158.  
  159.         userDao.insert(user);  
  160.     }  
  161. }  

3.activity_main.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <RelativeLayout  

  3.     xmlns:android="http://schemas.android.com/apk/res/android"  

  4.     xmlns:tools="http://schemas.android.com/tools" 
  5.  
  6.     android:layout_width="match_parent"  

  7.     android:layout_height="match_parent"
  8.   
  9.     android:layout_margin="16dp"  

  10.     tools:context="com.example.leidong.greendaotest.MainActivity">  
  11.   
  12.     <android.support.design.widget.TextInputLayout 
  13.  
  14.         android:layout_width="match_parent"  

  15.         android:layout_height="wrap_content" 
  16.  
  17.         android:layout_alignParentTop="true" 
  18.  
  19.         android:layout_alignParentLeft="true"  

  20.         android:layout_alignParentStart="true" 
  21.  
  22.         android:id="@+id/textInputLayout">  
  23.   
  24.         <EditText  
  25.             android:id="@+id/editText1"  

  26.             android:layout_width="match_parent"  

  27.             android:layout_height="wrap_content" 
  28.  
  29.             android:hint="ID" />  
  30.     </android.support.design.widget.TextInputLayout>  
  31.   
  32.     <android.support.design.widget.TextInputLayout  

  33.         android:layout_width="match_parent"  

  34.         android:layout_height="wrap_content"  

  35.         android:layout_below="@+id/textInputLayout" 
  36.  
  37.         android:layout_centerHorizontal="true"  

  38.         android:layout_marginTop="16dp"  

  39.         android:id="@+id/textInputLayout2">  
  40.   
  41.         <EditText  
  42.             android:id="@+id/editText2"  

  43.             android:layout_width="match_parent"  

  44.             android:layout_height="wrap_content"  

  45.             android:hint="Name" />  

  46.     </android.support.design.widget.TextInputLayout>  
  47.   
  48.     <android.support.design.widget.TextInputLayout  

  49.         android:layout_width="match_parent"  

  50.         android:layout_height="wrap_content"  

  51.         android:layout_below="@+id/textInputLayout2"  

  52.         android:layout_centerHorizontal="true"  

  53.         android:layout_marginTop="16dp"  

  54.         android:id="@+id/textInputLayout3">  
  55.   
  56.         <EditText  
  57.             android:id="@+id/editText3" 
  58.  
  59.             android:layout_width="match_parent"  

  60.             android:layout_height="wrap_content" 
  61.  
  62.             android:hint="Age" />  
  63.     </android.support.design.widget.TextInputLayout>  

  64.   
  65.     <TextView  

  66.         android:id="@+id/textView"  

  67.         android:background="@drawable/fillet"  

  68.         android:textSize="16sp"  

  69.         android:textColor="#ffffff"  

  70.         android:padding="16dp"  

  71.         android:layout_width="wrap_content"  

  72.         android:layout_height="wrap_content"  

  73.         android:layout_alignParentEnd="true"  

  74.         android:layout_alignParentLeft="true"  

  75.         android:layout_alignParentRight="true" 
  76.  
  77.         android:layout_alignParentStart="true"  

  78.         android:layout_below="@+id/textInputLayout3" 
  79.  
  80.         android:layout_above="@+id/linearLayout" />  
  81.   
  82.     <LinearLayout  

  83.         android:layout_width="match_parent"  

  84.         android:layout_height="wrap_content"  

  85.         android:layout_alignParentBottom="true" 
  86.  
  87.         android:layout_alignParentLeft="true"  

  88.         android:layout_alignParentStart="true" 
  89.  
  90.         android:orientation="horizontal"  

  91.         android:weightSum="4"  

  92.         android:layout_marginTop="8dp"  

  93.         android:id="@+id/linearLayout">  
  94.         <Button  
  95.             android:id="@+id/button1"  

  96.             android:layout_margin="1dp"  

  97.             android:layout_weight="1"  

  98.             android:layout_width="match_parent"  

  99.             android:layout_height="36dp"  

  100.             android:text="Add"  

  101.             android:textAllCaps="false" 
  102.  
  103.             android:background="@drawable/fillet"/>  
  104.   
  105.         <Button  
  106.             android:id="@+id/button2"  

  107.             android:layout_margin="1dp"  

  108.             android:layout_weight="1"  

  109.             android:layout_width="match_parent"  

  110.             android:layout_height="36dp"  

  111.             android:text="Delete"  

  112.             android:textAllCaps="false"  

  113.             android:background="@drawable/fillet"/>  
  114.   
  115.         <Button  
  116.             android:id="@+id/button3"  

  117.             android:layout_margin="1dp"  

  118.             android:layout_weight="1"  

  119.             android:layout_width="match_parent"  

  120.             android:layout_height="36dp"  

  121.             android:text="Modify"  

  122.             android:textAllCaps="false"  

  123.             android:background="@drawable/fillet"/>  
  124.   
  125.         <Button  
  126.             android:id="@+id/button4"  

  127.             android:layout_margin="1dp"  

  128.             android:layout_weight="1"  

  129.             android:layout_width="match_parent"  

  130.             android:layout_height="36dp"  

  131.             android:text="Query"  

  132.             android:textAllCaps="false"  

  133.             android:background="@drawable/fillet"/>  
  134.     </LinearLayout>  

  135. </RelativeLayout>  

其中需要在build.gradle中引入:

compile 'com.android.support:design:23.4.0'

三.运行结果





工程下载:https://github.com/leidongld/GreenDaoTest