1、查询帖子(Post)为例

查找所有的帖子

    public List<Post> findPosts() {
String hql = "from Post p left join fetch p.user";
List<Post> list= null;
list = (List<Post>) this.ht.find(hql);
return list;
}
    @Transactional(propagation=Propagation.REQUIRED)
public List<Post> findPosts2() {
List<Post> list= null;
DetachedCriteria dc = DetachedCriteria.forClass(Post.class);
list = (List<Post>) this.ht.findByCriteria(dc);
return list;
}
    @Transactional(propagation=Propagation.REQUIRED)
public List<Post> findPosts3() {
List<Post> list= null;
list = (List<Post>) this.ht.findByExample(new Post());
return list;
}

特定条件查询

  @Transactional(propagation= Propagation.REQUIRED)
public List<Post> findPostByUser(String userid) {
String hql = "from Post p where p.user.userid = :userid";
List<Post> list= null;
try{
list = (List<Post>) this.ht.findByNamedParam(hql, "userid",userid);//list = (List<Post>) this.ht.findByNamedParam(hql, new String[]{"userid"},new String[]{userid})
}catch(Exception e){ e.printStackTrace(); } return list; }

分页查询

    @Transactional(propagation=Propagation.REQUIRED)
public List<Post> findPosts(int start,int limit) {
String hql = "from Post p left join fetch p.user";
List<Post> list= null;
DetachedCriteria dc = DetachedCriteria.forClass(Post.class);
list = (List<Post>) this.ht.findByCriteria(dc, start, limit);//this.ht.findByExample(new Post(), start, limit);
return list;
}

原生的sql查询(返回的是object数组)

     @Transactional(propagation=Propagation.REQUIRED)
public List<Post> findPosts4() {
String sql = "select * from post";
List<Post> list= null;
list = (List<Post>) this.ht.getSessionFactory().getCurrentSession().createSQLQuery(sql).list();
return list;
}

HibernateTemplate方法的使用的更多相关文章

  1. hibernateTemplate方法使用

  2. Spring HibernateTemplate的使用

    Spring HibernateTemplate的使用 2008-03-25 11:38 2020人阅读 评论(0) 收藏 举报 springbeanhibernatesessiondaoclass ...

  3. sping整合hibernate之二:dao层开发

     在上一篇日志中将hibernate的会话工厂sessionFactory注入到了spring的容器中,但这样还不够,因为hibernate的增删改查是要使用事务机制的, 所以还要在spring中配置 ...

  4. SSH 架构

    这几天学习了 ssh 架构,中间出了好多错误,现在终于整理好了,就记录下来 ssh机构的框架构成,以及它们的作用 struts2 :这个框架主要用做控制处理的,其核心是 Contraller ,即 A ...

  5. 【SSH】---【Struts2、Hibernate5、Spring4集成开发】【SSH框架整合笔记】

    Struts2.Hibernate5.Spring4集成开发步骤: 一.导入Jar包(基本的大致有41个,根据实际项目的需求自己添加) antlr-2.7.7.jar aopalliance.jar ...

  6. javaSE27天复习总结

    JAVA学习总结    2 第一天    2 1:计算机概述(了解)    2 (1)计算机    2 (2)计算机硬件    2 (3)计算机软件    2 (4)软件开发(理解)    2 (5) ...

  7. SSH实战 · 用spring框架下的hibernatetemplate的get方法出现的问题

    用get方法查询:      return this.getHibernateTemplate().get(Product.class, pid); 出现错误为:id to load is requi ...

  8. 'sessionFactory' or 'hibernateTemplate' is required解决方法

    这种情况就是在通过spring配置hibernate4的时候(注意,这里是hibernate4不是hibernate3,hibernate3的),使用的是HibernateDaoSupport的这种方 ...

  9. HibernateTemplate、HibernateDaoSupport两种方法实现增删改查Good(转)

    Spring+Hibernate两种方法实现增删改查 首先,定义一个Customer的bean类,设置好Customer.hbm.xml文件.再定义好一个Dao接口.准备好一个jdbc.propert ...

随机推荐

  1. autofac 用法总结

    autofac官网: http://autofaccn.readthedocs.io/en/latest/getting-started/index.html autofac作为一个热门ioc框架,还 ...

  2. 10.1综合强化刷题 Day5

    T1 拼不出的数 lost.in/.out/.cpp[问题描述]3 个元素的集合{5; 1; 2}的所有子集的和分别是0; 1; 2; 3; 5; 6; 7; 8.发现最小的不能由该集合子集拼出的数字 ...

  3. 利用Jdk 6260652 Bug解析Arrays.asList

    在java.util.ArrayList源码中: c.toArray might (incorrectly) not return Object[] (see 6260652) 产生疑惑: 附上Jav ...

  4. jcraft--SFTP demo

    import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import ...

  5. Linux下创建和删除用户

    在Linux下创建用户和删除用户,必须在root用户下,如果你当前不是用根用户登录,你可以打开终端,输入"su root"命令,再输入根口令,就可以进入root用户模式下,如下所示 ...

  6. 12.【nuxt起步】-接口请求重构

    用store把api数据交互部分重构出来,让前端更轻一点 新建 /store/gettter.js /store/actions.js /server/config/index.js Index.js ...

  7. openssl之EVP系列之12---EVP_Seal系列函数介绍

    openssl之EVP系列之12---EVP_Seal系列函数介绍     ---依据openssl doc/crypto/EVP_SealInit.pod翻译和自己的理解写成     (作者:Dra ...

  8. 负样本采样及bias校准、ctr平滑

    参考:https://zhuanlan.zhihu.com/p/31529643 在CTR预估中,负样本采样是一种常见的特征工程方法.一般CTR预估的原始正负样本比可能达到1:1000~1:10000 ...

  9. js 动态获取对象多级属性

    var obj={ f1:{f2:{f3:2}} } var key="f1.f2.f3" var value=eval("obj."+key); consol ...

  10. leetcode 46-Permutations and 47-Permutations II

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...