hibernate映射的两种方式

hibernate之映射的两种方式

将实体类映射到数据库中的表的方式有两种,包括通过xml文件映射以及通过注解映射。个人认为通过注解映射更加直观,出错率低一些吧。下面是简单的步骤介绍以及代码演示。

一、xml方式进行映射

1.在hibernate.cfg.xml为映射进行配置(需要放在src目录下,否则需要重新设置映射的路径)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/students?useSSL=false&serverTimezone=UTC&</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- <mapping resource="Student.hbm.xml"/> -->
<mapping resource="Accommodation.hbm.xml"/>
</session-factory>
</hibernate-configuration>

2.添加映射的文件Student.hbm.xml(在上一个文件的同级目录下)

<?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">
<hibernate-mapping>
<class name="com.ffy.hibernate.Student" table="student">
<id name="id" column="id">
<generator class="assigned"></generator> </id>
<property name="sid" column="sid"></property>
//这是将数据库的字段与java的属性对应
<property name="firstName" column="firstname"></property>
<property name="lastName" column="lastname"></property>
<property name="gender" column="gender"></property>
<property name="phone" column="phone"></property>
<property name="email" column="email"></property>
<property name="dateOfBirth" column="date_of_birth"></property>
<property name="studentStatusID" column="student_status_id"></property>
<property name="height" column="height"></property>
</class>
</hibernate-mapping>

这样,就完成了利用xml的方式数据库的实体与java实体类进行映射了。
二、注解方式进行映射
1.在配置文件中指定要进行映射的类

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/students?useSSL=false&serverTimezone=UTC&</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="com.ffy.hibernate.Student"/> <mapping class="com.ffy.hibernate.StudentStatus"/>
<mapping class="com.ffy.hibernate.Apartment"/>
</session-factory>
</hibernate-configuration>

2.在需要映射的类中加上注解

@Entity
@Table(name="student")
public class Student {
@Id
@Column(name="id")
private int id;
@Column(name="sid")
private int sid;
@Column(name="firstname")
private String firstName;
@Column(name="lastname")
private String lastName;
@Column(name="gender")
private String gender;
@Column(name="phone")
private String phone;
@Column(name="email")
private String email;
@Column(name="date_of_birth")
private String dateOfBirth;
@Column(name="height")
private double height=160;
@Column(name="student_status_id")
private int studentStatusID;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastname() {
return lastName;
}
public void setLastname(String lastName) {
this.lastName = lastName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
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;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getStudentStatusID() {
return studentStatusID;
}
public void setStudentStatusID(int studentStatusID) {
this.studentStatusID = studentStatusID;
}
public String toString(){
return this.id+":"+this.lastName+this.firstName+this.gender+":"+this.phone+":"+this.email;
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值