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
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...