Android GreenDao 的使用

1.根目录build

// 加入GreenDao数据库的 插件
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'

2.项目

apply plugin: 'org.greenrobot.greendao'//GreenDao 的 插件
 //greendao依赖
    api 'org.greenrobot:greendao:3.2.2' // add library
    api 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.3'
    api 'net.zetetic:android-database-sqlcipher:3.5.1'//GreenDao 加密
@Entity
public class User {
    // 这个greenndao要求的一个 应该是用来设置表id  只能用long
    @Id(autoincrement = true)
    public Long id;
    //用户名
    public String name;
    //手机号
    public String phoneNumber;
    //头像 可能是 url  或者 resid
    public String icon;
    @Generated(hash = 677803454)
    public User(Long id, String name, String phoneNumber, String icon) {
        this.id = id;
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.icon = icon;
    }

}

项目正题build 一下就会自动生成

public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
    public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory);
    }
    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {
        MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {

                    @Override
                    public void onCreateAllTables(Database db, boolean ifNotExists) {
                        DaoMaster.createAllTables(db, ifNotExists);
                    }

                    @Override
                    public void onDropAllTables(Database db, boolean ifExists) {
                        DaoMaster.dropAllTables(db, ifExists);
                    }
                },LocalMusicDao.class);
    }
}
public class GreenDaoUtils_MusicList {
    /**
     * 倒叙
     */
    public static List<LocalMusic> orderDesc() {
        List<LocalMusic> list = BaseApplication.getDaoInstant().getLocalMusicDao().queryBuilder().orderDesc(LocalMusicDao.Properties.Id).list();
        return list;
    }

    /**
     * 正序
     */
    public static List<LocalMusic> orderAsc() {
        List<LocalMusic> list = BaseApplication.getDaoInstant().getLocalMusicDao().queryBuilder().orderAsc(LocalMusicDao.Properties.Id).list();
        return list;
    }

    /**
     * 添加数据,如果有重复则覆盖
     *
     * @param shop
     */
    public static void insertMusic(LocalMusic shop) {
        BaseApplication.getDaoInstant().getLocalMusicDao().insertOrReplace(shop);
    }

    /**
     * 删除数据
     */
    public static void deleteMusic(String url) {
        BaseApplication.getDaoInstant().getLocalMusicDao().delete(queryMusic(url));
    }

    /**
     * 更新数据
     *
     * @param shop
     */
//    public static void updateLove(Shop shop) {
//        BaseApplication.getDaoInstant().getLocalMusicDao().update(shop);
//    }

    /**
     * 查询条件为Type=TYPE_LOVE的数据
     *
     * @return
     */
    public static LocalMusic queryMusic(String url) {

        LocalMusic localMusic = null;
        try {
            localMusic = BaseApplication.getDaoInstant().getLocalMusicDao().queryBuilder().where(LocalMusicDao.Properties.SongUrl.eq(url)).uniqueOrThrow();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return localMusic;
    }

    /**
     * 查询条件为Type=TYPE_LOVE的数据
     *
     * @return
     */
    public static LocalMusic queryMusicInNameAuthor(String musicName, String author) {
        LocalMusic localMusic = BaseApplication.getDaoInstant().getLocalMusicDao().queryBuilder().where(LocalMusicDao.Properties.Name.eq(musicName)).where(LocalMusicDao.Properties.SongAuthor.eq(author)).uniqueOrThrow();
        return localMusic;
    }

    /**
     * 查询全部数据
     */
    public static List<LocalMusic> queryAll() {
        List<LocalMusic> localMusics = null;
        try {
            localMusics = BaseApplication.getDaoInstant().getLocalMusicDao().loadAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return localMusics;
    }

    /**
     * 删除全部数据
     */
    public static void deleteAll() {
        BaseApplication.getDaoInstant().getLocalMusicDao().deleteAll();
    }
}
 /**
     * 配置数据库
     * 由于是注解型的所以使用之前需要编译一下
     */
    private void setupDatabase() {
        //创建数据库music.db"
//        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "music.db", null);
//        //获取可写数据库
//        SQLiteDatabase db = helper.getWritableDatabase();
//        //获取数据库对象
//        DaoMaster daoMaster = new DaoMaster(db);
//        //获取Dao对象管理者
//        daoSession = daoM aster.newSession();
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "music.db",
                null);
        daoSession = new DaoMaster(helper.getEncryptedWritableDb("QWERasdf1234")).newSession();
    }

    public static DaoSession getDaoInstant() {
        return daoSession;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值