Hibernate 開發流程java
1、導入相應的包
一、hibernate安裝文件夾中的lib->required中的包
二、導入log4j
三、導入數據庫驅動mysql
2、建立hibernate的配置文件
在src的目錄下建立相應的hibernate.cfg.xml在這個文件中加入相應的數據庫基本信息的配置
在hibernate.cfg.xml的配置文件中首先須要配置相應的數據庫基本鏈接sql
<hibernate-configuration> <session-factory> <!-- hibernate的方言,用來肯定鏈接的數據庫 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 數據庫的鏈接類 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 數據庫的鏈接字符串和用戶名密碼 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/itat_hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <!-- 在使用hibernate時會顯示相應的SQL --> <property name="show_sql">true</property> <!-- 會自動完成類到數據表的轉換 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 加入實體類的映射文件 --> </session-factory> </hibernate-configuration>
3、建立實體類數據庫
package org.test.model;
import java.util.Date; public class User { private int id; private String username; private String password; private String nickname; private Date born; public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public Date getBorn() { return born; } public void setBorn(Date born) { this.born = born; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", nickname=" + nickname + ", born=" + born + "]"; } }
4、在實體類的包中建立相應的hbm文件,用來指定實體類和數據庫映射關係安全
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.test.model"> <class name="User" table="t_user"> <id name="id"> <generator class="native"/> </id> <property name="username"/> <property name="password"/> <property name="nickname"/> <property name="born" type="timestamp"/> </class> </hibernate-mapping>
5、將配置文件添加到hibernate的cfg的配置文件中session
<hibernate-configuration> <session-factory> <!-- hibernate的方言,用來肯定鏈接的數據庫 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 數據庫的鏈接類 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 數據庫的鏈接字符串和用戶名密碼 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/itat_hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <!-- 在使用hibernate時會顯示相應的SQL --> <property name="show_sql">true</property> <!-- 會自動完成類到數據表的轉換 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 加入實體類的映射文件 --> <mapping resource="org/test/model/User.hbm.xml"/> </session-factory> </hibernate-configuration>
6、建立SessionFactory,SessionFactory是線程安全,因此整個SessionFactory應該基於單例的模式來建立
Configuration cfg = new Configuration().configure();
//cfg.buildSessionFactory();//在hibernate3中都是使用該種方法建立,可是在4中被禁用了
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory factory = cfg.buildSessionFactory(serviceRegistry);app
7、建立session
Session session = factory.openSession();ide
8、經過session來進行各類操做
如下代碼完成了對象的添加操做
try {
session = factory.openSession();
//開啓事務
session.beginTransaction();
User u = new User();
u.setNickname("張三");
u.setPassword("123");
u.setUsername("zhangsan");
u.setBorn(new Date());
session.save(u);
//提交事務
session.getTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
if(session!=null) session.getTransaction().rollback();
} finally {
if(session!=null) session.close();
}ui
完整Test類this
package org.test.test; import java.util.Date; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.Test; import org.test.model.User; public class TestFirst { @Test public void test01() { Configuration cfg = new Configuration().configure(); //cfg.buildSessionFactory();//在hibernate3中都是使用該種方法建立,可是在4中被禁用了 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory factory = cfg.buildSessionFactory(serviceRegistry); Session session = null; try { session = factory.openSession(); //開啓事務 session.beginTransaction(); User u = new User(); u.setNickname("張三"); u.setPassword("123"); u.setUsername("zhangsan"); u.setBorn(new Date()); session.save(u); //提交事務 session.getTransaction().commit(); } catch (HibernateException e) { e.printStackTrace(); if(session!=null) session.getTransaction().rollback(); } finally { if(session!=null) session.close(); } } }
note:感謝董浩老師,講解很清楚