`

SSH整合中hibernate的配置方式

 
阅读更多
hibernate的配置方式有两种
第一种为:编写hibernate.cfg.xml,并且为每个映射类编写.hbm.xml文件
,hibernate.cfg.xml文件的内容如下:
<hibernate-configuration>
	<session-factory>
		<property name="myeclipse.connection.profile">JDBC for MySQL</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/demo</property>
		<property name="connection.username">root</property>
		<property name="connection.password">admin</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<mapping resource="com/hibernate/bean/User.hbm.xml" />
	</session-factory>
</hibernate-configuration>

如果有新的类映射文件(即.hbm.xml文件)要加入进来,只需只需添加一个<mapping/>属性即可,配置如上面的User.hbm.xml
其中,hibernate.cfg.xml文件位置的指定由HibernateSessionFactory类来指定,其中这个类的代码如下:
package com.hibernate.util;

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

@SuppressWarnings("unchecked")
public class HibernateSessionFactory {
	
	private static String CONFIG_FILE_LOCATION ="/hibernate.cfg.xml";
	private static final ThreadLocal threadLocal = new ThreadLocal();
	private static final Configuration cfg = new Configuration();
	private static SessionFactory sessionFactory;
	
	public static Session currentSession() throws HibernateException{
		
		Session session = (Session)threadLocal.get();
		if(session == null){
			if(sessionFactory == null){
				try{
					cfg.configure(CONFIG_FILE_LOCATION);
					sessionFactory = cfg.buildSessionFactory();
				}catch(Exception e){
					System.out.println("=====Error Creating SessionFactory======");
					e.printStackTrace();
				}
			}
			session = sessionFactory.openSession();
			threadLocal.set(session);
		}
	return session;	
	}
	
	public static void closeSession()throws HibernateException{
		
		Session session = (Session)threadLocal.get();
		threadLocal.set(null);
		if(session!=null){
			session.close();
		}
	}
}


第二种为:直接在applicationContext.xml文件中指定数据库连接和每个类映射文件的具体位置,代码如下:
	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName">
			<value>org.gjt.mm.mysql.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/demo</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>admin</value>
		</property>
	</bean>


	<!-- 配置hibernate -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
		<property name="mappingResources">
			<list>
				<value>com/ssh/beans/User.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
		    </prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>

如果有新的类映射文件要加入进来,只需要在<list></list>标签中加上一个<value></value>标签,并且在中间填入类映射文件的具体位置,像User.hbm.xml文件一样
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics