zoukankan      html  css  js  c++  java
  • 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来说,用与不用连接池的使用效率几乎没什么差异

  • 相关阅读:
    wf(三)
    WF(二)
    WF4.0入门(一)
    枚举
    函数和立即函数
    对象字面量
    Break和continue语句
    对象
    循环语句
    条件分支语句(SWICH语句)
  • 原文地址:https://www.cnblogs.com/jonsnow/p/6646094.html
Copyright © 2011-2022 走看看