GreenDao的简单使用

1.基本配置

// 在根build.gradle文件:
buildscript {
    repositories {
        jcenter()
        mavenCentral() // 添加存储库
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // 添加插件
    }
}
 
// 在你的APPbuild.gradle项目文件:
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // 应用插件
 
dependencies {
    compile 'org.greenrobot:greendao:3.2.2' // 添加依赖
}
//自定义gen路径
greendao {
    schemaVersion 1
    daoPackage '自己包名.gen'
    targetGenDir 'src/main/java'
}

属性介绍:
schemaVersion--> 指定数据库schema版本号,迁移等操作会用到;
daoPackage --> dao的包名,包名默认是entity所在的包;
targetGenDir --> 生成数据库文件的目录;


2.创建一个User的实体类

@Entity
public class User {
    @Id 
    private Long id; 
    private String name; 
   
}
Build  ---> Make Modle  编译项目生产gen目录和set,get方法

3.工具类 DButils 

获取要操作的对象
import android.content.Context;

import com.example.xx.rikao_04.gen.DaoMaster;
import com.example.xx.rikao_04.gen.UserDao;


public class DButils {
    private static volatile DButils instance;
    private final UserDao dao;

    private DButils(Context context){
        //初始化数据库的一些配置
       /* DaoMaster.DevOpenHelper mHelper = new DaoMaster.DevOpenHelper(this, "user", null);
        //获取数据库操作对象
        SQLiteDatabase db = mHelper.getWritableDatabase();
        // 获取DaoMaster对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取DaoSession对象
        DaoSession daoSession = daoMaster.newSession();
        //拿到要操作的对象
        dao = daoSession.getPersonDao();*/
        dao = new DaoMaster(new DaoMaster.DevOpenHelper(context,"username",null).getWritableDatabase()).newSession().getUserDao();
    }


    //单例
    public static DButils getInstance(Context context){
        if(instance==null){
            synchronized (DButils.class){
                if(instance==null){
                    instance=new DButils(context);
                }
            }
        }
        return instance;
    }

    public UserDao getDao(){
        if(dao!=null){
            return dao;
        }
        return null;
    }

}

4.使用增删改查

 //在Activity中获取操作对象
UserDao dao = DButils.getInstance(this).getDao();

  4.1增
mUser = new User((long)2,"anye3");
dao.insert(mUser);//添加一个

dao.insertOrReplace(mUser) //如果数据库中没有则添加,有则替换

4.2删
dao.deleteByKey(id); //根据id删除
dao.delete(mUser);   //根据对象删除
4.3改
mUser = new User((long)2,"anye0803");
dao.update(mUser);

4.4查
List<User> users = dao.loadAll();  //查询所有
String userName = "";
for (int i = 0; i < users.size(); i++) {
    userName += users.get(i).getName()+",";
}
mContext.setText("查询全部数据==>"+userName);

List<User> lisi = dao.queryRaw("where _id=? and name=?", "1", "lisi"); //根据条件查询
                for(User i:lisi){
                    Log.e(TAG, i.toString()+"");
                }







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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值