Android 数据库框架 —— GreenDao

1. 配置Gradle

添加GreenDao插件支持,要添加的内容后面都已经加上了简单注释

buildscript {
    repositories {
        jcenter()
        mavenCentral() // 添加远程仓库地址
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.2'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // 添加插件 更好支持GreenDao
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

之后点击右上角Sync Now ,AS会自动下载插件,等待就好~!如下图所示:

这里写图片描述


2.配置项目下的build.gradle

  • 添加应用插件依赖;

apply plugin: 'org.greenrobot.greendao' // 添加应用依赖插件

  • 添加库文件

compile 'org.greenrobot:greendao:3.2.2' // 添加库

  • 初始化GreenDao配置

按照官方的说法,我们无需任何其他配置,但是,数据库版本这个你得考虑下吧,当然,如果也不想考虑,那就使用默认的(默认版本为1)。

// 配置GreenDao基本参数
greendao {

        schemaVersion 1 //当前数据库版本

}


3.编写GreenDao所需要的实体类

package cn.hlq.greendaostudy.entity;

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

/**
 * Created by HLQ on 2017/6/5
 */
@Entity
public class Student {

    @Id(autoincrement = true) // id自增长
    private Long stuId; // 学院id

    @Index(unique = true) // 唯一性
    private String stuNo; // 学员编号

    private String stuName; // 学员姓名

    private String stuSex; // 学员性别

    private String stuScore; // 学员成绩

}

编译项目,生成Dao相关文件~

这里写图片描述

编辑之后,我们会发现我们的实体类多了一些东西,这里贴出来大家看下:

package cn.hlq.greendaostudy.entity;

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

/**
 * Created by HLQ on 2017/6/5
 */
@Entity
public class Student {

    @Id(autoincrement = true) // id自增长
    private Long stuId; // 学院id

    @Index(unique = true) // 唯一性
    private String stuNo; // 学员编号

    private String stuName; // 学员姓名

    private String stuSex; // 学员性别

    private String stuScore; // 学员成绩

    @Generated(hash = 315497705)
    public Student(Long stuId, String stuNo, String stuName, String stuSex,
            String stuScore) {
        this.stuId = stuId;
        this.stuNo = stuNo;
        this.stuName = stuName;
        this.stuSex = stuSex;
        this.stuScore = stuScore;
    }

    @Generated(hash = 1556870573)
    public Student() {
    }

    public Long getStuId() {
        return this.stuId;
    }

    public void setStuId(Long stuId) {
        this.stuId = stuId;
    }

    public String getStuNo() {
        return this.stuNo;
    }

    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }

    public String getStuName() {
        return this.stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getStuSex() {
        return this.stuSex;
    }

    public void setStuSex(String stuSex) {
        this.stuSex = stuSex;
    }

    public String getStuScore() {
        return this.stuScore;
    }

    public void setStuScore(String stuScore) {
        this.stuScore = stuScore;
    }

}

获取StudentDao

也就是初始化Dao,如下:

    /**
     * 获取StudentDao
     */
    private void getStuDao() {
        // 创建数据
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(self, "hlq.db", null);
        daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        daoSession = daoMaster.newSession();
        stuDao = daoSession.getStudentDao();
    }

新增一条数据

        // 新增一条数据
        findViewById(R.id.id_insert).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student stu = new Student(null, "001", "贺da宝", "男孩", "50");
                long end = stuDao.insert(stu);
                String msg = "";
                if (end > 0) {
                    msg = "001新增成功~";
                } else {
                    msg = "新增失败~";
                }
                stuDao.insert(new Student(null, "002", "贺er宝", "男人", "66"));
                stuDao.insert(new Student(null, "003", "贺san宝", "爷儿们", "23"));
                stuDao.insert(new Student(null, "004", "贺si宝", "男人", "65"));
                Toast.makeText(self, "001 002 003 004新增成功~", Toast.LENGTH_SHORT).show();
            }
        });

