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

public interface Storage extends Serializable ...{
IdEntity get( Class entityClass, Serializable id );

Serializable create ( IdEntity object ) throws CreateException;

IdEntity update ( IdEntity object ) throws UpdateException;

Serializable merge ( IdEntity object ) throws StorageException;

int delete( Class entityClass, Serializable id ) throws CreateException;

int delete( IdEntity object ) throws CreateException;

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

public class MemoryStorage implements Storage ...{

private static final long serialVersionUID = 8611213748834904125L;


private Map memory = new HashMap();


private Map getEntityMap ( Class entityClass ) ...{

if (entityClass != null) ...{
Map tryMap = (Map) memory.get(entityClass);

if (tryMap == null) ...{

synchronized(memory) ...{
tryMap = new HashMap();
memory.put(entityClass, tryMap);
}
}
return tryMap;

} else ...{
return null;
}
}


private IdEntity intStore( Class entityClass, IdEntity object ) ...{
getEntityMap(entityClass).put(object.getId(), object);
return object;
}


public IdEntity get( Class entityClass, Serializable id ) ...{

if (entityClass != null && id != null) ...{
return (IdEntity) getEntityMap(entityClass).get(id);

} else ...{
return null;
}
}


public Serializable create ( IdEntity object ) throws CreateException ...{

if (object == null) ...{
throw new CreateException("Either given class or object was null");
}

if (object.getId() == null) ...{
throw new CreateException("Cannot store object with null id");
}

if (get(object.getClass(), object.getId()) != null) ...{
throw new DuplicateKeyException("Object with this id already exists.");
}
return intStore(object.getClass(), object).getId();
}


public IdEntity update ( IdEntity object ) throws UpdateException ...{

if (object == null) ...{
throw new UpdateException("Cannot update null object.");
}

if ( get(object.getClass(), object.getId())==null ) ...{
throw new UpdateException("Object to update not found.");
}
return intStore(object.getClass(), object);
}


public Serializable merge ( IdEntity object ) throws StorageException ...{

if (object == null) ...{
throw new StorageException("Cannot merge null object");
}

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

} else ...{
return update(object).getId();
}
}


public int delete( Class entityClass, Serializable id ) throws CreateException ...{

try ...{

if (get(entityClass, id) != null) ...{
getEntityMap(entityClass).remove(id);
return 1;

} else ...{
return 0;
}

} catch (Exception e) ...{
throw new CreateException(e);
}
}


public int delete( IdEntity object ) throws CreateException ...{

if (object == null) ...{
throw new CreateException("Cannot delete null object");
}
return delete(object.getClass(), object.getId());
}


public Collection findAll( Class entityClass ) ...{

if (entityClass != null) ...{
return getEntityMap(entityClass).values();

} else ...{
return new ArrayList();
}
}


public void reset() ...{
this.memory = new HashMap();
}

}
查看本文来源