struts2的demo中showcase的crud示例

文章作者 100test 发表时间 2007:05:12 13:48:24
来源 100Test.Com百考试题网


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

Serializable create ( IdEntity object )
throws CreateException.

IdEntity 0update ( IdEntity object )
throws UpdateException.

Serializable merge ( IdEntity object )
throws StorageException.

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

int 0delete( 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 0update ( IdEntity object ) throws UpdateException ...{
if (object == null) ...{
throw new UpdateException("Cannot 0update null object.").
}

if ( get(object.getClass(), object.getId())==null ) ...{
throw new UpdateException("Object to 0update 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 0update(object).getId().
}

}


public int 0delete( 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 0delete( IdEntity object ) throws CreateException ...{
if (object == null) ...{
throw new CreateException("Cannot 0delete null object").
}

return 0delete(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().
}


}



相关文章


J2ME中读取Unicode和UTF-8编码文件
用Stripes做JavaWeb开发
struts2的demo中showcase的crud示例
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