论坛首页 Java企业应用论坛

我的通用DAO理解,请大家指正

浏览 33800 次
精华帖 (2) :: 良好帖 (1) :: 新手帖 (2) :: 隐藏帖 (1)
作者 正文
   发表时间:2008-11-20   最后修改:2008-11-22
DAO
首先声明我的资料大我来自javaeye,先谢谢各位.
但因本人理解能力不足,现把我对通用DAO的引用贴出,望大家指点.
1.BaseDAO 接口
public interface BaseDAO<T, ID extends Serializable> {
public void save(T entity);
public void delete(T entity);
public T findById(Class<T> entityClass, ID id);
}
2.BaseDAO 的实现
public class BaseHibernateDAO<T, ID extends Serializable> extends HibernateDaoSupport implements BaseDAO<T,ID> {  
      private static final Logger logger = Logger.getLogger(BaseHibernateDAO.class);  
    public void save(T entity) {          
        try {  
            getHibernateTemplate().save(entity);  
        } catch (RuntimeException e) {  
            logger.error("保存实体异常", e);  
            throw e;  
        }  
    }  

    public void delete(T entity) {  
        try {  
            getHibernateTemplate().delete(entity);  
        } catch (RuntimeException e) {  
            logger.error("删除实体异常", e);  
            throw e;  
        }  
    }  

    public void saveOrUpdate(T entity) {  
        try {  
            getHibernateTemplate().saveOrUpdate(entity);  
        } catch (RuntimeException e) {  
            logger.error("更新或保存实体异常", e);  
            throw e;  
        }  
    }  

    @SuppressWarnings("unchecked")  
    public T findById(Class<T> entityClass, ID id) {  
        try {  
            return (T) getHibernateTemplate().get(entityClass, id);  
        } catch (RuntimeException e) {  
            logger.error("查找指定ID实体异常,ID:" + id, e);  
            throw e;  
        }  
    }  
3.一个实体DAO接口extends BaseDAO
public interface ProductDAO extends BaseDAO<Product,Integer>{
//如添加
public void productInsert(Product entity);
         public void findById(Product entityClass, Integer id)
        //如除crud外Product自己的业务的方法
          public void myProductDelete(class clz,Serializable pk);
......
}
4.实体DAO的实现
public class ProductDAOImpl extends BaseHibernateDAO<Product,Integer> implements
ProductDAO {
       public void productInsert(Product entity) {
save(entity);
       }
    public void findById(Class<T> entityClass, ID id) {
findById(entityClass,id);
       }
        public void myProductDelete(class clz,Serializable pk){
           Object object = this.getHibernateTemplate().get(clazz, pk);
this.getHibernateTemplate().delete(object);     
}
  ........
}
5.service层 实体接口
public interface ProductService {
// 添加
public void productInsert(Product entity) throws ProductException;
         public void save(Product  entity) throws ProductException;
public void delete(Product  entity) throws ProductException;
public Product findById(Product entityClass, Integer) throws ProductException;
}
6.service层 实体service实现
public class ProductServiceImpl implements ProductService {
private ProductDAO productDAOImpl;

public void delete(int id) throws ProductException {
try{
productDAOImpl.Delete(productDAOImpl.findproductByPrimaryKey(id));
}catch(Exception e ){
e.printStackTrace();
throw new ProductException(this.getClass().getName()+"Product Delete 删除失败");
}
}

public void save(Product product) throws ProductException {
try{
productDAOImpl.save(product);
}catch(Exception e){
e.printStackTrace();
throw new ProductException(this.getClass().getName()+"Product Insert 添加失败");
}
.......
}


请大家帮我看看我这个通用baseDAO的设计是否合理?
具体如:
1.dao的设计有没错?
2.T Class<T>等泛型,反射有没错?
3.service层的传值有没错?
4.这样的异常处理合理吗?
5.就这样的写法是否存在不合理的的写法,而我又不知道的?

     各位拍砖吧,至少知道那里不足,继续学习..........


   发表时间:2008-11-20   最后修改:2008-11-22
期待各位的宝贵意见!!!
前面的朋友提到异常UI的交互,
下面我又接着写了action异常处理,大家看看是否合理
请先看配置:
1.struts-config.xml配置
<global-exceptions>
   <exception key="error" type="com.kkk.exceptions.ChipInException" path="/commons/messages.jsp"/>
</global-exceptions>
2.action中的代码
public ActionForward saveChip(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

if(ename==null||ename.equals("")){
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("ename.null"));
saveMessages(request, messages);
    throw new ChipInException("名字为空");
}
try{
  xxxServiceImpl.insert(product);
}catch(Exception e ){
  e.printStackTrace();
  throw new ChipInException("保存失败");
}

}
3.资源文件中的配置
error=some errors here
ename.null=please select one name first!!!  
4.显示异常页面
<%-- Error Messages --%>
<logic:messagesPresent>
<div class="error">
<html:messages id="error">
${error}<br/>
</html:messages>
</div>
</logic:messagesPresent>

<%-- Success Messages --%>
<logic:messagesPresent message="true">
<div class="message" id="message">
<html:messages id="message" message="true">
${message}<br/>
</html:messages>
</div>

<script type="text/javascript">

new Effect.Highlight('message');
window.setTimeout("Effect.DropOut('message')", 1000);

</script>
</logic:messagesPresent>


我的问题是

我这里的异常处理及转向合理吗?配置文件的配置有没错?
key="error"是不是说明会读取资源文件中的error的值到messages.jsp页面显示?
还是说显示我action中的messages存的ename.null的资源文件值信息?
其实具体应该如何来进行这个异常处理在这几个文件中的配置?这里的异常显示页面message.jsp会不会和<global forwards>的有重合?
我要让"名字为空"这几个字在message.jsp页面中显示有办法吗?

Effect未定义是不是少了包支持?
0 请登录后投票
   发表时间:2008-11-20  
期待看到高人回复
0 请登录后投票
   发表时间:2008-11-20  
建议ID用object代替,不要用占用泛型,想想看看ID不是string就是int,long之类的

其次泛型dao需要有VO的支持,光溜溜的bean用泛型太浪费了。

附件提供了我的一个泛型dao的定义,可以参考
  • DAO.rar (958 Bytes)
  • 下载次数: 1561
0 请登录后投票
   发表时间:2008-11-20  
我看有T entity 和 class<T> clz的两种写法,是直接传对象好还是反射一下好?
0 请登录后投票
   发表时间:2008-11-20  
谢谢楼上朋友,我感觉我的service层传值有点乱,会吗?
0 请登录后投票
   发表时间:2008-11-20  
service层传值可以,业务层永远是核心,没有必要太简化,否则service层就到action里面去了
0 请登录后投票
   发表时间:2008-11-20  
dao和service的异常处理可以吗?
0 请登录后投票
   发表时间:2008-11-20  
dao最好不要显示的抛出任何的异常
service最好抛出自定义异常
0 请登录后投票
   发表时间:2008-11-20  
那在哪里抛出异常  在哪里捕捉异常 并进行处理呢?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics