1、注解
Hibernate的注解是什么? 简单的说,本来放在hbm.xml文件里的映射信息,现在不用配置文件做了,改由注解来完成
2、删除表
因为前面的某些教程修改了product_的表结构,所以这里先把product_表删除一下。
drop table product_;
3、Product.hbm.xml
这是Product.hbm.xml,用于提供类与表的映射,以及属性与表字段的映射关系
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.how2java.pojo">
<class name="Product" table="product_">
<id name="id" column="id">
<generator class="native">
</generator>
</id>
<property name="name" />
<property name="price" />
</class>
</hibernate-mapping>
4、注解方式
现在直接在类上使用注解,来达到相同的配置效果。
关于这些注解的解释,在接下来的知识点讲解:类和属性注解
使用注解之后的Product.java
package com.how2java.pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "product_")
public class Product {
int id;
String name;
float price;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "price")
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
- 原Product.java
package com.how2java.pojo;
public class Product {
int id;
String name;
float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
5、hibernate.cfg.xml 配置文件改动
为了能够让hibernate支持这种注解的方式,还需要做一个改动:
把hibernate.cfg.xml配置文件中的
<mapping resource="com/how2java/pojo/Product.hbm.xml" />
注释掉,采用:
<mapping class="com.how2java.pojo.Product" />
这种写法,其他所有代码都不用改变,这样就可以一样实现hibernate的CRUD操作
<?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://localhost:3306/test?characterEncoding=UTF-8</property>
<property name="connection.characterEncoding">utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- <mapping resource="com/how2java/pojo/Product.hbm.xml" /> -->
<mapping class="com.how2java.pojo.Product" />
</session-factory>
</hibernate-configuration>