|
spring配置hibernate的sessionFactory 之前用spring2+hibernate3+struts2开发了一个彩信发布系统,由于第一次使用此架构,造成applicationContext.xml中的配置非常冗长,而且经常因为更改一个小配置项(例:数据库ip、用户名、密码等)将此文件作修改,这及不利于项目维护,万一粗心造成其他地方变动,会对本来正常的项目造成bug 其实那个项目我最后做了分隔,将applicationContext.xml分隔成好几段,但是我觉得其实对于数据库方面的配置,完全可以通过加载hibernate.cfg.xml配置文件来配置项目的sessionFactory,所以这个新项目我决定使用此方式 这里介绍一下spring加载sessionFactory的这2种方式 1、通过配置dataSource来配置sessionFactory applicationContext.xml <!-- 数据库配置 --> <bean id="mydataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://192.168.0.2:3306/tuke_mmsmsys"></property> <property name="username" value="admin"></property> <property name="password" value="richard"></property> <!-- Connection Pooling Info --> <property name="maxActive" value="20" /> <property name="maxIdle" value="5" /> <property name="maxWait" value="5000" /> <property name="validationQuery" value="select count(0) from admin" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="mydataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> 使用 mappingDirectoryLocations 属性可以指定某目录下的 hbm 文件(“classpath*:”指向 WEB-INF/classes 目录) <property name="mappingDirectoryLocations"> <list> <value> classpath:com/tukechina/mms/pojos </value> </list> </property> 补充:使用 mappingResources 属性要一个一个写 hbm 文件(“classpath*:”指向 WEB-INF/classes 目录) <property name="mappingResources"> <list> <value>classpath*:/test/domain/MyBean.hbm.xml</value><value>classpath*:/test/domain/BasicBean.hbm.xml</value> </list> </property>
</bean> 2、通过加载hibernate.cfg.xml来配置sessionFactory
applicationContext.xml <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./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> 对于第二种配置方案,找到的资料很少,大多数采用第一种,其实还有一种较好的配置 3、通过配置jdbc.properties文件分离数据库的配置 jdbc.properties Mysqljdbc.driverClassName=com.mysql.jdbc.Driver Mysqljdbc.url=jdbc:mysql://localhost/goodshool Mysqljdbc.username=root Mysqljdbc.password=1234 # second cache statistics hibernate.generate_statistics=true # Property that determines the Hibernate dialect # (only applied with "applicationContext-hibernate.xml") hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true applicationContext.xml <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <!-- 数据库配置 --> <bean id="mysqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>${Mysqljdbc.driverClassName}</value> </property> <property name="jdbcUrl"> <value>${Mysqljdbc.url}</value> </property> <property name="user"> <value>${Mysqljdbc.username}</value> </property> <property name="password"> <value>${Mysqljdbc.password}</value> </property> <!-- 最小连接数 --> <property name="minPoolSize"> <value>5</value> </property> <!-- 达到最大连接数后可以增加的连接数 个 --> <property name="acquireIncrement"> <value>2</value> </property> <!-- 最大连接数 --> <property name="maxPoolSize"> <value>20</value> </property> <!-- 最大闲置时间 秒 --> <property name="maxIdleTime"> <value>600</value> </property> <!-- 最大的PreparedStatement的数量 --> <property name="maxStatements" value="100"></property> <!-- 闲置的连接测试周期 (秒) --> <property name="idleConnectionTestPeriod"> <value>120</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> --> <property name="dataSource"> <ref bean="mysqlDataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.generate_statistics"> ${hibernate.generate_statistics} </prop> <prop key="hibernate.dialect"> ${hibernate.dialect} </prop> <prop key="hibernate.show_sql"> ${hibernate.show_sql} </prop> </props> </property> <property name="mappingDirectoryLocations"> <list> <value> classpath:com/shangx/pojos </value> </list> </property> </bean> |
|
|