Hibernate4 所需jar
导入/lib/required文件夹下的所有jar包
配置连接池需要导入/lib/optional/c3p0文件夹中的所有jar包
数据库使用的是MYSQL,因此还需要mysql的连接驱动:mysql-connector-java-5.1.22-bin.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> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate4</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!--配置c3p0连接池-->
<!-- Enable c3p0 connection pooling,beacause hibernate pooling is not prod-ready. Apparently connection.provider_class is needed in hibernate 3+ --> <!-- <property name="connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property> --> <property name="hibernate.c3p0.min_size">1</property> <property name="hibernate.c3p0.max_size">100</property> <property name="hibernate.c3p0.idle_test_period">30</property>
<!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <!--<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> --> <!-- 配置二级缓存 --> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <!-- hibernate3的二级缓存配置 --> <!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> --> <!-- 开启查询缓存 --> <property name="hibernate.cache.use_query_cache">true</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping class="com.test.pojo.User" /> </session-factory> </hibernate-configuration>
注意:hibernate4和hibernate3配置不一样,hibernate4是而hibernate3的配置是<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>