对天乙社区bbscs8实现的详细分析三

此文为转载:http://www.diybl.com/course/1_web/webjs/2007113/82989.html


OK!我们直接进入DAO层:先看接口,发现与service接口层的方法并不相同,主要是由于service层有些方法是业务处理用,而不是用于数据处理,如createBoard,updateBoard,findBoardsAllTree,getBoardPermission,getBoardMasterPermission,isBoardMaster,当然DAO接口层也有一些方法是没有的:findBoardsNeedCount,findBoardsInIDsfindBoardsIdByParentIDInUse等等,我们看其实现吧:
进入com.laoer.bbscs.BoardHibernateDAO中首先是一些字符常量:
private static final String LOADS_BY_PARENTID_BY_ORDER = "from Board where parentID = ? order by orders";
private static final String LOADS_ALL = "from Board";
public static final String[] FIND_BOARDS = new String[3];
public static final String LOAD_NEXT_ORDER = "select max(orders) from Board where parentID = ?";
private static final String LOAD_IDS_IN_USE = "select id from Board where parentID = ? and useStat = 1";
我们看几个重点的实现方法:
public List findBoardsByParentID(long pid) {
return this.getHibernateTemplate().find(LOADS_BY_PARENTID_BY_ORDER, new Long(pid));
}
其重载方法:
public List findBoardsByParentID(final long pid, final int useStat, final int hidden, final int orderType) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
   public Object doInHibernate(Session s) throws HibernateException {
    Criteria c = s.createCriteria(Board.class);
    c.add(Restrictions.eq("parentID", new Long(pid)));
    if (useStat != -1) {
     c.add(Restrictions.eq("useStat", new Integer(useStat)));
    }
    if (hidden != -1) {
     c.add(Restrictions.eq("isHidden", new Integer(hidden)));
    }
    if (orderType != -1) {
     if (orderType == Constant.FIND_BOARDS_BY_ORDER) {
      c.addOrder(Order.asc("orders"));
     }
     if (orderType == Constant.FIND_BOARDS_BY_MAINPOSTNUM) {
      c.addOrder(Order.desc("mainPostNum"));
     }
     if (orderType == Constant.FIND_BOARDS_BY_POSTNUM) {
      c.addOrder(Order.desc("postNum"));
     }
    }

    return c.list();
   }
});
}
其中用到了条件查询(Criteria Query),参考http://blog.sina.com.cn/u/4a5e7dc401000878 和http://hi.baidu.com/yaolihui/blog/item/7c77b58286a56792f703a663.html,而public List findBoardIdsByParentID(final long pid, final int useStat, final int hidden, final int orderType)采用了构造HQL语句的方式来完成查询!根据parentID预取得Board序列:
public int getNextOrder(long pid) {
List l = getHibernateTemplate().find(LOAD_NEXT_ORDER, new Long(pid));
if (l != null && !l.isEmpty()) {
   if (l.get(0) == null) {
    return 5;
   } else {
    return ((Integer) l.get(0)).intValue() + 5;
   }
} else {
   return 5;
}
}
public int getPostSumNum(final int mainorall, final int useStat, final int hidden) {
int sum = 0;

List list = getHibernateTemplate().executeFind(new HibernateCallback() {
   public Object doInHibernate(Session s) throws HibernateException {
    Criteria c = s.createCriteria(Board.class);
    if (mainorall == 0) {
     c.setProjection(Projections.projectionList().add(Projections.sum("mainPostNum"))); //求mainPostNum的和为结果
    }
    if (mainorall == 1) {
     c.setProjection(Projections.projectionList().add(Projections.sum("postNum")));//求postNum的和为结果
    }
    if (useStat != -1) {
     c.add(Restrictions.eq("useStat", new Integer(useStat)));
    }
    if (hidden != -1) {
     c.add(Restrictions.eq("isHidden", new Integer(hidden)));
    }
    return c.list();
   }
});
if (!list.isEmpty()) {
   Object obj = (Object) list.get(0);//注:结果集只有一个字段
   if (obj != null) {
    sum = ((Integer) obj).intValue();
   }
}
return sum;
}

public List findBoardsInIDs(final List ids, final int useStat, final int hidden) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
   @SuppressWarnings("unchecked")
   public Object doInHibernate(Session s) throws HibernateException {
    Criteria c = s.createCriteria(Board.class);
    if (ids == null) { //ids为空
     List idss = new ArrayList();
     idss.add(new Long(0));
     c.add(Restrictions.in("id", idss));
    } else if (ids.isEmpty()) {//ids为empty
     ids.add(new Long(0));
     c.add(Restrictions.in("id", ids));
    } else {
     c.add(Restrictions.in("id", ids));//Restrictions.in
    }
    if (useStat != -1) {
     c.add(Restrictions.eq("useStat", new Integer(useStat)));
    }
    if (hidden != -1) {
     c.add(Restrictions.eq("isHidden", new Integer(hidden)));
    }
    c.addOrder(Order.asc("orders"));
    return c.list();
   }
});
}

public List findBoardsNeedCount(final int useStat, final int hidden) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
   public Object doInHibernate(Session s) throws HibernateException {
    Criteria c = s.createCriteria(Board.class);

    c.add(Restrictions.or(Restrictions.eq("boardType", new Integer(3)), Restrictions.eq("boardType",
      new Integer(4))));//Restrictions.or 3,4的都需要count

    if (useStat != -1) {
     c.add(Restrictions.eq("useStat", new Integer(useStat)));
    }
    if (hidden != -1) {
     c.add(Restrictions.eq("isHidden", new Integer(hidden)));
    }
    c.addOrder(Order.desc("mainPostNum"));
    c.addOrder(Order.desc("postNum"));
    return c.list();
   }
});
}

public List findBoardsByParentID(final long pid, final int useStat, final int hidden) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
   public Object doInHibernate(Session s) throws HibernateException {
    Criteria c = s.createCriteria(Board.class);
    c.add(Restrictions.eq("parentID", new Long(pid)));//条件!
    if (useStat != -1) {
     c.add(Restrictions.eq("useStat", new Integer(useStat)));//附加条件!
    }
    if (hidden != -1) {
     c.add(Restrictions.eq("isHidden", new Integer(hidden)));
    }
    return c.list();
   }
});
}

public List findBoardsIdByParentIDInUse(long pid) {
return this.getHibernateTemplate().find(LOAD_IDS_IN_USE, new Long(pid));
}
OK!分析完毕!当然,对于具体的方法是怎么被用上的,要看web层了.
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值