因此,基本上,您的持久化器应以以下方式实现:
public class MyFieldClassPersister extends StringType { private static final MyFieldClassPersister INSTANCE = new MyFieldClassPersister(); private MyFieldClassPersister() { super(SqlType.STRING, new Class<?>[] { MyFieldClass.class }); } public static MyFieldClassPersister getSingleton() { return INSTANCE; } @Override public Object javaToSqlArg(FieldType fieldType, Object javaObject) { MyFieldClass myFieldClass = (MyFieldClass) javaObject; return myFieldClass != null ? getJsonFromMyFieldClass(myFieldClass) : null; } @Override public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) { return sqlArg != null ? getMyFieldClassFromJson((String) sqlArg) : null; } private String getJsonFromMyFieldClass(MyFieldClass myFieldClass) { // logic here } private MyFieldClass getMyFieldClassFromJson(String json) { // logic here }}
您应该在
onCreate您的
OrmLiteSqliteOpenHelper课堂方法中注册它
@Override public void onCreate(SQLiteDatabaseHolder holder, ConnectionSource connectionSource) { try { //... DataPersisterManager .registerDataPersisters(MyFieldClassPersister.getSingleton()); } catch (SQLException e) { // log exception } }
然后可以在模型中使用它,如下所示:
@DatabaseField(persisterClass = MyFieldClassPersister.class, columnName = "column_name")protected MyFieldClass myFieldClass;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)