SSH整合中hibernate的配置方式

本文介绍了Hibernate的两种配置方法,一种是使用hibernate.cfg.xml文件并结合.hbm.xml映射文件,另一种是在applicationContext.xml中直接指定数据库连接及类映射文件的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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文件一样
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值