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;
        }
    
    }
     
  • 相关阅读:
    PyCharm设置Python版本,你肯定不知道!
    Python学习笔记之二——Python的运行机制,一般人肯定不会
    Django开发登录功能实战
    Python基础语法总结【新手必学】
    Python爬虫实现抓取腾讯视频所有电影【实战必学】
    委托和事件:要注意的事项
    asp.net网页防刷新重复提交、防后退解决办法!
    重新复习基础使用的网上资料
    JS和CS互访【后台前台代码调用JavaScript变量以及JavaScript调用代码变量】
    重新学习基础:草稿3(2)【后续】
  • 原文地址:https://www.cnblogs.com/shenxiaolin/p/6411382.html
Copyright © 2011-2022 走看看