HibernateTemplate方法的使用

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   使用   sp   for   

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方法的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.