greenDao3.0使用小结

关于greendao

greendao官网
GitHub
greenDao是一个将对象映射到SQLite数据库中的轻量且快速的ORM解决方案。


db相关的packageclass

greenDao3.0使用小结_第1张图片
P80IG`P8{C9J1SS_}6DR2V7.png

配置

app的build.gradle中

  • apply plugin: 'org.greenrobot.greendao' // apply plugin
  •   greendao{
          //指定数据库schema版本号,迁移等操作会用到;
          schemaVersion 1
          //通过gradle插件生成的数据库相关文件的包名,默认为你的entity所在的包名;
          daoPackage 'com.hyzlseries.march.claimupload.db.dao'
          //自定义生成数据库文件的目录,可以将生成的文件放到我们的java目录中,而不是build中,这样就不用额外的设置资源目录了。
          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

存储EntityA类型的数据 Entity申明

  • 服务器返回的数据
`{
  "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": "中国人民人寿保险股份有限公司福建省分公司",
      "InsCompanyCode": "RSFJ",
      "CompanyID": 1,
      "CompanyName": "兴业银行股份有限公司",
      "CompanyCode": "XYYH",
      "APPReceiptList": [
        {
          "ReceiptID": 849,
          "ReceiptNo": "0124",
          "ReceitpTime": null,
          "ImagePath": "/中国人民人寿保险股份有限公司福建省分公司/20171016/oth/RSFJXYYHY171016/201710161558504500385.jpg",
          "ReceiptType": 1,
          "ReceiptTypeName": "发票",
          "InsClaimCaseID": "RSFJXYYHY171016010001",
          "Operator": 0
        },
        {
          "ReceiptID": 1383,
          "ReceiptNo": "资料",
          "ReceitpTime": null,
          "ImagePath": "/中国人民人寿保险股份有限公司福建省分公司/20171016/rec/RSFJXYYHY171016/201710161555539199416.jpg",
          "ReceiptType": 2,
          "ReceiptTypeName": "非发票",
          "InsClaimCaseID": "RSFJXYYHY171016010001",
          "Operator": 0
        }
      ]
    }
  ],
  "Total": 1,
  "Code": "0000",
  "Message": "查询成功"
}
  • Entity申明
@Entity
public class ScanDetailModel {
    //不能用int  GreenDao的主键必须设置成包装类 Long , 大写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(/**指定转换器 **/converter = ModelConverent2String.class,/**指定数据库中的列字段**/columnType = String.class)
    private List APPReceiptList;//案件发票集合

  ...
}

如上在申明EntityAList属性的时候,需要指定转换器,即添加@Convert()注解。

ModelConverent2String

/**
 * =============================================
 * 作    者:Junl(袁军亮)
 * 版    本:1.0
 * 描    述:主要是通过greenDao 提供的PropertyConverter,gson,转换对象到字符串存到数据库。
 * 参    考:http://blog.csdn.net/zhangle1hao/article/details/71412999  http://blog.csdn.net/zxm317122667/article/details/73528387
 * 创建日期:2017/11/9
 * 人生如诗:人生若只如初见,何事秋风悲画扇。
 * =============================================
 */

public class ModelConverent2String implements PropertyConverter, String> {

    @Override
    public List convertToEntityProperty(String databaseValue) {

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


    @Override
    public String convertToDatabaseValue(List entityProperty) {

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

增删改查

/**
 * =============================================
 * 作    者:Junl(袁军亮)
 * 版    本:1.0
 * 描    述:数据库增删改查
 * 

* 创建日期:2017/11/9 * 人生如诗:人生若只如初见,何事秋风悲画扇。 * ============================================= */ public class ScanDetailDaoUtil { private static final String TAG = ScanDetailDaoUtil.class.getSimpleName(); public ScanDetailDaoUtil() {} /** * 新增数据 */ 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; } /** * 查询 * @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 案件号 * @param position 要删除的位置 * @return * * 删除思路:查询到ModelB ---> 删除position对应的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 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 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(); } } }

碰到的问题

  • 如何操作ListEntityB的某一个属性,如ReceiptModel类中ReceiptNo属性更改。
    解决方案:
    update
    update不起作用 ,可以query先拿到Entity属性值,设置完毕后再update

你可能感兴趣的:(greenDao3.0使用小结)