一: 依赖
app//build.gradle :
apply plugin: 'org.greenrobot.greendao'
android{
greendao { schemaVersion 1 daoPackage 'com.zgl.greentest.gen' targetGenDir 'src/main/java' }
}
dependencies{
compile 'org.greenrobot:greendao-generator:3.2.2' compile 'org.greenrobot:greendao:3.2.2'
}
project// build.gradle:
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.3.1' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }
二 在项目包下创建实体类(我是在db包里面写的OnlinePlayListBean)
@Entity@Keep public class OnlinePlayListBean { @Id private long playListId; @Property(nameInDb = "_data") private String localUrl; @Property(nameInDb = "name") private String listName; @Property(nameInDb = "date_added") private long addDate; @Property(nameInDb = "date_modified") private long modifiedDate; public OnlinePlayListBean(long playListId, String listName, String localUrl, long addDate, long modifiedDate) { this.playListId = playListId; this.listName = listName; this.localUrl = localUrl; this.addDate = addDate; this.modifiedDate = modifiedDate; } //primary public long getPlayListId() { return playListId; } public void setPlayListId(long playListId) { this.playListId = playListId; } public String getListName() { return listName; } public void setListName(String listName) { this.listName = listName; } public String getLocalUrl() { return localUrl; } public void setLocalUrl(String localUrl) { this.localUrl = localUrl; } public long getAddDate() { return addDate; } public void setAddDate(long addDate) { this.addDate = addDate; } public long getModifiedDate() { return modifiedDate; } public void setModifiedDate(long modifiedDate) { this.modifiedDate = modifiedDate; } }
GreenDao的初始化:
public class GreenDaoManager { private DaoMaster mDaoMaster; private DaoSession mDaoSession; private static GreenDaoManager mInstance; //单例 private GreenDaoManager(){ if (mInstance == null) { DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(App.getContext(), "user1-db", null);//此处为自己需要处理的表 Log.d("green dao manager???", "GreenDaoManager: app context?"+App.getContext()); mDaoMaster = new DaoMaster(devOpenHelper.getWritableDatabase()); mDaoSession = mDaoMaster.newSession(); } } public static GreenDaoManager getInstance() { if (mInstance == null) { synchronized (GreenDaoManager.class) {//保证异步处理安全操作 if (mInstance == null) { mInstance = new GreenDaoManager(); } } } return mInstance; } public DaoMaster getMaster() { return mDaoMaster; } public DaoSession getSession() { return mDaoSession; } public DaoSession getNewSession() { mDaoSession = mDaoMaster.newSession(); return mDaoSession; } }
在App的配置
public class App extends Application { private static Context mContext; @Override public void onCreate() { super.onCreate(); mContext = getApplicationContext(); //greenDao全局配置,只希望有一个数据库操作对象 GreenDaoManager.getInstance(); // Set up Crashlytics, disabled for debug builds Crashlytics crashlyticsKit = new Crashlytics.Builder() .core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()) .build(); Fabric.with(this, crashlyticsKit); //Set up dynamic shortcuts if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { new DynamicShortcutManager(this).initDynamicShortcuts(); } } public static Context getContext() { return mContext; } }
好了, 我们配置的差不多了 ,现在可以使用了: 可以在一个工具类里面写
//返回数据库操作对象 public static OnlinePlayListBeanDao getOnlinePlayListBeanDao() { return GreenDaoManager.getInstance().getSession().getOnlinePlayListBeanDao(); }
以下是本人在项目和文献中使用过的一些方法 比如:
//搜id对应的信息 public static void getOnlinePlayListBeanById() { OnlinePlayListBean OnlinePlayListBean = getOnlinePlayListBeanDao().load(1l); Log.i("tag", "结果:" + OnlinePlayListBean.getPlayListId() + "," + OnlinePlayListBean.getLocalUrl() + "," + OnlinePlayListBean.getListName() + "," + OnlinePlayListBean.getAddDate() + "," + OnlinePlayListBean.getModifiedDate()); } //way : add a bean directly by watch out the primary key not duplicated public static void insertdata() { //插入数据 long1:id ,long2:add_date , long3:modify_date OnlinePlayListBean insertData = new OnlinePlayListBean(2050, "要插入的表名", "存储位置", 20170428, 20170429); getOnlinePlayListBeanDao().insert(insertData); } //way: add a bean directly identified by the exist primary key public static void updatadata() { //更改数据 int updatePostion = 0; ListOnlinePlayListBeanss = getOnlinePlayListBeanDao().loadAll(); OnlinePlayListBean OnlinePlayListBean = new OnlinePlayListBean(OnlinePlayListBeanss.get(updatePostion).getPlayListId(), "更改后的数据用户", "存储位置", 20170428, 20170429); getOnlinePlayListBeanDao().update(OnlinePlayListBean); } //query all by for loop public static List querydata() { //查询数据详细 List onlinePlayListBeans = getOnlinePlayListBeanDao().loadAll(); Log.i("tag", "当前数量:" + onlinePlayListBeans.size()); for (int i = 0; i < onlinePlayListBeans.size(); i++) { Log.i("tag", "结果:" + onlinePlayListBeans.get(i).getPlayListId() + "," + onlinePlayListBeans.get(i).getListName() + "," + onlinePlayListBeans.get(i).getLocalUrl() + "," + onlinePlayListBeans.get(i).getAddDate() + "," + onlinePlayListBeans.get(i).getModifiedDate() + ";"); } return onlinePlayListBeans; } //以listname来查询 public static void querydataBy() {////查询条件 Query nQuery = getOnlinePlayListBeanDao().queryBuilder() // .where(OnlinePlayListBeanDao.Properties.Name.eq("OnlinePlayListBean1"))//.where(OnlinePlayListBeanDao.Properties.Id.notEq(999)) .orderAsc(OnlinePlayListBeanDao.Properties.ListName)//.limit(5)//orderDesc .build(); List OnlinePlayListBeans = nQuery.list(); Log.i("tag", "当前数量:" + OnlinePlayListBeans.size()); for (int i = 0; i < OnlinePlayListBeans.size(); i++) { Log.i("tag", "结果:" + OnlinePlayListBeans.get(i).getPlayListId() + "," + OnlinePlayListBeans.get(i).getListName() + "," + OnlinePlayListBeans.get(i).getLocalUrl() + "," + OnlinePlayListBeans.get(i).getModifiedDate() + "," + OnlinePlayListBeans.get(i).getAddDate() + ";"); } // QueryBuilder qb = OnlinePlayListBeanDao.queryBuilder(); // qb.where(Properties.FirstName.eq("Joe"), // qb.or(Properties.YearOfBirth.gt(1970), // qb.and(Properties.YearOfBirth.eq(1970), Properties.MonthOfBirth.ge(10)))); // List youngJoes = qb.list(); } /** * 根据查询条件,返回数据列表 * * @param where 条件 * @param params 参数 * @return 数据列表 */ public static List queryN(String where, String... params) { return getOnlinePlayListBeanDao().queryRaw(where, params); } /** * 根据用户信息,插件或修改信息 * * @param OnlinePlayListBean 用户信息 * @return 插件或修改的用户id */ public static long saveN(OnlinePlayListBean OnlinePlayListBean) { return getOnlinePlayListBeanDao().insertOrReplace(OnlinePlayListBean); } /** * 批量插入或修改用户信息 * * @param list 用户信息列表 根据id 修正表 */ public static void saveNLists(final List list) { if (list == null || list.isEmpty()) { return; } getOnlinePlayListBeanDao().getSession().runInTx(new Runnable() { @Override public void run() { for (int i = 0; i < list.size(); i++) { OnlinePlayListBean OnlinePlayListBean = list.get(i); getOnlinePlayListBeanDao().insertOrReplace(OnlinePlayListBean); } } }); } /** * 删除所有数据 */ public static void deleteAllNote() { getOnlinePlayListBeanDao().deleteAll(); } /** * 根据用户类,删除信息 * * 删除传入的实际参数bean对象 * * @param OnlinePlayListBean 用户信息类 */ public static void deleteNote(OnlinePlayListBean OnlinePlayListBean) { getOnlinePlayListBeanDao().delete(OnlinePlayListBean); }
按id是否为某个数字时候查询: unique() 返回一个或者0个对象 , uniqueOrThrow 返回一个对象否则抛出异常
list() 返回多个对象
OnlinePlayListBean defalutOnlinePlayListBean = OnlinePlayListBeanDaoUtils.getOnlinePlayListBeanDao().queryBuilder() .where(OnlinePlayListBeanDao.Properties.PlayListId.eq(446623648)).unique();
添加数据 以Bean的形式
OnlinePlayListBean insertData = new OnlinePlayListBean(playlistId, charSequence.toString(), "pco", playlistId, playlistId); OnlinePlayListBeanDaoUtils.getOnlinePlayListBeanDao().insert(insertData);
GreenDao 常见错误 解析:
1. Caused by: java.lang.ClassNotFoundException: Didn't find class xxx on path: DexPathList
相关类没有混淆: 混淆时候 把相关包 按以下格式 其实class genre 是父级包
-keep public class genre.**{*;}
2.
Error:Execution failed
for
task
':platform:greendao'
.
> Can't replace field in /Users/dsx/Documents/XXX/platform/src/main/java/com/XXX/platform/bean/OrderXXX.java:
43
with generated version.
//董铂然 博客园
If you would like to keep it, it should be explicitly marked with
@Keep
annotation.
Otherwise please mark it with
@Generated
annotation