Android中GreenDao的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chendeshan330/article/details/52672071

在这里我只是写写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;
    }
}


展开阅读全文

没有更多推荐了,返回首页