hibernate , proxool

Hibernate 3.3.2版本
proxool 0.9.1
1.所需jar
hibernate
required文件夹下所有jar
hibernate3.jar
antlr-2.7.6.jar
dom4j.jar
javassist.jar
jta.jar
commons-collections.jar
slf4j-api.jar(最好跟slf4j-simple.jar版本一致)
slf4j-simple.jar(需另外下)

proxool
proxool.jar
proxool-cglib.jar
commons-loggin.jar(需另下)




2.编写config
src/config/hibernate/hibernate.cfg.xml
src/config/proxool.xml

proxool.xml(名字随便)
位置这里放在类路径下
<?xml version="1.0" encoding="UTF-8"?>
<proxool>
    <alias>stone-proxool</alias>
    <driver-url>jdbc:mysql://localhost:3306/stone</driver-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <driver-properties>
      <property name="user" value="aguu"/>
      <property name="password" value="nopass"/>
    </driver-properties>
    <maximum-connection-count>10</maximum-connection-count>
    <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
</proxool>

hibernate.cfc.xml(也在类路径下)
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<!-- hibernate自带连接池
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>		
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/stone</property>

<property name="hibernate.connection.username">aguu</property>		
<property name="hibernate.connection.password">nopass</property>
		<property name="show_sql">true</property>
		<mapping resource="config/hibernate/Sort.hbm.xml"/>

</session-factory>
	 -->

<!--proxool连接池配置-->
	 <session-factory>

	<!-- proxool连接池加载的类-->
  	<property name="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>

  	<!--连接池的别名,即配置连接池时起的别名-->
  	<property name="hibernate.proxool.pool_alias">stone-proxool</property>

  	<!--连接池文件的地址-->
  	<property name="hibernate.proxool.xml">config/proxool.xml</property>
  	<!--是否将运行期生成的SQL输出到日志以供调试-->
  <property name="show_sql">true</property>
  
  <mapping resource="config/hibernate/Sort.hbm.xml" />
</session-factory>
</hibernate-configuration>


3.hibernate 工具类
hibernateUtil.java
package com.aguu.core;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    private static final ThreadLocal session=new ThreadLocal();
    
    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure("config/hibernate/hibernate.cfg.xml").buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    //取得当前线程的session
    public static Session currentSession(){
    	Session s = (Session) session.get();
    	if(s==null){
    		s=sessionFactory.openSession();
    		session.set(s);
    	}
    	return s;
    }
    
    public static void closeSession(){
    	Session s = (Session)session.get();
    	if(s!=null){
    		s.close();
    		session.set(null);
    	}
    }
    
}

你可能感兴趣的:(java,Hibernate,xml,mysql,jdbc)