hibernate框架的使用需要配置 hibernate.cfg.xml ,下面为该文件的常用配置信息。(需修改自己的数据库连接,用户名,密码,映射关系)
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!--下面是数据库的基本连接信息,对一个应用来说,设置一个session-factory节点就够了,除非我们中间使用了多个数据库-->
<session-factory>
<!-- 配置数据库的驱动程序,Hibernate在连接数据库时,需要用到数据库的驱动程序 -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<!-- 设置数据库的连接url -->
<property name="hibernate.connection.url">jdbc:MySQL://localhost:3306/homework?serverTimezone=UTC</property>
<!-- 连接数据库的用户名 -->
<property name="hibernate.connection.username">root </property>
<!-- 连接数据库的密码 -->
<property name="hibernate.connection.password">123456 </property>
<!-- Hibernate使用的数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property>
<!-- 关于c3p0配置 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size">5</property>
<!-- 连接的最大空闲时间,时间为秒 -->
<property name="hibernate.c3p0.timeout">120</property>
<!-- 最大的statement数 -->
<property name="hibernate.c3p0.max_statements">100</property>
<!-- 连接池用完后新建的连接数量 -->
<property name="hibernate.c3p0.acquire_increment">1</property>
<!-- 是否在后台显示Hibernate用到的SQL语句,开发时设置为true,
便于差错,程序运行时可以在Eclipse的控制台显示Hibernate的执行Sql语句。 -->
<property name="hibernate.show_sql">true </property>
<!-- 连接数据库时数据的传输字符集编码方式,最好设置为ut8 -->
<property name="connection.characterEncoding">UTF-8 </property>
<!-- 是否对数据库进行数据结构的更新 -->
<property name="hbm2ddl.auto">validate</property>
<!-- 映射关系 -->
<mapping resource="com/test/model/name.hbm.xml"/>
</session-factory>
</hibernate-configuration>
需用到的jar包:
官网下载的包解压后:
hibernate-release-5.3.6.Final --> lib -->required 内所有包;
hibernate-release-5.3.6.Final --> lib -->optional -->c3p0 内所有包;
将上述包导入eclipse你的工程的 web-INF --> lib 下