新增List集合数据

        // 新增List集合数据
        findViewById(R.id.id_insert_list).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Student> stuList = new ArrayList<Student>();
                stuList.add(new Student(null, "005", "贺利权", "小爷儿们", "43"));
                stuList.add(new Student(null, "006", "贺利权", "大爷儿们", "35"));
                stuList.add(new Student(null, "007", "贺利权", "老爷儿们", "99"));
                stuList.add(new Student(null, "008", "贺利权", "老少爷儿们", "88"));
                stuDao.insertInTx(stuList);
                Toast.makeText(self, "新增成功~", Toast.LENGTH_SHORT).show();
            }
        });

查询所有

        findViewById(R.id.id_search_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Student> stuList = stuDao.queryBuilder().list();
                if (stuList != null) {
                    String searchAllInfo = "";
                    for (int i = 0; i < stuList.size(); i++) {
                        Student stu = stuList.get(i);
                        searchAllInfo += "id:" + stu.getStuId() + "编号:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性别:" + stu.getStuSex() + "成绩:" + stu.getStuScore() + "\n";
                    }
                    TextView tvSearchInfo = (TextView) findViewById(R.id.id_search_all_info);
                    tvSearchInfo.setText(searchAllInfo);
                }
            }
        });

查询指定数据 查询姓名为"贺da宝"的信息

        // 查询指定数据 查询姓名为"贺da宝"的信息
        findViewById(R.id.id_search_assign).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchAssignInfo = "";
                List<Student> stuList = stuDao.queryBuilder().where(StudentDao.Properties.StuName.eq("贺da宝")).list();
                for (int i = 0; i < stuList.size(); i++) {
                    Student stu = stuList.get(i);
                    searchAssignInfo += "id:" + stu.getStuId() + "编号:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性别:" + stu.getStuSex() + "成绩:" + stu.getStuScore() + "\n";
                }
                TextView tvSearchAssign = (TextView) findViewById(R.id.id_search_assign_info);
                tvSearchAssign.setText(searchAssignInfo);
            }
        });

查询指定数据 查询姓名为"贺da宝"的信息并按照成绩排序-降序

        // 查询指定数据 查询姓名为"贺da宝"的信息并按照成绩排序-降序
        findViewById(R.id.id_search_assign_order_desc).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchAssignOrderDesc = "";
                List<Student> stuList = stuDao.queryBuilder().where(StudentDao.Properties.StuName.eq("贺利权")).orderDesc(StudentDao.Properties.StuScore).list();
                for (int i = 0; i < stuList.size(); i++) {
                    Student stu = stuList.get(i);
                    searchAssignOrderDesc += "id:" + stu.getStuId() + "编号:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性别:" + stu.getStuSex() + "成绩:" + stu.getStuScore();
                }
                TextView tvSearchOrderDesc = (TextView) findViewById(R.id.id_search_assign_order_desc_info);
                tvSearchOrderDesc.setText(searchAssignOrderDesc);
            }
        });

查询指定数据 查询姓名为"贺da宝"的信息并按照成绩排序-升序

        // 查询指定数据 查询姓名为"贺da宝"的信息并按照成绩排序-升序
        findViewById(R.id.id_search_assign_order_asc).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchAssignOrderDesc = "";
                List<Student> stuList = stuDao.queryBuilder().where(StudentDao.Properties.StuName.eq("贺利权")).orderAsc(StudentDao.Properties.StuScore).list();
                for (int i = 0; i < stuList.size(); i++) {
                    Student stu = stuList.get(i);
                    searchAssignOrderDesc += "id:" + stu.getStuId() + "编号:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性别:" + stu.getStuSex() + "成绩:" + stu.getStuScore();
                }
                TextView tvSearchOrderDesc = (TextView) findViewById(R.id.id_search_assign_order_asc_info);
                tvSearchOrderDesc.setText(searchAssignOrderDesc);
            }
        });

