GreenDao 優點:
性能高,號稱Android最快的關系型數據庫內存占用小庫文件比較小,小于100K支持數據庫加密 greendao支持SQLCipher進行數據庫加密簡潔易用的APIGreenDao 3.2使用方式 - 第一步:在項目的Build.gradle(PRoject)中添加如下配置
buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' }}第二步:在模塊中的Build.gradle(app)中添加如下配置apply plugin: 'org.greenrobot.greendao'android{ …… greendao{ // 數據庫版本號 schemaVersion 1 // 設置DaoMaster,Daosession,Dao包名 daoPackage 'xxx.xxx.xxx.gen' // 設置DaoMaster,DaoSession,Dao目錄 targetGenDir 'src/main/java' //設置生成單元測試目錄 // targetGenDirTest //設置自動生成單元測試用例 // generateTests } ……}dependencies { compile 'org.greenrobot:greendao:3.2.0'}第四步:寫實體類// 實體注解@Entity( // 如果你有超過一個的數據庫結構,可以通過這個字段來區分該實體屬于哪個結構 schema = "myschema", // 實體活動狀體標志位(默認為false) 若設置為true,實體有更新、刪除和刷新方法 active = true, // 在數據庫中表的名稱,默認為實體的類名 nameInDb = "NOTE", // 定義索引,可以跨越多個列(默認為實體類成員變量的個數) indexes = { @Index(value = "name DESC", unique = true) }, // DAO是否應該創建數據庫表的標志(默認為true) // 如果你有多對一的表,將這個字段設置為false // 或者你已經在GreenDAO之外創建了表,也將其置為false createInDb = false)public class Note { @Id //選定一個long/Long類型的字段作為實體的ID,即數據庫中的主鍵。 private Long id; @NotNull private long createTime; private String content; private String title; private int weatherPosition; private String localtion; @Generated //hash值會自動生成(hash = 346208569) public Note(Long id, long createTime, String content, String title, int weatherPosition, String localtion) { this.id = id; this.createTime = createTime; this.content = content; this.title = title; this.weatherPosition = weatherPosition; this.localtion = localtion; } @Generated public Note() { } //make project后變成會自動生成get/set方法說明: 實體注解 @Entity 實體注解 基礎屬性注解 @Id 選定一個long/Long類型的字段作為實體的ID,即數據庫中的主鍵。 @Generated GreenDao運行所產生的構造函數或者方法,被此標注的代碼可以變更或者下次運行時清除 @Keep 注解的代碼段在GreenDao下次運行時保持不變,注解實體類:默認禁止修改此類,注解其他代碼段,默認禁止修改注解的代碼段。 @Property 讓你自定義字段在數據庫中的名稱,如果為空,GreenDAO將根據駝峰法將其用”_”分割,并全部轉為大寫,如userName 變為 USER_NAME。 @NotNull 使字段在數據庫中成為非空字段,通常都會將基本類型加上NonNull標志。 @Transient 使得字段不再持久化。 索引注解 @Index:使用@Index作為一個屬性來創建一個索引,通過name設置索引別名,也可以通過unique給索引添加約束 @Unique:向數據庫列添加了一個唯一的約束 關系注解 @ToOne:定義與另一個實體(一個實體對象)的關系 @ToMany:定義與多個實體對象的關系
第五步:封裝GreenDao調用方法public class GreenDaoManager { private static GreenDaoManager mInstance; //單例 private DaoMaster mDaoMaster; //以一定的模式管理Dao類的數據庫對象 private DaoSession mDaoSession; //管理制定模式下的所有可用Dao對象 public GreenDaoManager() { if (mInstance == null) { DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(App.getContext(), "myDay-note", null); 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; }}第六步:執行增刪改查// 獲取dao實例對象NoteDao noteDao = GreenDaoManager.getInstance().getSession().getNoteDao();//執行增刪改查操作// 增Note note = new Note(null, creatTime, noteContent, noteTitle, weatherPosition,location);noteDao.insert(note);// 刪noteDao.deleteByKey(noteId);// 改noteDao.update(note);// 查QueryBuilder<Note> queryBuilder = noteDao.queryBuilder().where(NoteDao.Properties.Id.eq(noteId));Note note = queryBuilder.unique();以上即為GreenDao的基本使用方法
新聞熱點
疑難解答