Android ORM Series Greendao custom type converter with code obfuscation configuration

Source: Internet
Author: User

Sometimes there are some attributes in our entity classes, but there are no corresponding types in the database, and we need custom converters for type conversions. The most common treatment is the date type. If the accuracy requirement is not high, we will convert to a long type to store, read the time to say that it is converted to the date type. So if we want it to be stored directly in a time string, this is a good thing, Greendao provides us with support for custom type conversions.

First, we need to implement the Propertyconverter interface to implement the two methods inside the converttoentityproperty and Converttodatabasevalue , the interface requires two generic parameters, the first parameter is the type in the entity class, and the second parameter is the type stored in the database. Now suppose we need to convert the date to a string for storage, so the implementation of the interface should be like this.

publicclass DateStringConverter implements PropertyConverter<Date,String> {    @Override    public Date convertToEntityProperty(String databaseValue) {        returnnull;    }    @Override    public String convertToDatabaseValue(Date entityProperty) {        return bull;    }}

And then we're going to convert

 Public  class datestringconverter implements Propertyconverter<Date, String> {    Private Static FinalString default_format="Yyyy-mm-dd HH:mm:ss"; @Override PublicDate Converttoentityproperty (String databasevalue) {returnConvert2date (Databasevalue,default_format); } @Override PublicString Converttodatabasevalue (Date entityproperty) {returnConvert2string (Entityproperty,default_format); } Public StaticString convert2string (DateDate, string format) {string currentdate=NULL;Try{SimpleDateFormat formatter=NewSimpleDateFormat (format); Currentdate=formatter.format (Date); }Catch(Exception e)        {E.printstacktrace (); }returncurrentdate; } Public StaticDate convert2date (String day, string format) {if(Day = =NULL|| Format = =NULL)return NULL; SimpleDateFormat formatter =NewSimpleDateFormat (format);Try{Date dt = Formatter.parse (day);returnDt }Catch(ParseException e)        {E.printstacktrace (); }return NULL; }}

The entity class is then generated

type = schema.addEntity("Demo");type.addStringProperty("test").customType("java.util.Date","cn.edu.zafu.greendao.db.converter.DateStringConverter");

AddProperty is the corresponding type in the database, where we store it as a string, so it is addstringproperty, and then through the customtype function to specify the type of the entity class, here is Java.util.Date, followed by the full class name of our type converter. Then try inserting a piece of data into the database, and we'll find that he's storing it directly in a string,

Finally, a confusing configuration is included to add the following statement to the item that needs to be confused

class * extends de.greenrobot.dao.AbstractDao {    publicstaticclass **$Properties

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android ORM Series Greendao custom type converter with code obfuscation configuration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.