使用spring配置hibernate3--风间小筑

J2EE

使用spring配置hibernate3
风间残月 发表于 2005-9-9 16:30:00

applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  <property name="mappingResources">
   <list>
    <value>GameCity.hbm.xml</value>
   </list>
  </property>
  
  <property name="entityCacheStrategies">
   <props>
    <prop key="test.GameCity">read-only</prop>
   </props>
  </property>
  
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
   
    <prop key="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</prop>
    <prop key="hibernate.proxool.pool_alias">xjgame</prop>
    <prop key="hibernate.proxool.xml">proxool.xml</prop>
    
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
    
    <prop key="hibernate.cache.use_query_cache">true</prop>
   </props>
  </property>
 </bean>
   
</beans>

proxool.xml

 <?xml version="1.0" encoding="ISO-8859-1"?>
<something-else-entirely>
  <proxool>
    <alias>xjgame</alias>
    <driver-url>jdbc:mysql://localhost/xjgame</driver-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <driver-properties>
      <property name="user" value="root"/>
      <property name="password" value="1234"/>
    </driver-properties>
    <maximum-connection-count>10</maximum-connection-count>
    <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
    <statistics>30s</statistics>
    <statistics-log-level>FATAL</statistics-log-level>
  </proxool>
</something-else-entirely>

Test.java

 /**
 *
 */
package test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

/**
 * @author shenshan
 * @version 1.0
 *
 */
public class Test
{

 /**
  * @param args
  */
 public static void main( String[ ] args )
 {
  ClassPathResource res = new ClassPathResource( "applicationContext.xml" );
  XmlBeanFactory factory = new XmlBeanFactory( res );
  SessionFactory sf = ( SessionFactory ) factory
    .getBean( "mySessionFactory" );
  Session sess = sf.openSession( );

  List rs = sess.createQuery( "FROM test.GameCity" ).list( );

  for ( int i = 0; i < rs.size( ); i++ )
  {
   System.out.println( rs.get( i ) );
  }

  sess.close( );

 }

}