DAO 레이어에서 자주 사용하는 쿼리 방법

4657 단어
public List<ContentDocument> getList(Integer contentId, Integer docType, Integer count, boolean cacheable) {

		String s = "select * from xh_contentdocument where	content_id=:contentId ";
		try {
			if (docType != null) {
				s = s + " and doc_type=:docType ";				
			}
			s = s + " order by top_level desc ,priority asc";
			Query query = getSession().createSQLQuery(s).addEntity(ContentDocument.class);
			query.setInteger("contentId", contentId);
			if (docType != null) {			
				query.setInteger("docType", docType);
			}			
			if (count != null) {
				query.setMaxResults(count);
			}
			query.setCacheable(cacheable);
			return query.list();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
String s = "select bean.channel_id,bean.source_name,bean.is_link,bean.subject from xh_channel_datasource bean ";
 if(typeId!=null){
 s = s +" where	source_name=:source_name";
 }
 s = s+" order by channel_id asc ,priority desc";
 try {
 Query query = getSession().createSQLQuery(s).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
 if(typeId!=null){ 
 query.setString("source_name", typeId);
 }
 query.setCacheable(false);
 return query.list();
 } catch (Exception ex) {
 ex.printStackTrace();
 }
 return null;
Finder f = Finder.create("select bean from Channel bean");
 f.append(" join bean.users user");
 f.append(" where user.id=:userId and bean.parent.id=:parentId");
 f.setParam("userId", userId).setParam("parentId", parentId);
 if (hasContentOnly) {
 f.append(" and bean.hasContent=true");
 }
 f.append(" order by bean.priority asc,bean.id asc");
 return find(f);  
 String hql = "select count(*) from CmsSite bean"; return ((Number) getSession().createQuery(hql).setCacheable(cacheable).iterate().next()).intValue(); 
    Finder f = Finder.create("delete CmsLog bean where 1=1"); if (category != null) { f.append(" and bean.category=:category"); f.setParam("category", category); } if (siteId != null) { f.append(" and bean.site.id=:siteId"); f.setParam("siteId", siteId); } if (before != null) { f.append(" and bean.time<=:before"); f.setParam("before", before); } Query q = f.createQuery(getSession()); return q.executeUpdate(); 
 
 
  List<XmlBean>      @SuppressWarnings("unchecked")
 public List<CmsTopic> getListByChannelIds(Integer[] channelIds) {
 String hql = "from CmsTopic bean where bean.channel.id in (:ids) order by bean.id asc";
 return getSession().createQuery(hql).setParameterList("ids", channelIds).list();
 } 
 
update Status      
public int clearCount(boolean week, boolean month) {
 StringBuilder hql = new StringBuilder("update ContentCount bean");
 hql.append(" set bean.viewsDay=0,commentsDay=0,upsDay=0");
 if (week) {
 hql.append(",bean.viewsWeek=0,commentsWeek=0,upsWeek=0");
 }
 if (month) {
 hql.append(",bean.viewsMonth=0,commentsMonth=0,upsMonth=0");
 }
 return getSession().createQuery(hql.toString()).executeUpdate();
 } 
public void clear(Integer siteId, String channelUrl) {
 StringBuilder sb = new StringBuilder(100);
 Map<String, Object> params = new HashMap<String, Object>();
 sb.append("delete from CmsAcquisitionTemp where site.id=:siteId");
 params.put("siteId", siteId);
 if (StringUtils.isNotBlank(channelUrl)) {
 sb.append(" and channelUrl!=:channelUrl");
 params.put("channelUrl", channelUrl);
 }
 Query query = getSession().createQuery(sb.toString());
 for (Entry<String, Object> entry : params.entrySet()) {
 query.setParameter(entry.getKey(), entry.getValue());
 }
 query.executeUpdate();
 } 

좋은 웹페이지 즐겨찾기