zoukankan      html  css  js  c++  java
  • 【private HibernateTemplate template;】 的作用

    【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;
        }
    
    }
     
  • 相关阅读:
    转角色权限系统的一些概念
    error message cs0012
    关于Action返回结果类型的事儿(下)
    MVC中权限的知识点及具体实现代码
    iis7 发布mvc3 遇到的HTTP错误 403.14Forbidden Web 服务器被配置为不列出此目录的内容及Login on failed for "IIS APPPOOL\ASP.NET v4.0"问题
    关于获取时间段的整理片段
    ASP.NET MVC – 关于Action返回结果类型的事儿(上)
    Lucene 查询权重排序因子解释(备查)
    Lucene代替SQL Server NewGuid获取随机结果
    如何在Web数据挖掘中保证用户访问速度的一点实践(SQLite+Quartz)
  • 原文地址:https://www.cnblogs.com/shenxiaolin/p/6411382.html
Copyright © 2011-2022 走看看