Spring配置SessionFactory
1.不用dataSource引入hibernate.cfg.xml
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- </bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean>
2.丢掉hibernate.cfg.xml
1>使用注解:
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="<SPAN style="BACKGROUND-COLOR: #ff0000">annotatedClasses</SPAN>">
- <list>
- <value>com.bjsxt.model.User</value>
- <value>com.bjsxt.model.Log</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>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.bjsxt.model.User</value>
<value>com.bjsxt.model.Log</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>
2>不使用注解
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <!-- 依赖注入数据源,注入正是上面定义的dataSource -->
- <property name="dataSource" ref="dataSource" />
- <property name="<SPAN style="BACKGROUND-COLOR: #ff0000">mappingResources</SPAN>"><!-- mappingResouces属性用来列出全部映射文件 -->
- <list><!-- 以下用来列出Hibernate映射文件 -->
- <value>jsf/web/entity/User.hbm.xml</value>
- </list>
- </property>
- <!-- 定义Hibernate的SessionFactory的属性 -->
- <property name="hibernateProperties">
- <props>
- <!-- 指定数据库方言 -->
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQLInnoDBDialect</prop>
- <!-- 是否根据需要每次自动创建数据库 -->
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- <!-- 显示Hibernate持久化操作所生成的SQL -->
- <prop key="hibernate.show_sql">true</prop>
- <!-- 将SQL脚本进行格式化后再输出 -->
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 依赖注入数据源,注入正是上面定义的dataSource -->
<property name="dataSource" ref="dataSource" />
<property name="mappingResources"><!-- mappingResouces属性用来列出全部映射文件 -->
<list><!-- 以下用来列出Hibernate映射文件 -->
<value>jsf/web/entity/User.hbm.xml</value>
</list>
</property>
<!-- 定义Hibernate的SessionFactory的属性 -->
<property name="hibernateProperties">
<props>
<!-- 指定数据库方言 -->
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect</prop>
<!-- 是否根据需要每次自动创建数据库 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<prop key="hibernate.show_sql">true</prop>
<!-- 将SQL脚本进行格式化后再输出 -->
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
3>使用packagesToScan
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="<SPAN style="BACKGROUND-COLOR: #ff0000">packagesToScan</SPAN>">
- <list>
- <value>com.bjsxt.model</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>