HibernateUtil 小弟写的hibernate东西类 - 代码共享

[代码] hibernateUtils   package cn.itcast.oa0808.hibernate;

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;

    /**
     * http://www.haofapiao.com/linked/20130213.do;  比来没事写了一次hibernate的东西类,不知道写的有没有错,感受有许多欠好的当地 尤其是在前面界说了许多static类型的变量, 这是欠好的感受,
     * 浪费资源
     * 
     * @author 安普尚 安小沫 2013-1-24 下午08:57:02
     */
    public class HibernateSessionFactory {
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
        private static final ThreadLocal threadLocal = new ThreadLocal();
        private static Configuration configuration = new Configuration();
        private static SessionFactory sessionFactory;
        private static String config = CONFIG_FILE_LOCATION;

        private HibernateSessionFactory() {

        }

        /**
         * 得到session
         * 
         * @Name getSession
         * @Description 这个办法是得到session
         * @Create:2013-1-24 下午08:37:32 《创立日期》
         */
        public static Session getSession() {
            // 这里的session是从当时线程中取得的,意图是便于管理业务,有必要取得是同一个衔接, 也相当于servlet中的connection
            Session session = (Session) threadLocal.get();
            if (session == null || !session.isOpen()) {// 若是session为null或许session没有翻开,
                if (sessionFactory == null) {
                    rebuildSessionFactory();
                }
                session = (sessionFactory != null) ? sessionFactory.openSession()// 若是sessionFactory
                        // !=null 那么就把session翻开
                        : null;
                threadLocal.set(session);// 把session放到线程中
            }

            return session;
        }

        /**
         * 
         * @Name rebuildSessionFactory
         * @Description 加载配置文件, 创立sessionFactory
         * @Create:2013-1-24 下午09:09:12 《创立日期》
         */
        private static void rebuildSessionFactory() {
            configuration.configure(config);
            sessionFactory = configuration.buildSessionFactory();
        }

        /**
         * 
         * @Name colseSession
         * @Description 封闭session,其实不必要个人封闭,
         * @Create:2013-1-24 下午09:14:35 《创立日期》
         */
        public static void colseSession() {
            Session session = threadLocal.get();
            threadLocal.set(null);
            if (session != null) {
                session.close();
            }
        }

        /**
         * 
         * @Name getSessionFactory 其实就是一个缓存
         * @Description 得到getSessionFactory 用于得到session
         * @Create:2013-1-24 下午09:16:41 《创立日期》
         */
        public static SessionFactory getSessionFactory() {
            return configuration.buildSessionFactory();
        }

        /**
         * 
         * @Name setConfigFile
         * @Description 这个是个人指定配置文件的方位
         * @Create:2013-1-24 下午09:19:52 《创立日期》
         */
        public static void setConfigFile(String configName) {
            HibernateSessionFactory.config = configName;
            sessionFactory = null;
        }

        /**
         * 
         * @Name getConfigration
         * @Description  得到创立sessionFactory的 类
         * @Create:2013-1-24 下午09:21:47 《创立日期》
         */
        public static Configuration getConfigration() {
            return configuration;
        }

    } http://www.starkp.com/linked/20130213.do; 
posted @ 2013-02-14 02:31  chinadiy197601  阅读(144)  评论(0编辑  收藏  举报