Android中GreenDao的使用

参考:Android之greenDao使用(非原创) - 走看看

Android数据库GreenDao的使用完全解析_android greendao-CSDN博客

在这里我只是写写GreenDao数据库的使用.详细的数据库的介绍自己百度.

首先在自己项目底下build.gradle.对应着进行粘贴

//添加字段1
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'
    }
}
//添加字段2(要在程序启动前)
apply plugin: 'org.greenrobot.greendao'
apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"


    defaultConfig {
        applicationId "com.example.qf.zhemaifanli"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.squareup.picasso:picasso:2.3.2'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'eu.the4thfloor.volley:com.android.volley:2015.05.28'
    //添加的字段3(不能重复添加)
    compile 'org.greenrobot:greendao:3.0.1'
    compile project(':pulltorefresh')
    compile 'org.xutils:xutils:3.3.36'
}

这样就会成功的添加依赖.并且剩下的就是对自己的数据库进行操作了.

然后建一个java管理类对自己的数据库进行管理实现数据库的增删改查.

public class DBManager {
    //上下文
    private Context context;
    //数据库表名
    public static final String COLLECT = "collect.db";
    public static final String SHOPCAR = "shopping_car.db";
    public static final String PAY = "pay.db";
    public static DBManager dbManager;

    public DBManager(Context context) {
        //上下文
        this.context = context;
    }

    //单例模式
    public static DBManager getDbmanger(Context context) {
        if (dbManager == null) {
            //加同步锁
            synchronized (DBManager.class) {
                if (dbManager == null) {
                    dbManager = new DBManager(context);
                }
            }
        }
        return dbManager;
    }

    //插入一条数据
    public void insert(SqliteBean collect, String db_name) {
        //1.创建会话对象(开启会话(事务)明确操作者(由他来获取studentDao对象))
        DaoSession daoSession = DaoMaster.newDevSession(context, db_name);
        //获取数据库真正的操作对象(studentDao)(由他来做增删改查操作)
        SqliteBeanDao collectEntityDao = daoSession.getSqliteBeanDao();
        collectEntityDao.insert(collect);
    }


    //插入多条数据
    public void insertAll(List<SqliteBean> list2,String db_name) {
        DaoSession daoSession = DaoMaster.newDevSession(context, db_name);
        SqliteBeanDao collectEntityDao = daoSession.getSqliteBeanDao();
        collectEntityDao.insertInTx(list2);
    }

    /**
     * Properties属性:
     * ge:>=(g:greater:大)
     * le:<=(l:less:小)
     * NotEq:!=(不等于)
     * 条件:asc-递增,desc-递减,order-排序[QueryBuilder中查找]
     * 例如:orderDesc(StudentDao.Properties.Id)
     */

    //查询单个
    public SqliteBean queryOne(int id,String db_name) {
        //1.创建会话对象(开启会话(事务)明确操作者(由他来获取studentDao对象))
        DaoSession daoSession = DaoMaster.newDevSession(context, db_name);
        //2.获取数据库真正的操作对象(studentDao)(由他来做增删改查操作)
        SqliteBeanDao collectEntityDao = daoSession.getSqliteBeanDao();
        //3.where跟条件(特殊点:只需要指出条件:属性名称以及对应的值)
        return collectEntityDao.queryBuilder().orderDesc(SqliteBeanDao.Properties.Id)
                .where(SqliteBeanDao.Properties.Id.eq(id))
                .list().get(0);
    }

    //查询所有
    public List<SqliteBean> queryAll(String db_name) {
        //1.创建会话对象(开启会话(事务)明确操作者(由他来获取studentDao对象))
        DaoSession daoSession = DaoMaster.newDevSession(context, db_name);
        //2.获取数据库真正的操作对象(studentDao)(由他来做增删改查操作)
        SqliteBeanDao collectEntityDao = daoSession.getSqliteBeanDao();
        //3.查询所有
        return collectEntityDao.queryBuilder().list();
    }


    //删除单个
    public void delete(SqliteBean collect,String db_name) {
        DaoSession daoSession = DaoMaster.newDevSession(context, db_name);
        SqliteBeanDao collectEntityDao = daoSession.getSqliteBeanDao();
        //3.删除单个
        collectEntityDao.delete(collect);
    }


    //删除所有
    public void deleteAll(String db_name) {
        DaoSession daoSession = DaoMaster.newDevSession(context, db_name);
        SqliteBeanDao collectEntityDao = daoSession.getSqliteBeanDao();
        //3.删除所有
        collectEntityDao.deleteAll();
    }

    //更新单条
    public void update(SqliteBean collect,String db_name) {
        DaoSession daoSession = DaoMaster.newDevSession(context, db_name);
        SqliteBeanDao collectEntityDao = daoSession.getSqliteBeanDao();
        //3.更新所有
        collectEntityDao.update(collect);
    }

    //更新所有
    public void updateAll(List<SqliteBean> list,String db_name) {
        DaoSession daoSession = DaoMaster.newDevSession(context, db_name);
        SqliteBeanDao collectEntityDao = daoSession.getSqliteBeanDao();
        //3.更新所有
        collectEntityDao.updateInTx(list);
    }

}
 
 
最后一步就是自己在代码中进行调用建立自己想要的数据
 
 
List<SqliteBean> data = new ArrayList<>(); SqliteBean sqliteBean = new SqliteBean(); sqliteBean.setImpageUrl(figure); sqliteBean.setPlace(supplier_name); sqliteBean.setTag(goodDetailBean.getResult().getProduct_info().getSupplier_code()); //数据库的插入操作 DBManager.getDbmanger(this).insertAll(data,DBManager.SHOPCAR);

//数据库的查询操作

 
List<SqliteBean> sqliteBeen = DBManager.getDbmanger(CartActivity.this).queryAll(DBManager.SHOPCAR);

 

附加一下自己的实体类
 
public class SqliteBean { private String tag; private String title; private String type; private String impageUrl; private String place; private String price; private boolean isSend; @Id(autoincrement = true) private long id; @Generated(hash = 763413378) public SqliteBean(String title, String type, String impageUrl, String place, String price, boolean isSend, long id,String tag) { this.title = title; this.type = type; this.impageUrl = impageUrl; this.place = place; this.price = price; this.isSend = isSend; this.id = id; this.tag = tag; } @Generated(hash = 1508872193) public SqliteBean() { } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getImpageUrl() { return impageUrl; } public void setImpageUrl(String impageUrl) { this.impageUrl = impageUrl; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public boolean isSend() { return isSend; } public void setSend(boolean send) { isSend = send; } public long getId() { return id; } public void setId(long id) { this.id = id; } public boolean getIsSend() { return this.isSend; } public void setIsSend(boolean isSend) { this.isSend = isSend; } } 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈德山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值