美文网首页
singleLine0的GreenDao学习

singleLine0的GreenDao学习

作者: singleLine0 | 来源:发表于2018-11-28 11:34 被阅读0次

    参考:https://juejin.im/post/5959b5bcf265da6c4d1bb245

    1. 配置

    • build.gradle(Project)
    buildscript {
        repositories {
            ......
            mavenCentral()     // add repository,因为无法下载添加仓库
            google()
        }
        dependencies {
           ......
           classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'   //添加插件
        }
    }
    
    allprojects {
        repositories {
            ......
            maven {
                //url "https://maven.google.com"    //遇到无法下载的情况
                url "http://repo1.maven.org/maven2"
            }
        }
    }
    
    • build.gradle(Module)
    ......
    apply plugin: 'org.greenrobot.greendao'
    
    android {......}
    
    dependencies {
        ......
        implementation 'org.greenrobot:greendao:3.2.2'   //添加依赖
    }
    
    greendao {
        schemaVersion 1 //数据库版本号
        daoPackage 'com.example.xjyfa.test1129.db'// 设置DaoMaster、DaoSession、Dao 包名
        targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录,就是包的目录
    }
    
    • sync同步

    2. 初始化

    • 使用注解,新建Entity类,然后make,会自动生成目录,注:db目录都是自动生成的
    package com.example.xjyfa.test1129;
    
    import org.greenrobot.greendao.annotation.Entity;
    import org.greenrobot.greendao.annotation.Id;
    import org.greenrobot.greendao.annotation.Generated;
    
    @Entity
    public class Book {
        @Id
        private String name;    //author,page省略
        public String getName() {return name;}
        public void setName(String name) {this.name = name;}
        
        //以下为make后,自动生成!!!
        @Generated(hash = 1742999289)
        public Book(String name, String author, int page) {
            this.name = name;
            this.author = author;
            this.page = page;
        }
        @Generated(hash = 1839243756)
            public Book() {
            }
    
    image.png
    • 数据库初始化,得到DaoSession,通常写在Application的onCreate()中
    • 有个小坑,要在清单文件配置application:name 属性
    ......
    public class MyApp extends Application {
        private DaoSession daoSession;
    
        @Override
        public void onCreate() {
            super.onCreate();
            initGreenDao();
        }
    
        //初始化GreenDao
        private void initGreenDao() {
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"test.db");
            Database database = helper.getWritableDb();
            DaoMaster daoMaster = new DaoMaster(database);
            daoSession = daoMaster.newSession();
        }
    
        //获取daoSession
        public DaoSession getDaoSession() {
            return daoSession;
        }
    }
    

    3. 基本使用

    • 获取entityDao表
            //获取entityDao
            MyApp myApp = (MyApp) getApplication();
            DaoSession daoSession= myApp.getDaoSession();
            BookDao bookDao = daoSession.getBookDao();
    
    • 增删查改操作
            //新建一个book实例
            Book book = new Book();
            book.setName("go");
            book.setAuthor("zard");
            book.setPage(486);
            //部分操作
            bookDao.insert(book);   //插入book
            bookDao.insertOrReplace(book);  //插入或者替换book,???怎么确定重复
            bookDao.delete(book);   //删除
            bookDao.deleteByKey("go");  //???
            bookDao.update(book);   //更新
            bookDao.loadAll();      //返回list<Book>
            bookDao.loadByRowId(1); //???
            //两种方式,查询页码大于300的书
            bookDao.queryRaw("where page > ?","300");    // SQL语句
            QueryBuilder builder = bookDao.queryBuilder();  //greedDao的api
            builder.where(BookDao.Properties.Page.gt(300)).build().list();
    

    4. 注解

    5 实际应用

    相关文章

      网友评论

          本文标题:singleLine0的GreenDao学习

          本文链接:https://www.haomeiwen.com/subject/ezcaqqtx.html