05年写的一个Common DAO
写得不好,仅为给一个fightman 童鞋用。
?
fightman :
?
就贴了DAO的接口和实现,及一个使用例子,留个你的mail,发给你所有代码
?
Common DAO 接口
/* * Created on 2004-11-19 * */package net.yigoware.jbbs.dao;import java.io.Serializable;import java.util.List;import net.yigoware.jbbs.service.exception.ObjectNotFoundException;/** * * @author Yigo * @createdate 2005-11-04 * @param <T> */// import org.springframework.dao.DataAccessException;public interface DAO<T> { /** * @return */ public List<T> readAll(); // CRUD /** * @param object */ public void create(T object);// throws DataAccessException; /** * @param id * @return * @throws ObjectNotFoundException */ public T read(Serializable id) throws ObjectNotFoundException; /** * update方法:对于dao来说,只要取得那个id的对象,就可以更新,所以他只需要抛出ObjectNotFoundException。 * 而是否存在唯一键的验证,应当放在业务层处理,所以不需要抛出ObjectAlreadyExistException异常。 * * @param id * @param object * @throws ObjectNotFoundException */ public void update(Serializable id, T object) throws ObjectNotFoundException; /** * @param id * @throws ObjectNotFoundException */ public void delete(Serializable id) throws ObjectNotFoundException;}
?
Hibernate的实现
?
/* * Created on 2004-11-19 * */package net.yigoware.jbbs.dao.impl.hibernate;import java.io.Serializable;import java.util.Iterator;import java.util.List;import java.util.Map;import net.yigoware.jbbs.dao.DAO;import net.yigoware.jbbs.service.exception.ObjectNotFoundException;// import org.springframework.dao.DataAccessException;// import org.apache.commons.beanutils.BeanUtils;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;// 这个不能算是渗透哦,没办法,common beanutils没法解决import com.opensymphony.util.BeanUtils;/** * * @author Yigo * @createdate 2005-11-04 * @param <T> */abstract public class DAOImplHibernate<T> extends HibernateDaoSupport implements DAO<T> { private Class objectClass; private DAOImplHibernate() { } /** * @param objectClass */ public DAOImplHibernate(Class objectClass) { this.objectClass = objectClass; } public List<T> readAll() { return this.getHibernateTemplate().loadAll(this.objectClass); } public void create(T object) { // System.out.println("basic..........create............"); this.getHibernateTemplate().save(object); // System.out.println("basic..........create............end......."); } public T read(Serializable id) throws ObjectNotFoundException { T object = (T) this.getHibernateTemplate().get(this.objectClass, id); if (null == object) { throw new ObjectNotFoundException(this.objectClass, id); } return object; } public void update(Serializable id, T object) throws ObjectNotFoundException { T cacheObject = this.read(id); // 虽说这里的BeanUtils有点渗透,但是opensymphony的BeanUtils,SO GREAT! // 比起commons的BeanUtils,第一,可以读取每个value的object对象,第二,一点异常都没有。 Map map = BeanUtils.getValues(object, BeanUtils .getPropertyNames(object)); for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { String key = iter.next().toString(); Object value = map.get(key); if (value != null && !value.toString().equals("")) { BeanUtils.setValue(cacheObject, key, value); } } this.getHibernateTemplate().update(cacheObject); } public void delete(Serializable id) throws ObjectNotFoundException { this.getHibernateTemplate().delete(this.read(id)); }}
?
以及一个使用了改DAO的例子:
package net.yigoware.jbbs.dao.impl.hibernate;import java.io.Serializable;import java.util.Date;import java.util.List;import net.yigoware.jbbs.dao.BoardDAO;import net.yigoware.jbbs.model.Board;import net.yigoware.jbbs.service.exception.ObjectNotFoundException;/** * * @author Yigo * @createdate 2005-11-04 */public class BoardDAOImplHibernate extends DAOImplHibernate<Board> implements BoardDAO { /** * */ public BoardDAOImplHibernate() { super(Board.class); } @Override public void update(Serializable id, Board board) throws ObjectNotFoundException { board.setModifyTime(new Date()); super.update(id, board); } @Override public void create(Board board) { board.setCreateTime(new Date()); super.create(board); } public void createChildrenBoard(Board parent, Board child) { // TODO Auto-generated method stub } public Board getBoardByName(String name) throws ObjectNotFoundException { Board board = null; List list = this.getHibernateTemplate().find( "from Board where name = '" + name + "'"); if (list.size() > 0) { board = (Board) list.get(0); } else { // System.out.println("没有找到坛子哦"); throw new ObjectNotFoundException(Board.class, name); } return board; }}
?
?
?
写得不好,请大家勿拍砖,现在自己看来,很多用泛型的地方也蛮可笑的,尤其是那个private Class objectClass;,毕竟是05年的习作了
?
改项目为非公司项目,自己学习hibernate和spring以及泛型的习作