greenDao3.0使用小結(jié)

關(guān)于greendao

greendao官網(wǎng)
GitHub
greenDao是一個(gè)將對(duì)象映射到SQLite數(shù)據(jù)庫(kù)中的輕量且快速的ORM解決方案。


db相關(guān)的packageclass

P80IG`P8{C9J1SS_}6DR2V7.png

配置

app的build.gradle中

  • apply plugin: 'org.greenrobot.greendao' // apply plugin
  •   greendao{
          //指定數(shù)據(jù)庫(kù)schema版本號(hào),遷移等操作會(huì)用到;
          schemaVersion 1
          //通過(guò)gradle插件生成的數(shù)據(jù)庫(kù)相關(guān)文件的包名,默認(rèn)為你的entity所在的包名;
          daoPackage 'com.hyzlseries.march.claimupload.db.dao'
          //自定義生成數(shù)據(jù)庫(kù)文件的目錄,可以將生成的文件放到我們的java目錄中,而不是build中,這樣就不用額外的設(shè)置資源目錄了。
          targetGenDir 'src/main/java'
      }
    
  • compile 'org.greenrobot:greendao:3.2.2' // add library

project的build.gradle中

  • classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin

存儲(chǔ)EntityA<EntityB>類(lèi)型的數(shù)據(jù) Entity申明

  • 服務(wù)器返回的數(shù)據(jù)
`{
  "Rows": 0,
  "Results": [
    {
      "ScanTime": "2017-10-16",
      "InsuredID": 3521,
      "AccountName": "李海玲",
      "IDCardNo": "420106197412240849",
      "InsClaimCaseID": "RSFJXYYHY171016010001",
      "ReceiptNumber": 1,
      "ImgNumber": 2,
      "ClaimCaseState": 14,
      "ClaimCaseStateName": null,
      "Memo": null,
      "InsCompanyID": 1,
      "InsCompanyName": "中國(guó)人民人壽保險(xiǎn)股份有限公司福建省分公司",
      "InsCompanyCode": "RSFJ",
      "CompanyID": 1,
      "CompanyName": "興業(yè)銀行股份有限公司",
      "CompanyCode": "XYYH",
      "APPReceiptList": [
        {
          "ReceiptID": 849,
          "ReceiptNo": "0124",
          "ReceitpTime": null,
          "ImagePath": "/中國(guó)人民人壽保險(xiǎn)股份有限公司福建省分公司/20171016/oth/RSFJXYYHY171016/201710161558504500385.jpg",
          "ReceiptType": 1,
          "ReceiptTypeName": "發(fā)票",
          "InsClaimCaseID": "RSFJXYYHY171016010001",
          "Operator": 0
        },
        {
          "ReceiptID": 1383,
          "ReceiptNo": "資料",
          "ReceitpTime": null,
          "ImagePath": "/中國(guó)人民人壽保險(xiǎn)股份有限公司福建省分公司/20171016/rec/RSFJXYYHY171016/201710161555539199416.jpg",
          "ReceiptType": 2,
          "ReceiptTypeName": "非發(fā)票",
          "InsClaimCaseID": "RSFJXYYHY171016010001",
          "Operator": 0
        }
      ]
    }
  ],
  "Total": 1,
  "Code": "0000",
  "Message": "查詢(xún)成功"
}
  • Entity申明
@Entity
public class ScanDetailModel {
    //不能用int  GreenDao的主鍵必須設(shè)置成包裝類(lèi) Long , 大寫(xiě)L  參考:http://blog.csdn.net/sunsteam/article/details/52634945
    @Id(autoincrement = true)
    private Long id;
    @Unique
    private String InsClaimCaseID;
    private String AccountName;
    private String IDCardNo;
    private int ReceiptNumber;
    private String ScanTime;
    private int InsuredID;
    private int ImgNumber;
    private String Memo;
    private String InsCompanyID;
    private String InsCompanyCode;
    private String InsCompanyName;
    private int CompanyID;
    private int ClaimCaseState;
    private String CompanyCode;
    private String CompanyName;
    @Convert(/**指定轉(zhuǎn)換器 **/converter = ModelConverent2String.class,/**指定數(shù)據(jù)庫(kù)中的列字段**/columnType = String.class)
    private List<ReceiptModel> APPReceiptList;//案件發(fā)票集合

  ...
}

如上在申明EntityAList<EntityB>屬性的時(shí)候,需要指定轉(zhuǎn)換器,即添加@Convert()注解。

ModelConverent2String類(lèi)

/**
 * =============================================
 * 作    者:Junl(袁軍亮)
 * 版    本:1.0
 * 描    述:主要是通過(guò)greenDao 提供的PropertyConverter,gson,轉(zhuǎn)換對(duì)象到字符串存到數(shù)據(jù)庫(kù)。
 * 參    考:http://blog.csdn.net/zhangle1hao/article/details/71412999  http://blog.csdn.net/zxm317122667/article/details/73528387
 * 創(chuàng)建日期:2017/11/9
 * 人生如詩(shī):人生若只如初見(jiàn),何事秋風(fēng)悲畫(huà)扇。
 * =============================================
 */

