Category: JAVA Spring Hibernate 2013-01-27 20:471851 people readComments (0)CollectionReportSpring Configuration for Hibernate sessionfactory
Prior to the development of a MMS publishing system with SPRING2+HIBERNATE3+STRUTS2, due to the first use of this architecture, resulting in applicationcontext.xml configuration is very lengthy, and often because of changing a small configuration item (example: Database IP, User name, password, etc.) to modify this file, which is not conducive to the maintenance of the project, in case of careless cause other changes, will cause the normal project caused a bug
In fact, the project I finally made a separation, Divide the applicationcontext.xml into several paragraphs, but I think in fact, for the database configuration, it is possible to load the Hibernate.cfg.xml configuration file to configure the project Sessionfactory, so this new project I decided to use this way
Here are 2 ways spring loads Sessionfactory
1. Configure the DataSource by configuring the Sessionfactory
Applicationcontext.xml
<!--database Configuration--
<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= "/>"
<property name= "Maxidle" value= "5"/>
<property name= "maxwait" value= "/>"
<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>
Use the Mappingdirectorylocations property to specify HBM files under a directory ("classpath*:" Point to Web-inf /classes directory)
<property name= "mappingdirectorylocations";
<list>
<value>
classpath:com/tukechina/mms/pojos
</value>
</list>
</property>
Supplement: Use Mappingresources property to write one hbm file ("classpath*:" to web-inf/classes directory)
<property name= "Mappingresources" >
<list>
<value>classpath*:/test/domain/MyBean.hbm.xml</value>
<value>classpath*:/test/domain/BasicBean.hbm.xml</value>
</list>
</property>
</bean>
2. Configure Sessionfactory by loading Hibernate.cfg.xml
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.sourceforge.net/hibernate-configuration-3.0.dtd" >
<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>
<!--maximum number of connections--
<property name= "Hibernate.c3p0.max_size" >20</property>
< minimum number of connections!----
<property name= "Hibernate.c3p0.min_size" >5</property>
<!--Get the connection timeout, if more than this time, will throw an exception, in milliseconds--and
<property name= "Hibernate.c3p0.timeout" >120</property>
<!--the largest number of PreparedStatement--
<property name= "Hibernate.c3p0.max_statements" >100</property>
<!--Check the connection pool for idle connections every 120 seconds, in seconds--
<property name= "Hibernate.c3p0.idle_test_period" >120</property>
<!--when the connection pool is out of use, c3p0 a new number of connections--
<property name= "Hibernate.c3p0.acquire_increment" >2</property>
<!--Verify that the connection is available every time-
<property name= "Hibernate.c3p0.validate" >true</property>
<mapping resource= "Com/shangx/pojos/user.hbm.xml"/>
</session-factory>
For the second configuration scenario, the data found is very small, most of the first, in fact, there is a better configuration
3. Separating the configuration of the database by configuring the Jdbc.properties file
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>
<!--database Configuration--
<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>
< minimum number of connections!----
<property name= "Minpoolsize" >
<value>5</value>
</property>
<!--the number of connections that can be increased after reaching the maximum number of connections
<property name= "Acquireincrement" >
<value>2</value>
</property>
<!--maximum number of connections--
<property name= "Maxpoolsize" >
<value>20</value>
</property>
<!--Max idle time Seconds--
<property name= "MaxIdleTime" >
<value>600</value>
</property>
<!--the largest number of PreparedStatement--
<property name= "maxstatements" value= "></property>"
<!--idle connection test period (seconds)--
<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>
Several methods of spring configuring Hibernate's Sessionfactory