Hibernate配置步骤

时间:2023-03-08 18:57:01

1、创建WEB项目

2、从http://www.hibernate.org/下载hibernate-release-4.3.11.Final.zip,并解压。

3、将hibernate必须的包加入lib

4、打开hibernate-release-4.3.11.Final\lib\required文件夹,导入jar文件:

5、打开hibernate-release-4.3.11.Final\lib\optional\ehcache文件夹,导入jar文件:

6、打开hibernate-release-4.3.11.Final\lib\optional\c3p0文件夹,导入jar文件:

7、配置hibernate.cfg.xml。打开hibernate-release-4.3.11.Final\project\etc文件夹,选择hibernate.cfg.xml文件并复制到src下。

8、打开hibernate.cfg.xml文件,并设置数据库连接

  1 <!DOCTYPE hibernate-configuration PUBLIC
2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
4
5 <hibernate-configuration>
6
7 <session-factory>
8 <!-- 定义方言,即告诉框架你用的是什么数据库 -->
9 <property name="dialect">
10 org.hibernate.dialect.MySQLDialect
11 </property>
12
13 <!-- 定义连接路径 -->
14 <property name="connection.url">
15 jdbc:mysql://localhost:3306/hibernate?characterEncoding=UTF-8
16 </property>
17 <!-- 定义连接名与密码 -->
18 <property name="connection.username">root</property>
19 <property name="connection.password"></property>
20 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
21
22 <!-- 指定使用c3p0连接池 -->
23 <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
24 <!-- 指定连接池最大连接数 -->
25 <property name="hibernate.c3p0.max_size">20</property>
26 <!-- 指定连接池最小连接数 -->
27 <property name="hibernate.c3p0.min_size">1</property>
28 <!-- 指定连接池里连接超时时长 -->
29 <property name="hibernate.c3p0.timeout">5000</property>
30
31 <!-- 指定连接池里最大缓存多少个PreparedStatement对象 -->
32 <property name="hibernate.c3p0.max_statements">100</property>
33 <property name="hibernate.c3p0.idle_test_period">3000</property>
34 <property name="hibernate.c3p0.acquire_increment">2</property>
35
36 <!--以格式良好的方式显示SQL语句-->
37 <property name="format_sql">true</property>
38 <!--显示SQL语句-->
39 <property name="show_sql">true</property>
40
41 <!-- 添加hbm文件配置 -->
42 <mapping resource="com/lovo/xmls/UserInfoT.hbm.xml" />
43      <mapping class="com.lovo.beans.UserInfoT"/>
44     </session-factory>
45
46 </hibernate-configuration>

hibernate.cfg.xml

9、创建数据库表,并封装实体Bean与XXX.hbm.xml文件,例如:UserInfoT.java:

 public class UserInfoT implements java.io.Serializable {

     // Fields

     /**
*
*/
private static final long serialVersionUID = -8360690722407472061L;
private Long id;
private String userName;
private String password;
private Integer age; // Constructors /** default constructor */
public UserInfoT() {
} /** full constructor */
public UserInfoT(String userName, String password, Integer age) {
this.userName = userName;
this.password = password;
this.age = age;
} // Property accessors public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getUserName() {
return this.userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return this.password;
} public void setPassword(String password) {
this.password = password;
} public Integer getAge() {
return this.age;
} public void setAge(Integer age) {
this.age = age;
} }

UserInfoT

然后打开 hibernate-release-4.3.11.Final\project\hibernate-core\src\main\resources\org\hibernate文件夹下的hibernate-mapping-3.0.dtd文件。,复制head部分。

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.lovo.beans.UserInfoT" table="user_info_t" catalog="hibernate">
<id name="id" type="java.lang.Long">
<column name="id" />
<!-- 指定ID的生成方式 -->
<generator class="identity" />
</id>
<property name="userName" type="java.lang.String">
<column name="user_name" length="32">
<comment>用户名</comment>
</column>
</property>
<property name="password" type="java.lang.String">
<column name="password" length="32">
<comment>密码</comment>
</column>
</property>
<property name="age" type="java.lang.Integer">
<column name="age">
<comment>年龄</comment>
</column>
</property>
</class>
</hibernate-mapping>

UserInfoT.hbm.xml

OK,配置完成!