首先进行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来说,用不用连接池的效率几乎相同。
时间: 2024-10-07 05:30:30