翻出大半年前写的代码,还是可以揪出点东西、收获良多,比如entiy和协议,数据库的绑定
总结一下
在android下面实现的数据entity的封装。
通过对bean进行配置注解,网络交互,数据库的CRUD操作都只要调用统一接口,具体实现都在BaseEntity父类,以下代码是在发送数据组装的具体实现
@AnoDBType(tableName = "e")
public class ReportShow extends BaseEntity {
@AnoDbField(columnName = "a", isPrymaryKey = true)
private int id;
@AnoDbField(columnName = "b")
@AnoNetField(netName = "time")
private String time;
@AnoDbField(columnName = "c")
@AnoNetField(netName = "position")
BaseEntity类的实现:
public class BaseEntity {
public String createXml() {
StringBuilder builder = new StringBuilder();
Field[] declaredFields = this.getClass().getDeclaredFields();
try {
for (Field field : declaredFields) {
field.setAccessible(true);
AnoNetField annotation = field.getAnnotation(AnoNetField.class);
if (annotation != null) {
String netName = annotation.netName();
builder.append("<").append(netName).append(">");
Object object = field.get(this);
if (object != null) {
builder.append(object);
}
builder.append("</").append(netName).append(">");
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return builder.toString();
以下代码是实现解析网络数据的具体实现,resultFilePath 参数是本地xml的地址,因为收到数据包之后,为了分析每一条协议,我把他持久化保存在本地磁盘上面。 解析成entity时候通过注解设置相应的值,并通过递归的方式寻找每一个子项,子项的子项。。并给他们设置值。最终转成entity对象。
public BaseEntity parseXml(String resultFilePath) {
PlusXmlParser xmlParser = new PlusXmlParser(resultFilePath);
XmlBean xmlBean = xmlParser.getRootXmlBean();
return buildEntity(xmlBean);
}
public BaseEntity buildEntity(XmlBean mainBean) {
Field[] declaredFields = this.getClass().getDeclaredFields();
try {
for (Field field : declaredFields) {
field.setAccessible(true);
AnoNetField annotation = field.getAnnotation(AnoNetField.class);
if (annotation != null && annotation.netName().length() > 0) {
String netName = annotation.netName();
Set<Entry<Integer, XmlBean>> entrySet = mainBean
.getChildren().entrySet();
if (annotation.isChildEntity()) {
List<BaseEntity> list = new ArrayList<BaseEntity>();
for (Entry<Integer, XmlBean> entry : entrySet) {
XmlBean value = entry.getValue();
if (value.getName().equals(netName)) {
Class<? extends BaseEntity> childClass = annotation
.childClass();
BaseEntity newInstance = childClass
.newInstance();
BaseEntity buildEntity = newInstance
.buildEntity(value);
list.add(buildEntity);
}
}
field.set(this, list);
} else {
for (Entry<Integer, XmlBean> entry : entrySet) {
XmlBean value = entry.getValue();
if (netName.equals(value.getName())) {
field.set(this, value.getContent());
}
}
}
} else {
/** 这里是没有配置注解的属性 */
}
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return this;
}
以下是数据库CUID的接口实现,具体代码比较多就不贴上来。因为都是持久化数据,所以这个解析和组装的实现是和网络模块一致的。
public boolean update(BaseEntity entity, BaseEntity alterEntity,String condition) {}
public boolean delete(BaseEntity entity) {}
public boolean insert(BaseEntity baseEntity) {}
public <T> LinkedList<T> query(BaseEntity baseEntity, Class<T> entityClass,
String condition) {}