大家好,我是j2ee新手。想请教一下大家开发中是这样分层的么,dao怎么写会好一点。dao层如果直接用hibernate自动产生的dao有什么缺点?自己写的话,大家都怎么实现的?是不是用session实现crud操作效率低,用hql语句效率高些。用hql语句的怎么实现。谢谢!

解决方案 »

  1.   

    根据需要封装HQL,提供不同的接口。
      

  2.   

    dao做个接口的定义。然后daoimpl是具体实现类。自动生成的dao缺点不多,就是和你的项目还不够贴切!session就是hql。。
      

  3.   

    示例:
    import java.util.List;import com.ssh.orm.Users;public interface UsersDao {
    public boolean insert(Users users) throws Exception;public Users select(int id) throws Exception;public boolean update(Users users) throws Exception;public boolean delete(int id) throws Exception;public List selectAll() throws Exception;public List selectAllByPage(int curPage, int lineSize) throws Exception;public int getCount() throws Exception;}daoImpl:
     
    import java.util.List;import org.hibernate.Query;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.ssh.dao.UsersDao;
    import com.ssh.orm.Users;public class UsersDaoImpl extends HibernateDaoSupport implements UsersDao {
    public boolean delete(int id) throws Exception {
       String hql = "delete from Users where id=:id";// 注意删除的写法!
       Query q = this.getSession().createQuery(hql);
       q.setParameter("id", id);
       if (q.executeUpdate() > 0) {
        return true;
       }
       return false;
    }public boolean insert(Users users) throws Exception {
       this.getSession().save(users);
       return true;
    }public Users select(int id) throws Exception {
       String hql = "from Users u where u.id=:id";
       Query q = this.getSession().createQuery(hql);
       q.setParameter("id", id);
       List l = q.list();
       if (l.size() > 0) {
        return (Users) l.get(0);
       }
       return null;
    }public boolean update(Users users) throws Exception {
       this.getHibernateTemplate().update(users);//注意更新操作
       return true;
    }public List selectAll() throws Exception {
       List all = null;
       String hql = "from Users";
       Query q = this.getSession().createQuery(hql);
       List l = q.list();
       if (l.size() > 0) {
        all = l;
       }
       return all;
    }public List selectAllByPage(int curPage, int lineSize) throws Exception {
       List all = null;
       String hql = "from Users";
       Query q = this.getSession().createQuery(hql);
       q.setFirstResult((curPage - 1) * lineSize);
       q.setMaxResults(lineSize);
       List l = q.list();
       if (l.size() > 0) {
        all = l;
       }
       return all;
    }public int getCount() throws Exception {
       String hql = "select count(*) from Users";
       Query q = this.getSession().createQuery(hql);
       if (q.list().size() > 0) {
        return (Integer) q.list().get(0);
       }
       return 0;
    }
    }
      

  4.   

    内容如下
    com.conn   连接类 里边装入同步异步的方法连接数据库,一般连接数据库的文件信息在properties提取
    com.entity 实体类 对应的信息完全就是数据库内表中的字段有get set方法
    com.dao    接口类 里边装进去所有实体类的调用接口
    com.impl   实现类 将接口加以实现一般也吧impl包放于dao之下
    com.proxy  代理类 实现dao接口,但是不实现其方法,因为impl中有方法的实现,利用代理类进行调用
    com.factory工厂类 在工厂类里边返回相应的代理对象,利用工厂类进行调用相应的代理类
    com.serv   控制类 在此类中只调用工厂类相应的方法,不在重复写入java代码
    com.filter 过滤器 不解释
    com.listen 监听器 不解释
    com.xxx    功能包 因人而异
    一般的情况是这样的结构 至少实现三层的概念ssh框架也要给予多层的模式进行附加
      

  5.   

    黑莲居士,删除的方法中id=:id求解释,谢谢!