Greendao的简单使用和封装

依赖:

主app中:

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.1.0'
        
    }
}
自己的项目中:
apply plugin:'org.greenrobot.greendao'//加在开头第二行
//加在android的大括号里面
greendao{
    schemaVersion 1
    daoPackage'bwei.com.myapplication.gen'
    targetGenDir'src/main/java'
}
compile 'org.greenrobot:greendao:3.1.0'
compile 'org.greenrobot:greendao-generator:3.1.0'

实体类创建数据库

public class DBUtils {
    private static volatile DBUtils instance;
    private final CategoryDao dao;
    private DBUtils(Context context) {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "category.db", null);
        SQLiteDatabase database = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(database);
        DaoSession daoSession = daoMaster.newSession();
        dao = daoSession.getCategoryDao();
    }
//单例模式
 public static DBUtils getInstance(Context context){
        if (instance == null) {
            synchronized (DBUtils.class) {
                if (null == instance) {
                    instance = new DBUtils(context);
                }
            }
        }
        return instance;
    }
    public CategoryDao getDao(){
        return dao;
    }

}

Bean类的创建:

@Entity
public class Category {//@Id与@Entity必写,然后在Build里面Make进行编译生成相应的表结构
    @Id
    private Long cid;
    private String createtime;
    private String   name;
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCreatetime() {
        return this.createtime;
    }
    public void setCreatetime(String createtime) {
        this.createtime = createtime;
    }
    public Long getCid() {
        return this.cid;
    }
    public void setCid(Long cid) {
        this.cid = cid;
    }
    @Generated(hash = 115230139)
    public Category(Long cid, String createtime, String name) {
        this.cid = cid;
        this.createtime = createtime;
        this.name = name;
    }
    @Generated(hash = 1150634039)
    public Category() {
    }
}
@Id private Long cid; private String createtime; private String name; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getCreatetime() { return this.createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public Long getCid() { return this.cid; } public void setCid(Long cid) { this.cid = cid; } @Generated(hash = 115230139) public Category(Long cid, String createtime, String name) { this.cid = cid; this.createtime = createtime; this.name = name; } @Generated(hash = 1150634039) public Category() { }}

创建一个Applocation

public class BaseApplication extends Application {
    private static BaseApplication instance;
    private List<Category> list;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }
    public static BaseApplication getInstance() {
        return instance;
    }
    public List<Category> getData() {
        if (list == null) {
            list = new ArrayList<>();
        }
        return list;
    }

    public void setData(List<Category> list) {
        this.list = list;
    }

}
二级缓存

  //获取到数据库
    dao = DBUtils.getInstance(getActivity()).getDao();
    list = getDataFromDB();//查询数据库
    viewAdapter = new RecyclerViewAdapter(getActivity(), list);
    //如果我的数据库没有数据
    if (list == null || list.size() == 0) {
            Toast.makeText(getActivity(), "数据库为空", Toast.LENGTH_SHORT).show();
            //从网络中取数据
            getDataFromNet();
        }
    rlv.setAdapter(viewAdapter);
    return view;
}
//通过网络获取数据
public void getDataFromNet() {
    Retrofit build = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("http://gank.io/api/")
            .build();
    Intent intent = build.create(Intent.class);
    Call<AllBean<List<child_Bean>>> call = intent.call(10, 1);
    call.enqueue(new Callback<AllBean<List<child_Bean>>>() {
        @Override
        public void onResponse(Call<AllBean<List<child_Bean>>> call, Response<AllBean<List<child_Bean>>> response) {
            AllBean<List<child_Bean>> body = response.body();
            List<child_Bean> results = body.getResults();
            for (int i = 0; i < results.size(); i++) {
                Category category = new Category();
                category.setCreatetime(results.get(i).getDesc());
                category.setName(results.get(i).getWho());
                dao.insertOrReplace(category);
            }
            viewAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<AllBean<List<child_Bean>>> call, Throwable t) {

        }
    });
}
//查询数据
    public List<Category> getDataFromDB() {
        list = dao.loadAll();
        return list;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值