public class ModelConverent2String implements PropertyConverter<List<ReceiptModel>, String> {

    @Override
    public List<ReceiptModel> convertToEntityProperty(String databaseValue) {

        Type type = new TypeToken<ArrayList<ReceiptModel>>() {
        }.getType();
        ArrayList<ReceiptModel> itemList = new Gson().fromJson(databaseValue, type);
        return itemList;
    }


    @Override
    public String convertToDatabaseValue(List<ReceiptModel> entityProperty) {

        String dbString = new Gson().toJson(entityProperty);
        return dbString;
    }
}

增刪改查

/**
 * =============================================
 * 作    者:Junl(袁軍亮)
 * 版    本:1.0
 * 描    述:數(shù)據(jù)庫(kù)增刪改查
 * <p>
 * 創(chuàng)建日期:2017/11/9
 * 人生如詩(shī):人生若只如初見(jiàn),何事秋風(fēng)悲畫(huà)扇。
 * =============================================
 */

public class ScanDetailDaoUtil {
    private static final String TAG = ScanDetailDaoUtil.class.getSimpleName();

    public ScanDetailDaoUtil() {}

    /**
     * 新增數(shù)據(jù)
     */
    public static boolean insert(ScanDetailModel model) {
        try {
            boolean flag;
            flag = App.getDaoSession().getScanDetailModelDao().insert(model) == -1 ? false : true;
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 查詢(xún)
     * @param InsClaimCaseID
     * @return
     */
    public static ScanDetailModel query(String InsClaimCaseID) {
        try {
            ScanDetailModel model = App.getDaoSession().getScanDetailModelDao().queryBuilder().where(ScanDetailModelDao.Properties.InsClaimCaseID.eq(InsClaimCaseID)).unique();
            return model;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param InsClaimCaseID  案件號(hào)
     * @param position 要?jiǎng)h除的位置
     * @return
     *
     * 刪除思路:查詢(xún)到ModelB ---> 刪除position對(duì)應(yīng)的ModelA--->update
     */
    public static boolean delete(String InsClaimCaseID , int position) {
        try {
            if (!JudgeNullUtil.iStr(InsClaimCaseID))
                return false;
            ScanDetailModel model = App.getDaoSession().getScanDetailModelDao().queryBuilder().where(ScanDetailModelDao.Properties.InsClaimCaseID.eq(InsClaimCaseID)).build().unique();
            if (model != null) {
                List<ReceiptModel> receiptList = model.getAPPReceiptList();
                if (JudgeNullUtil.iList(receiptList)) {
                    receiptList.remove(position);//
                    update(InsClaimCaseID,model);
                }
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 解決bug:Cannot update entity without key - was it inserted before?
     * blog.csdn.net/plmmmmlq/article/details/50404495
     * @param shop
     */
    public static void update(String InsClaimCaseID ,ScanDetailModel shop) {
        try {
            List<ScanDetailModel> list = App.getDaoSession().getScanDetailModelDao().queryBuilder().where(ScanDetailModelDao.Properties.InsClaimCaseID.eq(InsClaimCaseID)).build().list();
            ScanDetailModel model = list.get(0);
            shop.setId(model.getId());
            App.getDaoSession().getScanDetailModelDao().update(shop);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

碰到的問(wèn)題

  • 如何操作List<EntityB>EntityB的某一個(gè)屬性,如ReceiptModel類(lèi)中ReceiptNo屬性更改。
    解決方案:
    update
    update不起作用 ,可以query先拿到Entity屬性值,設(shè)置完畢后再update
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、關(guān)于greenDAO greenDAO應(yīng)該算是當(dāng)前最火的數(shù)據(jù)庫(kù)開(kāi)源框架了,它是一個(gè)將對(duì)象映射到SQLite數(shù)據(jù)...
    當(dāng)幸福來(lái)敲門(mén)58閱讀 14,040評(píng)論 3 19
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評(píng)論 19 139
  • 1. 什么是greenDao 弄明白greenDao之前我們應(yīng)該先了解什么是ORM(Object Relation...
    看一季殘花落幕閱讀 2,658評(píng)論 0 4
  • greenDAO官方主頁(yè):http://greendao-orm.com/ 官方主頁(yè)新地址:http://gree...
    sunny_zhang閱讀 10,310評(píng)論 12 49
  • 關(guān)于GreenDao greenDao是一個(gè)將對(duì)象映射到SQLite數(shù)據(jù)庫(kù)中的輕量且快速的ORM解決方案。 關(guān)于g...
    天天大保建閱讀 675評(píng)論 0 0

友情鏈接更多精彩內(nèi)容