您的位置: 兴安盟信息网 > 游戏

struts2的demo中show case的crud示例

发布时间:2019-09-13 19:06:33

开始再看这个crud示例时,居然发现我没有准备数据库信息及数据也可以运行成功,后来看了实现才明白,该示例使用了Map模拟数据库存储操作数据,不过struts2的实现还是又可以观飨的地方,下面就看看它的实现。

首先,定义持久类存储接口Storage:

publicinterfaceStorageextendsSerializable...{

IdEntityget(ClassentityClass,Serializableid);

Serializablecreate(IdEntityobject)throwsCreateException;

IdEntityupdate(IdEntityobject)throwsUpdateException;

Serializablemerge(IdEntityobject)throwsStorageException;

intdelete(ClassentityClass,Serializableid)throwsCreateException;

intdelete(IdEntityobject)throwsCreateException;

CollectionfindAll(ClassentityClass);

}

很明了,定义了操作数据的几种的方法,然后实现了一种使用map存储数据的方式(当然你可以使用自己存储数据的方式实现,比如xml等等)代码如下: publicclassMemoryStorageimplementsStorage...{

privatestaticfinallongserialVersionUID=8611213748834904125L;

privateMapmemory=newHashMap();

privateMapgetEntityMap(ClassentityClass)...{

if(entityClass!=null)...{

MaptryMap=(Map)memory.get(entityClass);

if(tryMap==null)...{

synchronized(memory)...{

tryMap=newHashMap();

memory.put(entityClass,tryMap);

}

}

returntryMap;

}else...{

returnnull;

}

}

privateIdEntityintStore(ClassentityClass,IdEntityobject)...{

getEntityMap(entityClass).put(object.getId(),object);

returnobject;

}

publicIdEntityget(ClassentityClass,Serializableid)...{

if(entityClass!=null&&id!=null)...{

return(IdEntity)getEntityMap(entityClass).get(id);

}else...{

returnnull;

}

}

publicSerializablecreate(IdEntityobject)throwsCreateException...{

if(object==null)...{

thrownewCreateException("Eithergivenclassorobjectwasnull");

}

if(object.getId()==null)...{

thrownewCreateException("Cannotstoreobjectwithnullid");

}

if(get(object.getClass(),object.getId())!=null)...{

thrownewDuplicateKeyException("Objectwiththisidalreadyexists.");

}

returnintStore(object.getClass(),object).getId();

}

publicIdEntityupdate(IdEntityobject)throwsUpdateException...{

if(object==null)...{

thrownewUpdateException("Cannotupdatenullobject.");

}

if(get(object.getClass(),object.getId())==null)...{

thrownewUpdateException("Objecttoupdatenotfound.");

}

returnintStore(object.getClass(),object);

}

publicSerializablemerge(IdEntityobject)throwsStorageException...{

if(object==null)...{

thrownewStorageException("Cannotmergenullobject");

}

if(object.getId()==null||get(object.getClass(),object.getId())==null)...{

returncreate(object);

}else...{

returnupdate(object).getId();

}

}

publicintdelete(ClassentityClass,Serializableid)throwsCreateException...{

try...{

if(get(entityClass,id)!=null)...{

getEntityMap(entityClass).remove(id);

return1;

}else...{

return0;

}

}catch(Exceptione)...{

thrownewCreateException(e);

}

}

publicintdelete(IdEntityobject)throwsCreateException...{

if(object==null)...{

thrownewCreateException("Cannotdeletenullobject");

}

returndelete(object.getClass(),object.getId());

}

publicCollectionfindAll(ClassentityClass)...{

if(entityClass!=null)...{

returngetEntityMap(entityClass).values();

}else...{

returnnewArrayList();

}

}

publicvoidreset()...{

this.memory=newHashMap();

}

}

查看本文来源

儿童化痰的药哪个好
小孩发烧原因
动脉硬化吃什么药
热淋清颗粒怎样服用
猜你会喜欢的
猜你会喜欢的