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="dataSource" 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"/></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop></props></property><property name="mappingDirectoryLocations"><list><value>classpath:com/tukechina/mms/pojos </value></list></property></bean>
<bean id="sessionFactory"value="classpath:hibernate.cfg.xml"></property></bean>
<?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>
Mysqljdbc.driverClassName=com.mysql.jdbc.DriverMysqljdbc.url=jdbc:mysql://localhost/goodshoolMysqljdbc.username=rootMysqljdbc.password=1234# second cache statisticshibernate.generate_statistics=true# Property that determines the Hibernate dialect# (only applied with "applicationContext-hibernate.xml")hibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate.show_sql=true
<bean id="propertyConfigurer"value="classpath:jdbc.properties" /></bean><!-- 数据库配置 --><bean id="mysqlDataSource" value="100"></property><!-- 闲置的连接测试周期 (秒) --><property name="idleConnectionTestPeriod"><value>120</value></property></bean><bean id="sessionFactory"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>