DAO层常用的查询方法
2019独角兽企业重金招聘Python工程师标准>>>
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
DAO层常用的查询方法相关推荐
- 实现以最快速度搭建springboot后台web项目并连通数据库实现控制层与服务层与dao层的select查询...
背景 Servlet的出现 上世纪90年代,随着Internet和浏览器的飞速发展,基于浏览器的B/S模式随之火爆发展起来.最初,用户使用浏览器向WEB服务器发送的请求都是请求静态的资源,比如html ...
- Dao层抽取BaseDao公共方法
Dao层抽取BaseDao公共方法 设计IBseDao接口,定义公共的CRUD方法. // IBaseDao 接口,定义公共的CRUD方法 public interface IBaseDao<T ...
- 关于DAO层统计总数的方法
DAO层统计数据库某属性总数的功能是比较常见的,代码如下: /** * 获取用户总人数 * @return 返回总人数 */public int getUsernumber(){Connection ...
- oracle 查询连接方法,Oracle 常用连接查询方法和函数
Oracle常用的连接查询方法(以oracle自带的表做的练习),left join是以左表的记录为基础的,左表中的记录会全部显示,右表只显 一:Oracle常用的连接查询方法(以oracle自带的表 ...
- SpringBoot Dao层常用注解
@Repository:作用在DAO层,将该类依赖注入,其他层就可以调用该mapper,如果启动类添加了@MapperScan注解,就不需要再写这个注解. @Data:注解在实体类上,为该实体类提供g ...
- java dao层的泛型get方法_dao层的泛型实现(2种方法)
一: package com.wzs.test2.dao; import java.util.List; public interface CommonDAO { public void sava(T ...
- Django ORM 常用的查询方法
1.exact(SQl精确查找 =/is) class Article(models.Model):title = models.CharField(max_length=200)content = ...
- Pymongo常用查询方法总结
Python 直接连接mongodb数据库进行查询操作 1.安装所需模块 使用到的是pymongo模块,安装方法:pip install pymongo 2.环境验证 3.连接数据库 import p ...
- 实战SSM_O2O商铺_32【商品】商品编辑之Dao层的实现
文章目录 概述 Dao层接口 ProductDao#selectProductById ProductDao#updateProduct ProductImgDao#deleteProductImgB ...
最新文章
- iOS LLDB调试命令(Low Lever Debug)
- AWS — AWS EC2
- 动态规划原理介绍(附7个算例,有代码讲解)
- 计算机网络(十四)-CSMA/CD协议
- 数据3分钟丨Databricks与Snowflake开撕;阿里云多款自研数据库支撑首个“100%云上双11”...
- 求最大公约数(辗转相除)
- mysql 存储过程 错误码_mysql存储过程中的错误处理_mysql
- Perforce的环境变量配置
- visjs使用小记-2.option参数注释说明
- Linkerd2安装和使用
- win7 做服务器开网站,win7下创建Web服务器
- 幅度和幅值有区别吗_你知道避雷器与浪涌保护器二者的区别吗?
- 内存碎片产生原因及解决办法
- php下载excel乱码,如何解决php下载excel乱码的问题
- 实用的科技论文写作短语
- 用html做祝福语朋友,对朋友的励志祝福语大全
- 数字电路34( 计数器—二进制加计数器)
- winmerge多个文件夹生成html,winmerge使用方法,winmerge使用教程
- COMSOL弱形式解微分方程
- 光电耦合器原理及应用介绍