Spring的sessionFactory配置详解

一句话,Spring对Hibernate的整合,是在applicationContext.xml中配置sessionFactory来实现的,其中sessionFactory中要装配dataSource。即需要配置两个主要的Bean:sessionFactory和dataSource。其中dataSource的配置在http://blog.csdn.net/dreamrealised/article/details/9127811中已经详细介绍过了。下面详细介绍sessionFactory这个Bean的配置。

sessionfactory的Bean的配置如下

<bean id="sessionFactory"
	class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
	<!-- 配置hibernate属性 -->
	<property name="hibernateProperties">
		<value> 
			hibernate.dialect=${hibernate.dialect}
			hibernate.query.substitutions=true 1,false 0,toLowercase=LOWER
			hibernate.show_sql=true
			hibernate.hbm2ddl.auto=none
		</value>
	</property>
	<!-- 配置持久化类 -->
	<property name="annotatedClasses">
		<list>
			<value>nju.software.xkxt.data.dataobject.UserDO</value>
		</list>
	</property>
</bean>


1.Bean的id为sessionFactory,对应的类为AnnotationSessionFactory,即采用注解的形式实现hibernate。

2.hibernateProperties,配置hibernate的属性

1)hibernate.dialect,配置Hibernate方言,可以让Hibernate使用某些特定的数据库平台的特性,具体的dialect大全请参见http://blog.csdn.net/dreamrealised/article/details/9123193

2)hibernate.query.substitutions,查询语言替换,Hibernate查询中的一些短语替换为SQL短语。取值true=1,false=0,toLowercase=LOWER表示在生成SQL语句时,将true替换成1,false替换成0,toLowercase替换成LOWER

3)hibernate.show_sql=true,将执行得到SQL语句都输出到控制台,便于程序员观察SQL的执行。

4)hibernate.hbm2ddl.auto=none,表示不会根据hibernate配置对数据库自动执行任何操作。其他的属性值如下:

create:表示sessionFactory启动的时候先drop掉表,再create,数据库的表数据会丢失。
create-drop:也表示创建,只不过在sessionFactory关闭前执行一下drop。
update:sessionFactory启动的时候会去检查schema是否一致,如果不一致会做scheme更新,是最常用的属性。根据 hibernate持久化类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行hibernate持久化类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行
validate:启动时验证现有数据库表schema与你配置的hibernate是否一致,如果不一致就抛出异常,并不做更新

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值