个人学习记录:
1:Hibernate 开发包引入:
至官网下载所需版本的开发包,将加压后的jar包引入项目中:src/main/webapp/WEB-INF/lib/
2:Hibernate tool安装
查看Elipse版本,Help >About Eclipse,然后到JBoss Tools - Home 官网找到对应Eclipse版本的安装链接:例如:Index of /jbosstools/photon/stable/updates
回到Eclipse中,Help>Install New Software
或者直接Help>Eclipse Marketplace 搜索 "Hibernate Tool"
3:新建hibernate配置文件:
选中项目源代码,通过向导新增Hibernate Configuation File, 配置文件名称为:hibernate.cfg.xml
<?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>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.password">111111</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/sakila?serverTimezone=GMT</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">sakila</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
4:利用Hibernate tool 自动生成简单Java类(POJO)
单击工具栏:Hibernate tool 下拉框,点击"Hibernate Code Generation Configurations"
切换到Exports选项卡中:
生成的java类和xml映射文件示例:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-access="property" default-cascade="none" default-lazy="true">
<class name="entity.Actor" table="actor">
<id name="actorId" type="java.lang.Short">
<column name="actor_id"/>
<generator class="identity"/>
</id>
<property generated="never" lazy="false" name="firstName" optimistic-lock="true" type="string" unique="false">
<column length="45" name="first_name" not-null="true"/>
</property>
<property generated="never" lazy="false" name="lastName" optimistic-lock="true" type="string" unique="false">
<column length="45" name="last_name" not-null="true"/>
</property>
<property generated="never" lazy="false" name="lastUpdate" optimistic-lock="true" type="timestamp" unique="false">
<column length="19" name="last_update" not-null="true"/>
</property>
</class>
</hibernate-mapping>
MySql中的示例数据库:sakila中的Actor表结构如下:
'actor_id', 'smallint unsigned', 'NO', 'PRI', NULL, 'auto_increment'
'first_name', 'varchar(45)', 'NO', '', NULL, ''
'last_name', 'varchar(45)', 'NO', 'MUL', NULL, ''
'last_update', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', 'DEFAULT_GENERATED on update CURRENT_TIMESTAMP'
package entity;
import java.util.Date;
/**
* Actor generated by hbm2java
*/
public class Actor implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Short actorId;
private String firstName;
private String lastName;
private Date lastUpdate;
public Actor(String firstName,String lastName) {
this.firstName=firstName;
this.lastName=lastName;
this.lastUpdate=new Date();
}
public Actor() {
}
public Actor(String firstName, String lastName, Date lastUpdate) {
this.firstName = firstName;
this.lastName = lastName;
this.lastUpdate = lastUpdate;
}
public Short getActorId() {
return this.actorId;
}
public void setActorId(Short actorId) {
this.actorId = actorId;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getLastUpdate() {
return this.lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
}
5:利用Hibernate持久化java类对象:
在hibernate.cfg.xml中添加映射文件:
<session-factory>
...
<mapping resource="entity/Actor.hbm.xml"/>
<mapping resource="entity/Address.hbm.xml"/>
</session-factory>
新建一个java类,引入junit测试:
双击AddActor方法,右键RunAs >JUnit Test
package example;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.jupiter.api.Test;
import entity.Actor;
public class HibernateTest {
@Test
void AddActor() {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
try {
Actor actor = new Actor("JIM", "Green");
session.save(actor);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
sessionFactory.close();
}
}
}