`

6 用Properties补充hibernate.cfg.xml配置

 
阅读更多

在一些特殊情况下,我们可能会把数据库连接信息保存在config.properties 文件中, 例如做一个install.jsp 来修改config.properties文件 ,实现对数据库信息的在线配置.这时 数据库连接信息保存在hibernate.cfg.xml就不方便.所以要单独保存到properties文件 中.

config.properties文件内容如下 :

#数据库IP
dbhost = localhost

#端口号
dbport=3306

#用户名
dbuser = root

#密码
dbpw = 1234

#数据库名
dbname = test

hibernate.cfg.xml 文件 无数据库配置信息, 如下:

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory >
		<property name="show_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		
		<!--   注释了, 不起作用
		//<property name="hibernate.connection.url">jdbc:mysql:///test</property>
		//<property name="hibernate.connection.username">root</property>
		//<property name="hibernate.connection.password">1234</property>
	    -->
	
	<mapping resource="dao/po/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

我们这样结合使用:

package dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Properties;

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

/**
 * 这是一个工具类, 快速取得session
 * 
 */
public class HibernateUtil
{
	static SessionFactory	sessionFactory	= null;

	static
	{
		try
		{

			final String path = URLDecoder.decode(Thread.currentThread().getContextClassLoader().getResource(
					"config.properties").getPath(), "UTF-8"); //config.properties保存的真实路径
			final Properties properties = new Properties();
			final InputStream fis = new FileInputStream(path); //config.properties 文件对象,里面有数据库的连接信息,
			properties.load(fis);
			fis.close(); //关流
			final String dbhost = properties.getProperty("dbhost"); //数据库IP(从config.properties读)
			final String dbport = properties.getProperty("dbport"); //端口(从config.properties读)
			final String dbname = properties.getProperty("dbname"); //数据库名(从config.properties读)
			final String dbuser = properties.getProperty("dbuser"); //用户名(从config.properties读)
			final String dbpw = properties.getProperty("dbpw"); //密码(从config.properties读)

			final Properties extraProperties = new Properties();
			extraProperties.setProperty("hibernate.connection.url", "jdbc:mysql://" + dbhost + ":" + dbport + "/"
					+ dbname + "?zeroDateTimeBehavior=convertToNull");
			extraProperties.setProperty("hibernate.connection.username", dbuser);
			extraProperties.setProperty("hibernate.connection.password", dbpw);

			final Configuration cfg = new Configuration();
			cfg.addProperties(extraProperties);
			cfg.configure("hibernate.cfg.xml"); //路径可以改变
			sessionFactory = cfg.buildSessionFactory();
		}
		catch (final UnsupportedEncodingException e)
		{
			//不支持字符编码。
			e.printStackTrace();
		}
		catch (final FileNotFoundException e)
		{
			//config.properties文件没找到
			e.printStackTrace();
		}
		catch (final HibernateException e)
		{
			//cfg.configure("hibernate.cfg.xml");时异常
			e.printStackTrace();
		}
		catch (final Exception e)
		{
			//创建SessionFactory 异常
			e.printStackTrace();
		}
	}

	public static SessionFactory getSessionFactory()
	{
		return sessionFactory;
	}

	/**
	 * 取得session
	 * 
	 * @return session
	 */
	public static Session getSeesion()
	{
		return sessionFactory.openSession();
	}
}
分享到:
评论

相关推荐

    ssh整合,不带hibernate.cfg.xml的方式

    继续在 LocalSessionFactoryBean 中配置,使用hibernateProperties属性继续来配置其他的属性,注意值是properties属性文件

    毕业设计+源码(项目管理系统)完整框架

    绝版作品!一切都只是曾经,神马都是浮云!...运行先改jdbc.properties和hibernate.cfg.xml里面的数据库配置!数据库最好事先存在。 再运行test包的testCreateDB-》testData(插入数据!)即可运行!

    Java框架SSH整合的所有配置文件

    内含有applicationContext.xml,c3p0-config.xml,c3p0-db.properties,hibernate.cfg.xml,log4j.properties,struts.xml,User.hbm.xml,pom.xml等等配置文件

    Hibernate实践例子程序

    1) Hibernate全局配置文件,hibernate.properties或者hibernate.cfg.xml.。一般使用XML文件。 2) 数据O/R mapping 配置文件,也就是数据库中每一条记录的详细说明,包括field, PrimaryKey等。*.hbm.xml,*一般用...

    毕业设计+源码(项目管理系统)界面超炫,完整框架

    运行先改jdbc.properties和hibernate.cfg.xml里面的数据库配置!数据库最好事先存在。 再运行test包的testCreateDB-》testData(插入数据!)即可运行! 资源珍贵,分必不低。请尊重原创血汗! 下载完请点评资源并留言...

    spring2.5 struts2.0 hibernate3.2.5 搭建的企业级开发基础模块

    Hibernate:spring.local.hibernate.cfg.xml Struts:struts.xml、struts.properties 配置很简单,用点心看就会了,多的不说了,好好享受咯 哦 忘记介绍了, 当中还有本人写的几个自定义标签:...

    Hibernate小例子

    正如其名,Configuration 类负责管理Hibernate 的配置信息。...这些属性在hibernate配置文件(hibernate.cfg.xml 或hibernate.properties)中加以设 定(参见前面“Hibernate配置”中的示例配置文件内容)。

    Hibernate使用技巧汇总

    你不必一定用hibernate.cfg.xml或hibernate.properties这两文件名, 你也不一定非得把配置文件放在Classes下, File file=new File("c:\\sample\\myhibernate.xml"); Configuration config=new ...

    ssh整合的常用配置文件,包括所有配置文件,需要的童鞋进行下载。

    ssh这个是常用的配置文件,里面包含hibernate.cfg.xml\struts.xml\beans.xml\log4j.properties\web.xml等等,是开发必备的配置文件

    SSH的jar包.rar

    读取并解析hibernate.cfg.xml配置文件 2.由hibernate.cfg.xml中的&lt;mapping resource="com/xx/User.hbm.xml"/&gt;读取并解析映射信息 3.通过config.buildSessionFactory();//创建SessionFactory 4.sessionFactory....

    hibernate-源代码-01

    另一种是XML 格式的配置文件, XML 配置文件的默认名称为 hibernate.cfg,xml。 上述两种格式的配置文件是等价的,具体使用哪个可以自由选择。 XML 格式的配置文件更易于修改,配置能力更强,当改变底层应用配置时不...

    最简单的Hibernate工程

    一个最简单的Hibernate工程,可通过hibernate.cfg.xml或者hibernate.properties加载数据源并对对象操作,下载后可直接导入eclipse运行

Global site tag (gtag.js) - Google Analytics