Spring 实践
hibernate配置
第一种、hibernate配置写在xml
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 数据源配置 -->
<property name="dataSource" ref="dataSource" />
<!-- hibernate拦截器 我这里实现分表 -->
<property name="entityInterceptor" ref="hibernateInterceptor"/>
<!-- hibernate一些配置 -->
<property name="hibernateProperties">
<props>
<!--支持位运算& 配置自定义方言 继承自org.hibernate.dialect.MySQLInnoDBDialect -->
<prop key="hibernate.dialect">com.xes.stone.common.expand.hibernate.ExpandMySQLInnoDBDialect</prop>
<!-- 是否根据需要每次自动创建数据库 -->
<!--<prop key="hibernate.hbm2ddl.auto">none</prop>-->
<!-- 显示Hibernate持久化操作所生成的SQL -->
<prop key="hibernate.show_sql">${hibernate.showsql}</prop>
<!-- 将SQL脚本进行格式化后再输出 -->
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache" >false</prop>
<prop key="hibernate.cache.use_query_cache" >false</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
</props>
</property>
<!-- hibernate xml文件路径 -->
<property name="mappingLocations">
<list>
<value>
classpath*:META-INF/complaint/core/hbm/*.hbm.xml
</value>
</list>
</property>
</bean>
第二种、hibernate配置写在hibernate.cfg.xml
springXml中配置
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="mysql">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/goodshool</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size">5</property>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="hibernate.c3p0.timeout">120</property>
<!-- 最大的PreparedStatement的数量 -->
<property name="hibernate.c3p0.max_statements">100</property>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="hibernate.c3p0.idle_test_period">120</property>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 每次都验证连接是否可用 -->
<property name="hibernate.c3p0.validate">true</property>
<mapping resource="com/shangx/pojos/User.hbm.xml" />
</session-factory>
</hibernate-configuration>