码迷,mamicode.com
首页 > 编程语言 > 详细

Spring c3p0连接池通过Hibernate配置

时间:2017-04-04 18:47:23      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:package   test   html   spring   hbm   nbsp   实现   dia   span   

  首先进行Hibernate配置,详见http://www.cnblogs.com/claricre/p/6509931.html

  然后调用技术分享这三个包。

配置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>
        <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
        <property name="hibernate.c3p0.min_size">1</property>
        <property name="hibernate.c3p0.max_size">5</property>
        <property name="hibernate.c3p0.acquire_increment">30</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <mapping resource="com/model/Nation.hbm.xml" />
    </session-factory>
</hibernate-configuration>

实现类

package com.test;

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

import org.hibernate.Session;

import com.model.Nation;

public class Test {

    public static void main(String[] args) {
        
        long start = Calendar.getInstance().getTimeInMillis();
        
        Session session = HibernateUtil.getSession();
        //Nation data = session.load(Nation.class, "n001");
        List<Nation> list = session.createQuery("from Nation").getResultList();
        for(Nation data :list){
            System.out.println(data);
        }
        HibernateUtil.closeSession();
        long end = Calendar.getInstance().getTimeInMillis();
        System.out.println(end-start);

    }

}

对于hibernate来说,用不用连接池的效率几乎相同。

Spring c3p0连接池通过Hibernate配置

标签:package   test   html   spring   hbm   nbsp   实现   dia   span   

原文地址:http://www.cnblogs.com/claricre/p/6665649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!