技巧篇(3):hibernate工具类(统一了添加、删除、更新、

2018-05-21  本文已影响0人  唐伟耀

这是一个hibernate工具类,提供了统一的添加、删除、更新、查询(分页查询)的模板,运用hibernate的框架,这一套模板可以适用于各种数据库,然而我们在使用不同数据库的时候只要修改hibernate.cfg.xml文件里的你所选择的数据库的配置。因此我们在开发的过程中可以省去了对各种数据库操作语言的研究,我们只需要掌握Hql,通过对对象的操作,在通过hibernate框架对JDBC的轻量级封装,把其转换成对应的数据库操作语言,从而实现对数据库的操作。在使用这个工具类前,我们要掌握hibernate框架(hql)很重要。


final public class HibernateUtil {

    private static SessionFactory sessionFactory=null;

    //使用线程局部模式

    private static ThreadLocal threadLocal=new ThreadLocal();

    private HibernateUtil(){};

    static {

        sessionFactory=new Configuration().configure().buildSessionFactory();

    }

    //获取全新的全新的sesession

    public static Session openSession(){

        return sessionFactory.openSession();

    }

    //获取和线程关联的session

    public static Session getCurrentSession(){

        Session session=threadLocal.get();

        //判断是否得到

        if(session==null){

            session=sessionFactory.openSession();

            //把session对象设置到 threadLocal,相当于该session已经和线程绑定

            threadLocal.set(session);

        }

        return session;

    }

    //统一的一个修改和删除(批量 hql) hql"delete upate ...??"

    public static void executeUpdate(String hql,String [] parameters){

        Session s=null;

        Transaction tx=null;

        try {

            s=openSession();

            tx=s.beginTransaction();

            Query query=s.createQuery(hql);

            //先判断是否有参数要绑定

            if(parameters!=null&& parameters.length>0){

                for(int i=0;i

                    query.setString(i, parameters[i]);

                }

            }

            query.executeUpdate();

            tx.commit();

        } catch (Exception e) {

            e.printStackTrace();

            throw new RuntimeException(e.getMessage());

            // TODO: handle exception

        }finally{

            if(s!=null&&s.isOpen()){

                s.close();

            }

        }

    }

    //统一的添加的方法

    public  static void save(Object obj){

        Session s=null;

        Transaction tx=null;

        try {

            s=openSession();

            tx=s.beginTransaction();

            s.save(obj);

tx.commit();

        } catch (Exception e) {

            if(tx!=null){

                tx.rollback();

            }

            throw new RuntimeException(e.getMessage());

            // TODO: handle exception

        }finally{

            if(s!=null && s.isOpen()){

                s.close();

            }

        }

    }

    //提供一个统一的查询方法(带分页) hql 形式 from 类  where 条件=? ..

    public static List executeQueryByPage(String hql,String [] parameters,int pageSize,int pageNow){

        Session s=null;

        List list=null;

        try {

            s=openSession();

            Query query=s.createQuery(hql);

            //先判断是否有参数要绑定

            if(parameters!=null&& parameters.length>0){

                for(int i=0;i

                    query.setString(i, parameters[i]);

                }

            }

            query.setFirstResult((pageNow-1)*pageSize).setMaxResults(pageSize);

            list=query.list();

        } catch (Exception e) {

            e.printStackTrace();

            throw new RuntimeException(e.getMessage());

            // TODO: handle exception

        }finally{

            if(s!=null&&s.isOpen()){

                s.close();

            }

        }

        return list;

    }

    //提供一个统一的查询方法 hql 形式 from 类  where 条件=? ..

    public static List executeQuery(String hql,String [] parameters){

     Session s=null;

        List list=null;

        try {

            s=openSession();

            Query query=s.createQuery(hql);

            //先判断是否有参数要绑定

            if(parameters!=null&& parameters.length>0){

                for(int i=0;i

                    query.setString(i, parameters[i]);

                }

            }

            list=query.list();

        } catch (Exception e) {

            e.printStackTrace();

            throw new RuntimeException(e.getMessage());

            // TODO: handle exception

        }finally{

            if(s!=null&&s.isOpen()){

                s.close();

            }

        }

        return list;

    }

}

上一篇 下一篇

猜你喜欢

热点阅读