Hibernate使用C3P0配置

一、导jar包:Spring、C3P0、MySQL的jar包

二、配置hibernate.cfg.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- 该类是hibernate-c3p0-5.2.6.Final中的-->
     <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
        <!-- 设置最小连接数 -->
        <property name="hibernate.c3p0.min_size">5</property>
        <!-- 设置最大连接数 -->
        <property name="hibernate.c3p0.max_size">15</property>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=GBK</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
     <mapping resource="com/itnba/maya/entities/Family.hbm.xml"/>
        <mapping resource="com/itnba/maya/entities/Info.hbm.xml"/>
        <mapping resource="com/itnba/maya/entities/Work.hbm.xml"/>
        <mapping resource="com/itnba/maya/entities/Nation.hbm.xml"/>
        <mapping resource="com/itnba/maya/entities/Title.hbm.xml"/>     
    </session-factory> 

</hibernate-configuration>
复制代码

三、写代码运行

复制代码
package com.itnba.maya.test;

import java.util.Calendar;
import java.util.List;

import org.hibernate.Session;

import com.itnba.maya.entities.Info;

public class TestHibernate {

    public static void main(String[] args) {
        
        long start = Calendar.getInstance().getTimeInMillis();
        for(int i=0;i<1000;i++){
            Session session = HibernateUtil.getSession();            
            List<Info> list = session.createQuery("from Info").getResultList();
            for(Info data:list){
                System.out.println(data);
            }
            HibernateUtil.closeSession();
        }
        long end = Calendar.getInstance().getTimeInMillis();
        System.out.println(end-start);
    }
}
复制代码

 

 

对于hibernate来说,用与不用连接池的使用效率几乎没什么差异

posted @   囧雪诺  阅读(1504)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示