才來公司上班,需求少!時間充裕些,所以尋思把項目使用第三方升級一下!首先是GreenDao
在greendao2.1時代,我們需要在build.gradle中添加以下依賴:
compile 'de.greenrobot:greendao:2.1.0'然后新建module,并在module中build.gradle添加greendao生成器
compile 'de.greenrobot:greendao-generator:2.1.0'然后新建自己的generator生成器類!例如:public class MainDaoGenerator { public static void main(String[] args) throws Exception { Schema schema = new Schema(1, "放置生成文件的包名");// addTeacher(schema); addUser(schema);//用戶表 addFile(schema); addSchedule(schema); addTeacherCourse(schema); addTeacherClassRoom(schema); new DaoGenerator().generateAll(schema, "放置生成文件的位置"); }每次要新建實體類的時候直接添加,最后選中這個類右鍵 選擇run生成相對應的bean 、beanDao!最后我們在新建DBHelper幫助類,取得DaoMaster
public static DaoMaster getDaoMaster(Context context) { if (daoMaster == null) { DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "my_db.db", null); daoMaster = new DaoMaster(helper.getWritableDatabase()); } return daoMaster; }取得Daosession
/** * 取得DaoSession * * @param context * @return */ public static DaoSession getDaoSession(Context context) { if (daoSession == null) { if (daoMaster == null) { daoMaster = getDaoMaster(context); } daoSession = daoMaster.newSession(); } return daoSession; }初始化我們greendao
public static void init(Context context) { mContext = context; instance = new DBHelper(); // 數據庫對象 daoSession = getDaoSession(mContext); }在application中初始化public void onCreate() { super.onCreate(); Logger.init("my"); DBHelper.init(getApplicationContext()); }在這個BDHelper編寫增刪查改方法就OK!升級到3.2!使用的方法就多了一種
首先就是替換module中的
compile 'de.greenrobot:greendao-generator:2.1.0'換成compile 'org.greenrobot:greendao-generator:3.2.0'同步后在類中替換導入greendao的新包名!編譯后運行生成新的注解最后再主工程build.gradle中替換成
compile 'org.greenrobot:greendao:3.2.0'從新替換導入greendao的包名,最后重新編譯PRoject,至此升級可以算是完成了!
但是在greendao3.2中推薦使用以下插件
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'有了我們就可以不使用generator了,使用步揍,按照官方的:
在build.gradle中添加依賴
compile 'org.greenrobot:greendao-generator:3.2.0'然后在android{......
//greendao配置 greendao { schemaVersion 1 //版本號,升級時可配置// daoPackage'com.example.admin.mydaggerdemo.dao' //包名// targetGenDir'src/main/java' //生成目錄 }greendao默認目錄在build-->generated-->source-->greendao中!........
}
在主project的build.gradle中添加
buildscript { repositories { jcenter() } dependencies { ..... 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 }}然后我們編寫我們需要的實體類,并在實體中加入注解@Entity!在隨意寫入某些屬性@Entitypublic class Test { @Id private int id;}直接運行project就OK,就能自動生成beanDao,并補齊bean!這樣加入插件,就可以省去使用genreator了,并且可以學到新的注解姿勢!注解:
@Entity 標識實體類,greenDAO會映射成sqlite的一個表,表名為實體類名的大寫形式
@Id 標識主鍵,該字段的類型為long或Long類型,autoincrement設置是否自動增長
@Property 標識該屬性在表中對應的列名稱, nameInDb設置名稱
@Transient 標識該屬性將不會映射到表中,也就是沒有這列
@NotNull 設置表中當前列的值不可為空
@Convert 指定自定義類型(@linkPropertyConverter)
@Generated greenDAO運行所產生的構造函數或者方法,被此標注的代碼可以變更或者下次運行時清除
@Index 使用@Index作為一個屬性來創建一個索引;定義多列索引(@link Entity#indexes())
@JoinEntity 定義表連接關系
@JoinProperty 定義名稱和引用名稱屬性關系
@Keep 注解的代碼段在GreenDao下次運行時保持不變
1.注解實體類:默認禁止修改此類 2.注解其他代碼段,默認禁止修改注解的代碼段
@OrderBy 指定排序
@ToMany 定義與多個實體對象的關系
@ToOne 定義與另一個實體(一個實體對象)的關系
@Unique 向數據庫列添加了一個唯一的約束
記錄完成整個greendao升級過程,及學習新的使用!由于使用GreenDao畢竟時間不長,肯定有很多不足的地方!歡迎大神們敲磚!
新聞熱點
疑難解答