Android GreenDAO3.0——entity建模

来源:http://www.cnblogs.com/rootself/archive/2017/05/15/6858992.html
-Advertisement-
Play Games

引言 在項目中,為了使用GreenDAO的自動生成DAO class的功能,我們必須建立entity,該entity通過java註解標識。 Schema是資料庫對象集合,我們可以通過gradle插件配置GreenDAO,除此之外,我們至少需要配置Schema的版本: 我們不僅可以配置schemaVe ...


引言

在項目中,為了使用GreenDAO的自動生成DAO class的功能,我們必須建立entity,該entity通過java註解標識。

                                                           

 

Schema是資料庫對象集合,我們可以通過gradle插件配置GreenDAO,除此之外,我們至少需要配置Schema的版本

// In the build.gradle file of your app project:
android {
...
}

greendao {
    schemaVersion 2
}

我們不僅可以配置schemaVersion,還可以配置其他的屬性:

daoPackageDAOs、DAOMaster和DAOSession生成的目錄,預設是entity所在的目錄

targetGenDir:預設在build/generated/source/greendao

其他......(貌似不重要)

@Entity

在Android項目中,GreenDAO通過註解表徵entity:

public class User {
    @Id
    private Long id;
 
    private String name;
 
    @Transient
    private int tempUsageCount; // not persisted
 
   // getters and setters for id and user ...
}

我們通過@Entity對User進行註解,GreenDAO通過識別@Entity在編譯期生成支持資料庫的對象。                

@Entity還支持一些屬性,對該對象進行描述,此時我們對上述代碼進行擴展:

@Entity(
        // If you have more than one schema, you can tell greenDAO
        // to which schema an entity belongs (pick any string as a name).
        schema = "myschema",
        
        // Flag to make an entity "active": Active entities have update,
        // delete, and refresh methods.
        active = true,
        
        // Specifies the name of the table in the database.
        // By default, the name is based on the entities class name.
        nameInDb = "AWESOME_USERS",
        
        // Define indexes spanning multiple columns here.
        indexes = {
                @Index(value = "name DESC", unique = true)
        },
        
        // Flag if the DAO should create the database table (default is true).
        // Set this to false, if you have multiple entities mapping to one table,
        // or the table creation is done outside of greenDAO.
        createInDb = false,
 
        // Whether an all properties constructor should be generated.
        // A no-args constructor is always required.
        generateConstructors = true,
 
        // Whether getters and setters for properties should be generated if missing.
        generateGettersSetters = true
)
public class User {
  ...
}

基本屬性

@Entity
public class User {
    @Id(autoincrement = true)
    private Long id;
 
    @Property(nameInDb = "USERNAME")
    private String name;
 
    @NotNull
    private int repos;
 
    @Transient
    private int tempUsageCount;
 
    ...
}

 @Id:對於資料庫來說,在數據表中作為主鍵,類型預設為long型,autoincrement =true使得id自增。

@Property :將java class中的欄位名映射為Property 提供的名字,在上述代碼中就是將name映射為USERNAME,預設情況下,如果欄位是駝峰命名轉為下劃線命名,如customName 轉換為                                CUSTOM_NAME。

@NotNull: 標記基本類型為非空。

@Transient  :表示java class中的該欄位不會存儲在資料庫中,是一個緩存值。

索引

如果我們不喜歡,GreenDAO定製的預設索引,我們可以自行設置新的索引,這是需要屬性@index。

@Entity
public class User {
    @Id private Long id;
    @Index(unique = true)
    private String name;
}

id是資料庫的唯一索引,即主鍵。但我們可以通過unique = true,指定name索引也是唯一的。

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Android 監聽 WiFi 開關狀態 轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/70854309 本文出自 "【趙彥軍的博客】" WifiSwitch_Presenter 源碼: WifiSwitch_Interface 源 ...
  • 轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/70854942 本文出自 "【趙彥軍的博客】" GPS_Presenter GPS_Interface 回調介面 在 Activity 中使用 ...
  • 轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/71750907 本文出自 "【趙彥軍的博客】" 什麼是 Monkey Monkey 是一個 Android 自動化測試小工具。主要用於Android 的壓力測試, 主要目的就是為了測試 ...
  • Unity項目,需要用Xcode運行,結果報了錯誤。 解決方案: 1、打開終端, 2、輸入以下命令: chmod +x /Users/......./MapFileParser.sh (MapFileParser.sh所在的目錄) ...
  • https://developer.apple.com/contact/phone/ ...
  • 一,效果圖。 二,工程圖。 三,代碼。 RootViewController.h RootViewController.m ...
  • 首頁中點擊城市TextView調轉到當前城市選擇Activity fragment_city.xml ...
  • 一 fragment_home.xml 二 home_head_page.xml banner 兩頁Bar標誌 熱門電影三個作為一體addHeaderView(headView) 進RefreshListView 三 GoodsListAdapter: 1.SimpleDraweeView實現圓角圖 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...