【private HibernateTemplate template;】 的作用

來源:互聯網
上載者:User

標籤:out   turn   res   常用操作   etc   als   getc   lex   bool   

【private HibernateTemplate template;】 的作用

這個是在spring中定義了一個bean,它是org.springframework.orm.hibernate3.HibernateTemplate的一個執行個體,
這個類是hibernate的模板類,裡面有很多hibernate的常用操作方法,如常用CRUD增刪改查,在我們的DAO中一般都會這麼寫一個。

package com.sxl.daoImpl;import java.sql.SQLException;import java.util.List;import org.hibernate.Query;import org.hibernate.HibernateException;import org.hibernate.Session;import org.springframework.orm.hibernate3.HibernateCallback;import org.springframework.orm.hibernate3.HibernateTemplate;import com.sun.istack.internal.FinalArrayList;import com.sxl.dao.ComputRoomDao;import com.sxl.pojos.TComputroom;public class ComputRoomDaoImpl implements ComputRoomDao {        private HibernateTemplate template;        public HibernateTemplate getTemplate() {        return template;    }    public void setTemplate(HibernateTemplate template) {        this.template = template;    }    public List<TComputroom> findAll() {        // TODO Auto-generated method stub        List<TComputroom> ls=template.find("from TComputroom");        return ls;    }    public List<TComputroom> findByCondition(TComputroom tc) {        // TODO Auto-generated method stub        return null;    }    public boolean addComputRoom(TComputroom tc) {        // TODO Auto-generated method stub        template.save(tc);        return true;    }    public boolean updateComputRoom(TComputroom tc) {        // TODO Auto-generated method stub        //……                        return false;    }    public boolean delComputRoom(TComputroom tc) {        // TODO Auto-generated method stub        TComputroom tc2=template.load(TComputroom.class, tc.getCrId());        template.delete(tc2);        return true;    }    public List<TComputroom> getPage(final int page,final int rows) {        // TODO Auto-generated method stub        List<TComputroom> ls=template.execute(new HibernateCallback<List>() {            public List doInHibernate(Session session)                    throws HibernateException, SQLException {                // TODO Auto-generated method stub                Query query=session.createQuery("from TComputRoom order by cr_id");                query.setFirstResult((page-1)*rows);                query.setMaxResults(rows);                                return query.list();            }                    });        return ls;    }    public int getAllrows() {        // TODO Auto-generated method stub        List<TComputroom> ls=template.find("from TComputRoom");        int allrows = ls.size();        System.out.println(allrows);        return allrows;    }    public List<TComputroom> pageTComputroom(final int page,final int rows,            final String searchid,final String searchname) {        // TODO Auto-generated method stub        String HQL=null;        if (!searchid.equals("")&&!searchname.equals("")) {            HQL = "from TComputroom where cr_id =‘"+searchid+"‘ cr_name like ‘"+searchname+"%‘ order by cr_id";        }        if (!searchname.equals("")&&searchid.equals("")) {            HQL="from TComputroom where cr_name like ‘%"+searchname+"%‘ order by cr_id";        }        if (!searchid.equals("")&&searchname.equals("")) {            HQL="from TComputroom where cr_id=‘"+searchid+"‘ order by cr_id";        }        if (searchid.equals("")&&searchname.equals("")) {            HQL="from TComputroom order by cr_id";                    }        final String h=HQL;        List<TComputroom> ls=template.execute(new HibernateCallback<List>() {            public List doInHibernate(Session session)                    throws HibernateException, SQLException {                // TODO Auto-generated method stub                Query query=session.createQuery(h);                query.setFirstResult((page-1)*rows);                query.setMaxResults(rows);                return query.list();            }                    });                return ls;    }    public int getAllrows(String searchid, String searchname) {        // TODO Auto-generated method stub        String HQL=null;        if (!searchid.equals("")&&!searchname.equals("")) {            HQL="from TComputroom where cr_id=‘"+searchid+"‘and cr_name like‘%"+searchname+"%‘ order by cr_id";        }        if (!searchname.equals("")&&searchid.equals("")) {            HQL="from TComputroom where cr_name =‘"+searchname+"‘ order by cr_id";        }        if (searchname.equals("")&&!searchid.equals("")) {            HQL="from TComputroom where cr_id =‘"+searchid+"‘ order by cr_id";        }        if (searchname.equals("")&&searchid.equals("")) {            HQL="from TComputroom  order by cr_id";        }        List<TComputroom> ls=template.find(HQL);        int allrows =ls.size();        System.out.println(allrows);         return allrows;    }}

 

 

 

【private HibernateTemplate template;】 的作用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.