• MVP-3:登陆注册自定义token拦截器


    GreenDao

    1.前言

    关于 Android 中常用的数据存储方式有 Sp 存储 和 文件存储,但是对于数据量比较大,并且结构复杂的数据我们想要存储只能通过数据库进行处理,Android 中提供了一个 SQLite 数据库,但是使用起来比较繁琐和复杂

    2.概述

    greenDAO 是适用于 Android 的轻量级快速 ORM 框架,可将对象映射到 SQLite 数据库中。 并且针对 Android 进行了高度的优化,greenDAO 提供了出色的性能,并占用了最少的内存,优点如下:

    • 性能上(可能是 Android 上最快的 ORM 框架);
    • 易用性上(提供强大并且简洁明了的 API);
    • 轻量(最小的内存消耗与小于 150KB 的库大小)。

    3.ORM 框架概述

    所谓 ORM 框架,即 Object-Relational Mapping,它的作用是在关系型数据库和对象之间作一个映射,这样,我们在具体操作数据库的时候,就不需要再去和复杂的 SQL 语句打交道,只是像平时操作对象一样操作它就可以了。
    在这里插入图片描述

    4.greenDAO 使用

    4.1、准备工作

    (1)project的gradle倒入插件

    // 在 Project的build.gradle 文件中添加:
    buildscript {
        repositories {
            jcenter()
            mavenCentral() // add repository
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.1.2'
            classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    (2) Moudle:app的gradle配置依赖:

    // 在 Moudle:app的  build.gradle 文件中添加:
    apply plugin: 'com.android.application'
    apply plugin: 'org.greenrobot.greendao' // apply plugin
     
    dependencies {
        implementation 'org.greenrobot:greendao:3.2.2' // add library
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    buildTypes下面配置数据库相关信息

    greendao {
        schemaVersion 1 //数据库版本号
        daoPackage 'com.aserbao.aserbaosandroid.functions.database.greenDao.db'
    // 设置DaoMaster、DaoSession、Dao 包名
        targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录,请注意,这里路径用/不要用.
        generateTests false //设置为true以自动生成单元测试。
        targetGenDirTests 'src/main/java' //应存储生成的单元测试的基本目录。默认为 src / androidTest / java。
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4.2 创建实体类

    @Entity
    public class StudentEntity {
        @Id
        private Long id;
        private String name;
        private Integer age;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    其中 @Entity 是 greenDAO 的实体注解(用于标识当前实体需要 GreenDao 生成代码)。

    @Id 是主键 id,Long 类型,可以通过 @Id(autoincrement = true) 设置自动增长(自动增长主键不能用基本类型 long,只能用包装类型 Long)。

    4.3 编译项目:build----》Make project

    在这里插入图片描述
    自动生成代码:
    在这里插入图片描述

    4.4 初始化GreenDao

    public class App extends Application {
        private DaoSession daoSession;
        @Override
        public void onCreate() {
            super.onCreate();
      
            DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "student.db");
            SQLiteDatabase writableDatabase = devOpenHelper.getWritableDatabase();
            DaoMaster daoMaster = new DaoMaster(writableDatabase);
            daoSession = daoMaster.newSession();
        }
    
        public DaoSession getDaoSession() {
            return daoSession;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4.5 具体使用

    
            StudentEntity studentEntity = new StudentEntity(1l, "yaotiaxneu", 12);
            App.getDaoSession().getStudentEntityDao().insert(studentEntity);//增
            App.getDaoSession().getStudentEntityDao().delete(studentEntity);//删
            App.getDaoSession().getStudentEntityDao().update(studentEntity);//改
            List<StudentEntity> studentEntities = App.getDaoSession().getStudentEntityDao().loadAll();//查询所有
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    模板方法模式
    18.1. Fabric2.2 区块链农产品溯源系统 - 多Peer部署(扩展)
    Prometheus启动成功访问页面显示Service Unavailable
    log is判断引发的一系列事件,哭哭
    【Leetcode】1754. Largest Merge Of Two Strings
    计算机网络相关知识点总结(二)
    【Mysql】第3篇--数据库约束
    [读论文]DiT Scalable Diffusion Models with Transformers
    文献阅读-VQAR-基于计算机视觉和自然语言处理的信息检索技术综述
    电商平台搭建流程是怎样的_不懂编程怎么搭建_OctShop
  • 原文地址:https://blog.csdn.net/qq_34178710/article/details/126242388