1. 包含hibernate庫
使用maven
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.1.Final</version>
</dependency>
不使用maven
hibernate官網:http://hibernate.org/orm/,將下載的jar包放到lib目錄下。
2. 配置hibernate
在class目錄下新建配置文件:hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置數據源 -->
<property name="connection.url">jdbc:mysql://localhost:3306/yourDatabaseName?characterEncoding=UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<!-- hibernate啓動時自動創建表:將update改爲create -->
<property name="hbm2ddl.auto">update</property>
<!-- 需要映射的類 -->
<mapping class="com.yourPackage.ClassA"/>
<mapping class="com.yourPackage.ClassB"/>
</session-factory>
</hibernate-configuration>
3. 使用hibernate
3.1 新建HibernateUtil類
public class HibernateUtil {
private static final SessionFactory sessionFactory;
private static Logger log;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
HibernateUtil類用於獲取sessionFactory,之後在代碼中就可以這樣獲取session:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
3.2 新建java類:User
package com.yourPackage;
// 註解@Entity和@Table表示這是一個實體類,映射到表:tb_user
@Entity
@Table(name = "tb_user")
public class User{
// Column表示將屬性映射到表的userName列。屬性名和列名相同時,可以省略註解
@Column(name="userName")
private String userName;
@Column(name="password")
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
使用註解的方式,需要在hibernate.cfg.xml中聲明映射類:
<hibernate-configuration>
<session-factory>
...
...
<!-- 需要映射的類 -->
<mapping class="com.yourPackage.User"/>
...
</session-factory>
</hibernate-configuration>
3.3 在MySQL中創建相應的表:tb_user
3.4 向表中新增一個用戶
User user = new User();
user.setUserName("jack");
user.setPassword("123456");
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try {
session.beginTransaction(); // 開啓事務
session.persist(user); // 保存對象
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
}