greenDao使用

 
 

greenDao的使用

本文是greenDao入门教程。
greenDao的高级使用:
http://blog.csdn.net/myrssq/article/details/78425780
http://www.jianshu.com/p/dbec25bd575f
http://www.jianshu.com/p/513fb2ba5485

1.gradle中引入

compile 'org.greenrobot:greendao:3.2.0'

2 gradle配置

//数据库相关配置

apply plugin: 'org.greenrobot.greendao'

greendao {

    schemaVersion 1  //版本号

    daoPackage 'com.zjrb.sjzsw.greendao'  //自动生成文件的目录


    targetGenDir 'src/main/java'  //生成数据库文件的目录,默认根目录

}

3 在工程gradle中配置

buildscript { 

repositories { 

    mavenCentral()    
}    
 dependencies {

classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'    
}

}

4 创建实体类

@Entity
public class User {
    @Id 
    private Long id; 
    private String name; 
    private int tempUsageCount; // not persisted  

}

5 makeProject

编译项目,User实体类会自动编译,生成get、set方法并且会在com.zjrb.sjzsw.greendao目录下生成三个数据库文件;

注意:实体类中要添加@Entity标签,不然编译的时候不会生成数据库文件,要设置主键 @Id,用于数据库查询、删除等操作

greenDao的使用

application中进行数据库的初始化

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 getAppContext(){
         return instances;
     }

    /**
     * 设置greenDao
     */
    private void setDatabase() {
        // 通过 DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的 SQLiteOpenHelper 对象。
        // 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为 greenDAO 已经帮你做了。
        // 注意:默认的 DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。
        // 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。
        mHelper = new DaoMaster.DevOpenHelper(this, "notes-db", null);
        db = mHelper.getWritableDatabase();
        // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。 
        mDaoMaster = new DaoMaster(db); 
        mDaoSession = mDaoMaster.newSession();
    }
    public DaoSession getDaoSession() {
          return mDaoSession;
    }
    public SQLiteDatabase getDb() {
          return db;
      }
}

获取newsChannelDao

newsChannelDao = MyApplication.getAppContext().getDaoSession().getNewsChannelDao();

查询

newsChannelDao.loadAll()

增加

newsChannelDao.insert(new User())

删除

newsChannelDao.delete(user)
newsChannelDao.deleteById(id)

更新

newsChannelDao.undate(user)

greenDao中的注解

(一) @Entity 定义实体
@nameInDb 在数据库中的名字,如不写则为实体中类名
@indexes 索引
@createInDb 是否创建表,默认为true,false时不创建
@schema 指定架构名称为实体
@active 无论是更新生成都刷新
(二) @Id
(三) @NotNull 不为null
(四) @Unique 唯一约束
(五) @ToMany 一对多
(六) @OrderBy 排序
(七) @ToOne 一对一
(八) @Transient 不存储在数据库中
(九) @generated 由greendao产生的构造函数或方法		

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值