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;
}