DAO层常用的查询方法

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();
 } 

转载于:https://my.oschina.net/rouchongzi/blog/145896

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值