1、导包:

(1)required文件夹下的jar包都需要导入:

(2)mysql驱动包:

(3)jpa目录下的jar包:

(4)日志jar包:

因为hibernate在使用的时候会有日志信息的输出,而hibernate本身没有日志输出的功能,因此要导入jar包。
2、创建实体类
package pers.zhb.domain;
public class Student {
private String studentno;
private String sname;
private String sex;
private String birthday;
private String classno;
private float point;
private String phone;
private String email;
@Override
public String toString() {
return "Student{" +
"studentno=‘" + studentno + ‘\‘‘ +
", sname=‘" + sname + ‘\‘‘ +
", sex=‘" + sex + ‘\‘‘ +
", birthday=‘" + birthday + ‘\‘‘ +
", classno=‘" + classno + ‘\‘‘ +
", point=" + point +
", phone=‘" + phone + ‘\‘‘ +
", email=‘" + email + ‘\‘‘ +
‘}‘;
}
public String getStudentno() {
return studentno;
}
public void setStudentno(String studentno) {
this.studentno = studentno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getClassno() {
return classno;
}
public void setClassno(String classno) {
this.classno = classno;
}
public float getPoint() {
return point;
}
public void setPoint(float point) {
this.point = point;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
在创建类的时候一定要注意:要创建一个类似于表的主键的属性,用于唯一标识一个实体。
3、通过配置文件实现实体类和数据库表的对应关系(表对应,实体属性与表的字段)
(1)引入dtd约束(https://www.cnblogs.com/zhai1997/p/11439664.html):

在上面的文件中复制如下约束:
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
创建xml文件,在实体类对应的包里面创建,文件名:hbm.xml(不是固定的)。
(2)创建实体类与表的对应关系:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="pers.zhb.domain.Student" table="student"> <id name="studentno" column="studentno"> <generator class="native"></generator> </id> <property name="birthday" column="birthday"></property> <property name="classno" column="classno"></property> <property name="email" column="email"></property> <property name="phone" column="phone"></property> <property name="sex" column="sex"></property> <property name="sname" column="sname"></property> <property name="point" column="point"></property> </class> </hibernate-mapping>
(1)配置类和表对应:
<class name="pers.zhb.domain.Student" table="student">
其中类为全路径,表为表的名字。
(2)实体类中的保持唯一的属性和表的主键保持对应:
<id name="studentno" column="studentno"> <generator class="native"></generator> </id>
设置主键为自动增长,name为实体类属性的名字,column为表的字段名称。
(3)设置其他属性实体类属性与表的字段的对应关系:
<property name="birthday" column="birthday"></property> <property name="classno" column="classno"></property> <property name="email" column="email"></property> <property name="phone" column="phone"></property> <property name="sex" column="sex"></property> <property name="sname" column="sname"></property> <property name="point" column="point"></property>
4、核心配置文件:
(1)位置(src目录下)和名称(hibernate.cfg.xml)固定。
(2)引入约束:

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
(3)hibernate在执行过程中,只会加载核心配置文件,其它文件不会被加载。
原文:https://www.cnblogs.com/zhai1997/p/11778545.html