【Hibernate学习笔记-3】在Spring下整合Hibernate时, 关于sessionFactory的类型的说明
2015-05-28 14:00
405 查看
摘要[/b]在Spring下整合Hibernate时,关于sessionFactory的配置方式主要有两种,分别为注解配置方式,和xml配置方式,下面将对这两种配置方式进行介绍。1. sessionFactory和数据库对应,有多少个数据库,就需要配置多少个sessionFactory;
2. session相当于数据库连接,进行数据库的CRUD操作时,需要开启session,用完需要关闭session;3. 配置sessionFactory,主要要配置如下三个方面:
3.1. 数据源配置; 3.2. Hibernate属性配置; 3.3. 映射文件配置; (3.4 一般还需要进行事务配置)关于sessionFactory的类型[/b]
Hibernate映射文件可以有两种方式:1. @注释 和 2.xml配置文件;使用两种不同方式配置时,sessionFactory的类型不同,具体如下:@注释方式:org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean XML文件方式:
org.springframework.orm.hibernate3.LocalSessionFactoryBean 见程序:(applicationContext-xxx.xml)XML方式
2. session相当于数据库连接,进行数据库的CRUD操作时,需要开启session,用完需要关闭session;3. 配置sessionFactory,主要要配置如下三个方面:
3.1. 数据源配置; 3.2. Hibernate属性配置; 3.3. 映射文件配置; (3.4 一般还需要进行事务配置)关于sessionFactory的类型[/b]
Hibernate映射文件可以有两种方式:1. @注释 和 2.xml配置文件;使用两种不同方式配置时,sessionFactory的类型不同,具体如下:@注释方式:org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean XML文件方式:
org.springframework.orm.hibernate3.LocalSessionFactoryBean 见程序:(applicationContext-xxx.xml)XML方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">[/code]<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop><prop key="connection.pool_size">100</prop><prop key="hibernate.jdbc.batch_size">100</prop><prop key="hibernate.show_sql">false</prop><prop key="hibernate.default_schema">${hibernate.default_schema}</prop><省略...></props></property><property name="mappingDirectoryLocations"><list><value>classpath:/config/hibernate-config-sql2000</value></list></property></bean></beans>
[/code]
@注解方式<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">[/code]<bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><!-- 此处ORACLE与SQL数据库应该区别 --><prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop><!-- <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> --><prop key="connection.pool_size">100</prop><prop key="hibernate.jdbc.batch_size">100</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.default_schema">${hibernate.default_schema}</prop><省略...></props></property><!--<property name="mappingResources"><list><value>hibernate-config/DistrictArea.hbm.xml</value></list></property>--><property name="mappingDirectoryLocations"><list><value>classpath:/config/hibernate-config</value></list></property></bean><bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean></beans>
[/code]
来自为知笔记(Wiz)
相关文章推荐
- SSH学习笔记之关于struts-spring-hibernate整合配置问
- 【hibernate+Spring学习】S2SH整合笔记--中文乱码
- 【hibernate+Spring学习】S2SH整合笔记--中文乱码
- 学习笔记-----------------struts2 hibernate3 spring2.5整合
- SSH整合学习笔记之spring与hibernate简单整合.doc
- SSH整合学习笔记之spring与hibernate整合(二)--C3P0
- 【SSH学习笔记】整合spring3-hibernate的小项目(一)
- 【SSH学习笔记】整合spring3-hibernate的小项目(二)
- Spring4学习笔记-Spring4整合Hibernate4
- Spring学习笔记之整合hibernate
- MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-5]
- 19.笔记JAVA Spring框架学习————Spring整合Hibernate
- 20.笔记JAVA Spring框架学习————Spring整合Hibernate
- [原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword 与 解析,.环境搭建,hibernate.cfg.xml文件及参数说明,持久化类,对象-关系映射文件.hbm.xml,Hibernate API (Configuration 类,SessionFactory 接口,Session 接口,Transaction(事务))
- Spring-Hibernate整合 学习笔记
- Spring+hibernate整合学习笔记-2