本文主要是介绍spring配置hibernate的sessionFactory,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、Spring通过dbcp配置dataSource来配置sessionFactory
jdbc.properties
# oracle JDBC
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
jdbc.username=admin
jdbc.password=admin
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.format_sql=false
jdbc.validationQuery=SELECT sysdate from dual
jdbc.testOnBorrow=true
applicationContext.xml
<!-- jdbc.properties文件读取 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="true">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
</list>
</property>
</bean><!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 设置JDBC驱动名称 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!-- 设置JDBC连接URL -->
<property name="url" value="${jdbc.url}" />
<!-- 设置数据库用户名 -->
<property name="username" value="${jdbc.username}" />
<!-- 设置数据库密码 -->
<property name="password" value="${jdbc.password}" /><!-- 设置连接池初始值 -->
<property name="initialSize" value="3" />
<!-- 设置连接池最大值 -->
<property name="maxActive" value="60" />
<!-- 设置连接池最小空闲值 -->
<property name="minIdle" value="2" />
<!-- 设置连接池最大空闲值 -->
<property name="maxIdle" value="5" />
<!-- 设置验证SQL -->
<property name="validationQuery" value="${jdbc.validationQuery}" />
<!-- 设置取出连接时验证其是否有效 -->
<property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
</bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 配置Hibernate拦截器,自动填充数据的插入、更新时间 -->
<property name="entityInterceptor" ref="entityInterceptor" />
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<!-- 设置数据库方言 -->
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!--
设置自动创建|更新|验证数据库表结构 <prop key="hibernate.hbm2ddl.auto">update</prop>
-->
<!-- 输出SQL语句到控制台 -->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<!-- 格式化输出到控制台的SQL语句 -->
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<!-- 是否开启二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<!-- 配置二级缓存产品 -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
<!-- 是否开启查询缓存 -->
<prop key="hibernate.cache.use_query_cache">false</prop>
<!-- 数据库批量查询数 -->
<!-- <prop key="hibernate.jdbc.fetch_size">50</prop> -->
<!-- 数据库批量更新数 -->
<prop key="hibernate.jdbc.batch_size">30</prop>
</props>
</property><!--扫描该包下的所有实体类-->
<property name="packagesToScan">
<list>
<value>com/eshop/entity</value>
</list>
</property>
</bean>
2、SpringMvc通过hikari配置dataSource来配置sessionFactory
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 数据源配置 -->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maximumPoolSize" value="100" />
<property name="minimumIdle" value="${druid.minIdle}" />
<property name="connectionTestQuery" value="${jdbc.validationQuery}" />
<property name="dataSourceProperties">
<props>
<prop key="cachePrepStmts">true</prop>
<prop key="prepStmtCacheSize">250</prop>
<prop key="prepStmtCacheSqlLimit">2048</prop>
<prop key="useServerPrepStmts">true</prop>
</props>
</property>
</bean><bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="entityInterceptor" ref="entityInterceptor" />
<property name="packagesToScan">
<list>
<value>org.eshop.*.*.entity</value>
<value>org.eshop.*.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- 设置数据库方言 -->
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!-- 输出SQL语句到控制台 -->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<!-- 格式化输出到控制台的SQL语句 -->
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<!-- 是否开启二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<!-- 配置二级缓存产品 -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
<!-- 是否开启查询缓存 -->
<prop key="hibernate.cache.use_query_cache">false</prop>
<!-- 数据库批量查询数 -->
<prop key="hibernate.jdbc.fetch_size">20</prop>
<!-- 数据库批量更新数 -->
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
这篇关于spring配置hibernate的sessionFactory的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!