第一种:根据hibernate.hbm.xml配置sessionFactory
1.配置hibernate.hbm.xml
例如:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-config>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.driver</property>
<property name="connection.url">jdbc:localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">xxx</property>
<property name="show_sql">true</property>
<mapping resource=""/>
session-factory>
</hibernate-config>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.hbm.xml"></property>
<property name="mappingResources">
<list>
<value>org/ssh/domain/user.hbm.xml</value>
</list>
</property>
</bean>
2.applicationContext.xml配置
例如:
<bean id="sessionFactory" class="[color=red]org.springframework.orm.hibernate4.LocalSessionFactoryBea[/color]n">
<property name="configLocation" value="classpath:hibernate.hbm.xml">
</bean>
如此这样既可,注意红色部分使用的类
第二种:根据hibernate实体类的映射文件来配置sessionFactory
1.配置数据源,spring jdbc或者其他都可
2.配置实体类映射文件(或者使用注解)
3.配置sessionFactory
例如:
<bean id="sessionFacotry" class="[color=red]org.springframework.orm.hibernate4.LocalSessionFactoryBea[/color]n"(如果使用注解请使用o[color=red]rg.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean[/color])
>
<property name="dataSource" ref="dataSource"/>
<property name="mappingResource">
<list><value>a/b/user.hbm.xml</value></list>
</property>
(如果使用注解那么可以把mappingResource换成如下:
<property name="packagesToScan" value="a/b/"/>
)
</bean>
1.配置hibernate.hbm.xml
例如:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-config>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.driver</property>
<property name="connection.url">jdbc:localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">xxx</property>
<property name="show_sql">true</property>
<mapping resource=""/>
session-factory>
</hibernate-config>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.hbm.xml"></property>
<property name="mappingResources">
<list>
<value>org/ssh/domain/user.hbm.xml</value>
</list>
</property>
</bean>
2.applicationContext.xml配置
例如:
<bean id="sessionFactory" class="[color=red]org.springframework.orm.hibernate4.LocalSessionFactoryBea[/color]n">
<property name="configLocation" value="classpath:hibernate.hbm.xml">
</bean>
如此这样既可,注意红色部分使用的类
第二种:根据hibernate实体类的映射文件来配置sessionFactory
1.配置数据源,spring jdbc或者其他都可
2.配置实体类映射文件(或者使用注解)
3.配置sessionFactory
例如:
<bean id="sessionFacotry" class="[color=red]org.springframework.orm.hibernate4.LocalSessionFactoryBea[/color]n"(如果使用注解请使用o[color=red]rg.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean[/color])
>
<property name="dataSource" ref="dataSource"/>
<property name="mappingResource">
<list><value>a/b/user.hbm.xml</value></list>
</property>
(如果使用注解那么可以把mappingResource换成如下:
<property name="packagesToScan" value="a/b/"/>
)
</bean>