好久沒接觸hibernate了,今天做畢設忽然要連接配接資料庫,想用hibernate來做持久化。就開始了搭建:
搭建環境:eclipse-luna-4.4.2 + jdk7
本人小菜一個,一開始在我的eclipse是沒有hibernate的插件的,在百度上說在這個位置下載下傳jboss插件:Eclipse, Help-->Eclipse Marketplace--》JBoss-Tools,但一打開就出現了當機。是以不推薦大家用這樣的方式。
然後在部落格(http://blog.sina.com.cn/s/blog_aaa756740101fub7.html)上看到要到這個位置上去下載下傳:http://jaist.dl.sourceforge.net/sourceforge/jboss/HibernateTools-3.2.4.Beta1-R200810311334.zip
,個人電腦windows10連的wifi,啟動内部的ie發現根本下載下傳不動這個14M的東西,後換成firefox。直接秒傳
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiZpdmL5Znbl9CX0xWdhZWZk9CX09Wbl9lcvRXakVGa49CXy9GdpRWZoh3LcRXZu5ibkN3Yuc2bsJmLjlGdhR3cvw1LcpDc0RHaiojIsJye.gif)
。
接下來進入正題:
1.将剛下載下傳的插件解壓:出現兩個檔案夾:plugins和features。将這兩個檔案夾直接放在eclipse的安裝位置。
2.下載下傳hibernate的jar包。我這裡用到的版本是:hibernate-distribution-3.6.10.Final。解壓後放在将以下jar放在 webinfo-lib下,add-to-buildpath:
解壓後主目錄下 hibernate3.jar
解壓後lib\required\* 目錄下antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar javassist-3.12.0.GA.jar jta-1.1.jar slf4j-api-1.6.1.jar
最後還有lib\jpa 下的 hibernate-jpa-2.0-api-1.0.1.Final.jar 本人這裡就因為沒有加這個jar,報了 Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
3.添加mysql驅動jar:本人這裡用到的是:mysql-connector-java-5.1.34.jar 到 webinfo-lib下,add-to-buildpath
4.環境基本get,接下來放大招了:
點選項目——》右鍵——》new ——》other——》輸入hibernate——》選擇
——》next——》填寫mysql驅動配置。finished。生成hibernate.cfg.xml
點選項目——》右鍵——》new ——》other——》輸入hibernate——》選擇
——》next ——》finished。生成User.hbm.xml。(hbm.xml前面的名字是參照你的bean類的)
環境基本搭成。
5.接下來配置xml檔案:
以我的bean類User為例:
User.java
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String password;
private int sex;
private int age;
Constructor;
getter and setter;
toString;
}
是以相對應的在hibernate.cfg.xml裡的session-factory裡面添加:
<mapping resource="com/cyan/entity/User.hbm.xml"></mapping>
這裡注意:resource裡的路徑是“/”而不是“.”
User.hbm.xml則為:
<hibernate-mapping package="com.cyan.entity">
<class name="User" table="user">
<id name="name" column="name"></id>
<property name="password" column="password"></property>
<property name="sex" column="sex"></property>
<property name="age" column="age"></property>
</class>
</hibernate-mapping>
這裡必須設定id,否則會報錯。
6.寫一個HibernateSessionFactory:(這是鄙人抄教科書的)
public class HibernateSessionFactory {
private static String CONGIG_FILE_LOCATION = "hibernate.cfg.xml";
private static final ThreadLocal<Session> THREAD_LOCAL = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static SessionFactory sessionFactory;
static{
configuration.configure(CONGIG_FILE_LOCATION);
sessionFactory = configuration.buildSessionFactory();
}
private HibernateSessionFactory(){
}
public static Session getSession(){
Session session = THREAD_LOCAL.get();
if(session == null || session.isOpen()){
if(sessionFactory == null){
rebuildSessionFactory();
}
}
session = (sessionFactory != null)?sessionFactory.openSession():null;
return session;
}
public static void rebuildSessionFactory(){
try{
configuration.configure(CONGIG_FILE_LOCATION);
sessionFactory = configuration.buildSessionFactory();
}catch(Exception e){
System.out.println("SessionFactory build error");
e.printStackTrace();
}
}
public static void main(String[] args) {
getSession();
}
public static void closeSession(){
Session session = THREAD_LOCAL.get();
THREAD_LOCAL.set(null);
if(session != null){
session.close();
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static void setConfigFile(String CONGIG_FILE_LOCATION){
HibernateSessionFactory.CONGIG_FILE_LOCATION = CONGIG_FILE_LOCATION;
sessionFactory = null;
}
public static Configuration getConfiguration(){
return configuration;
}
}
7.測試:
public static void main(String[] args) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
User user = new User("xiaoqing","xiaoqing",1,13);
session.save(user);
tx.commit();
System.out.println("get");
HibernateSessionFactory.closeSession();
}
}
8.get.
由于今天過晚,隻簡單記錄下自己的操縱步驟。後續再完善。