标签: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;】 的作用
标签:out turn res 常用操作 etc als getc lex bool
原文地址:http://www.cnblogs.com/shenxiaolin/p/6411382.html