1.不用dataSource引入hibernate.cfg.xml
Bean.xml代码
- <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>使用注解:
Java代码
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="<span style="#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>
2>不使用注解
Java代码
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <!-- 依赖注入数据源,注入正是上面定义的dataSource -->
- <property name="dataSource" ref="dataSource" />
- <property name="<span style="#ff0000;">mappingResources</span>"><!-- mappingResouces属性用来列出全部映射文件 -->
- <list><!-- 以下用来列出Hibernate映射文件 -->
- <value>classpath:hibernate/*.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
Java代码
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="<span style="#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>
Spring配置SessionFactory
时间: 2024-10-10 13:54:40