使用properties创建hibernate配置文件

阅读更多
dialect_interface=org.hibernate.dialect.Oracle9Dialect
driver_class_interface=oracle.jdbc.OracleDriver
url_interface=jdbc:oracle:thin:@172.16.11.244:1521/orcl
username_interface=wolf_rphis
password_interface=wolf
 
hibernate工具
package com.hwt.util;

import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

/**
 * Hibernate工具
 * @author liangdong
 *
 */
public class HibernateUtil{

	private static final ThreadLocal sessionThreadLocal = new ThreadLocal();
	private static Configuration configuration = new Configuration();
	private static org.hibernate.SessionFactory sessionFactory;

	/**
	 * 读取配置文件
	 */
	static {
		try {
			/* 设置hibernate参数 */
			setHibernateProperties(configuration);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取session
	 */
	public static Session getSession() throws HibernateException {
		Session session = (Session) sessionThreadLocal.get();

		if (session == null || !session.isOpen()) {
			if (sessionFactory == null) {
				rebuildSessionFactory();
			}
			session = (sessionFactory != null) ? sessionFactory.openSession() : null;
			sessionThreadLocal.set(session);
		}

		return session;
	}

	/**
	 * 重建session
	 *
	 */
	public static void rebuildSessionFactory() {
		try {
			/* 设置hibernate参数 */
			setHibernateProperties(configuration);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭session
	 */
	public static void closeSession() throws HibernateException {
		Session session = (Session) sessionThreadLocal.get();
		sessionThreadLocal.set(null);
		if (session != null) {
			session.close();
		}
	}

	/**
	 * 设置Hibernate参数
	 * 
	 * @throws Exception
	 */
	public static void setHibernateProperties(Configuration configuration) throws Exception {
		/* 设置配置文件中参数到hibernate */
		final Properties extraProperties = new Properties();
		extraProperties.setProperty("hibernate.dialect",PropertiesUtil.getConfigParameter("DBInfo","dialect_pingtai"));
		extraProperties.setProperty("hibernate.connection.driver_class", PropertiesUtil.getConfigParameter("DBInfo","driver_class_pingtai"));
		extraProperties.setProperty("hibernate.connection.url", PropertiesUtil.getConfigParameter("DBInfo","url_pingtai"));
		extraProperties.setProperty("hibernate.connection.username", PropertiesUtil.getConfigParameter("DBInfo","username_pingtai"));
		//String passwordString=new String(StringEncryptUtil.aesDecrypt(StringEncryptUtil.parseHexStr2Byte(PropertiesUtil.getConfigParameter("password_wolf"))));
		extraProperties.setProperty("hibernate.connection.password", PropertiesUtil.getConfigParameter("DBInfo","password_pingtai"));
		extraProperties.setProperty("hibernate.show_sql", PropertiesUtil.getConfigParameter("DBInfo","show_sql"));
		extraProperties.setProperty("hibernate.format_sql", PropertiesUtil.getConfigParameter("DBInfo","format_sql"));
		configuration.addProperties(extraProperties);
		configuration.configure("hibernate-pingtai.cfg.xml"); // 路径可以改变
		//System.out.println(extraProperties.toString());
		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
				.applySettings(configuration.getProperties()).build();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	          
	}

}
 注意添加映射文件
  1.      config.addResource("demo/domain/User.hbm.xml");// 加载映射文件  
  2. // config.addClass(demo.domain.User.class); 另外一种加载方式 
    

你可能感兴趣的:(hibernate)