组合查询数据 查询姓名为"贺利权" 并且成绩小于等于60

        // 组合查询数据 查询姓名为"贺利权" 并且成绩小于等于60
        findViewById(R.id.id_search_combination).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchAssignOrderDesc = "";
                QueryBuilder<Student> stuQB = stuDao.queryBuilder();
                stuQB.where(StudentDao.Properties.StuName.eq("贺利权"), StudentDao.Properties.StuScore.le("60"));
                List<Student> stuList = stuQB.list();
                for (int i = 0; i < stuList.size(); i++) {
                    Student stu = stuList.get(i);
                    searchAssignOrderDesc += "id:" + stu.getStuId() + "编号:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性别:" + stu.getStuSex() + "成绩:" + stu.getStuScore() + "\n";
                }
                TextView tvSearchOrderDesc = (TextView) findViewById(R.id.id_search_combination_info);
                tvSearchOrderDesc.setText(searchAssignOrderDesc);
            }
        });

查询所有返回数据 但只返回前三条数据

        // 查询所有返回数据 但只返回前三条数据
        findViewById(R.id.id_search_limit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Student> stuList = stuDao.queryBuilder().limit(3).list();
                if (stuList != null) {
                    String searchAllInfo = "";
                    for (int i = 0; i < stuList.size(); i++) {
                        Student stu = stuList.get(i);
                        searchAllInfo += "id:" + stu.getStuId() + "编号:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性别:" + stu.getStuSex() + "成绩:" + stu.getStuScore() + "\n";
                    }
                    TextView tvSearchInfo = (TextView) findViewById(R.id.id_search_limit_info);
                    tvSearchInfo.setText(searchAllInfo);
                }
            }
        });

查询所有返回数据 但只返回前三条数据 并且跳过第一条数据

        // 查询所有返回数据 但只返回前三条数据 并且跳过第一条数据
        findViewById(R.id.id_search_limit_offset).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Student> stuList = stuDao.queryBuilder().limit(3).offset(1).list();
                if (stuList != null) {
                    String searchAllInfo = "";
                    for (int i = 0; i < stuList.size(); i++) {
                        Student stu = stuList.get(i);
                        searchAllInfo += "id:" + stu.getStuId() + "编号:" + stu.getStuNo() + "姓名:" + stu.getStuName() + "性别:" + stu.getStuSex() + "成绩:" + stu.getStuScore() + "\n";
                    }
                    TextView tvSearchInfo = (TextView) findViewById(R.id.id_search_limit_offset_info);
                    tvSearchInfo.setText(searchAllInfo);
                }
            }
        });

查询所有信息总条数

        // 查询所有信息总条数
        findViewById(R.id.id_search_count).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int stuSumCount = stuDao.queryBuilder().list().size();
                TextView tvSearchInfo = (TextView) findViewById(R.id.id_search_count_info);
                tvSearchInfo.setText(stuSumCount + "");
            }
        });

删除指定信息

        // 删除指定信息
        findViewById(R.id.id_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stuDao.queryBuilder().where(StudentDao.Properties.StuName.eq("贺si宝")).buildDelete().executeDeleteWithoutDetachingEntities();
                Toast.makeText(self, "删除成功~", Toast.LENGTH_SHORT).show();
//                stuDao.delete(new Student()); // 删除指定对象
//                stuDao.deleteAll(); // 删除所有
            }
        });

更新指定信息

        // 更新指定信息
        findViewById(R.id.id_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student student = stuDao.queryBuilder().where(StudentDao.Properties.StuName.eq("贺da宝")).build().unique();
                if (student != null) {
                    student.setStuName("I Love You");
                    stuDao.update(student);
                }
                Toast.makeText(self, "更新成功~", Toast.LENGTH_SHORT).show();
            }
        });
转自:  静心Study http://www.apkbus.com/blog-904057-67980.html

猜你喜欢

转载自blog.csdn.net/qq_40116418/article/details/80161897