在resin3.0中配置hibernate2.1.2連mysql_PHP教程

來源:互聯網
上載者:User
在resin3.0中配置hibernate2.1.2連mysql

在resin3.0中配置hibernate2.1.2連mysql

Author : hamal

約定:

resin3 代表resin3.0的安裝根目錄

hibernate2 代表hibernate2.1.2的安裝根目錄



1. 在resin3下建立我們的web應用根,如resin3mydomain目錄(mydomain的目錄名可以隨意取,之後在設定檔中配置即可)

2. 在resin3mydomain目錄下分別建立resin3mydomainWEB-INFclasses目錄和resin3mydomainWEB-INFlib目錄。
這兩個目錄對應本web應用程式內容相關的類裝載器搜尋路徑(對於jar來說是resin3mydomainWEB-INFlib,對於class檔案來說是resin3mydomainWEB-INFclasses)。我們把這兩個路徑分別稱為應用庫類路徑(用於存放和本應用相關的jar類庫)和上下文類路徑(用於存放本應用的class檔案和xml設定檔)。
另外還有一個路徑是resin3lib目錄,我們稱該路徑為全域庫類路徑(存放於resin3伺服器上,供該伺服器上所有web應用共用使用的相關jar類庫)。

3. 本樣本使用的是mysql資料庫,所以我們將mysql的jdbc驅動jar包(mm.mysql-2.0.4-bin.jar)放入resin3lib目錄中,之後該驅動將能被所有web應用所用。

4. 將hibernate2 hibernate2.jar檔案拷貝到resin3mydomainWEB-INFlib 目錄下,然後拷貝hibernate2lib 目錄下必須的jar檔案也拷貝到resin3mydomainWEB-INFlib 目錄下。如果你不是很清楚哪些包是你所需要的,可以參看hibernate2libREADME.txt檔案,或者,再簡單一點,我們將hibernate2lib 目錄下的所有jar檔案都拷貝到resin3mydomainWEB-INFlib 目錄下。

5. 現在我們開始配置resin的jdbc資料庫連接池。修改resin3confresin.conf檔案

a) 搜尋webapps,將webapps替換為我們自訂的應用目錄mydomain。

b) 尋找元素,取消該元素的注釋狀態,該檔案的注釋採用的是html風格的注釋方式。

c) 將元素修改為如下形式



jdbc/mysql



jdbc:mysql://192.162.125.3:3306/mysql

root

12345678



8

20

30s



如此resin的jdbc串連池配置完成。

6. 將hibernate2src目錄下的hibernate.properties、log4j.properties、oscache.properties檔案拷貝到resin3 mydomainWEB-INFclasses目錄下。

7. 由於我們使用的是mysql資料庫,所以修改hibernate.properties檔案中關於mysql部分的配置,並注釋掉原預設的HypersonicSQL配置。注釋配置就是在語句前加 # 符號。如:

#hibernate.dialect net.sf.hibernate.dialect.HSQLDialect



下面是一個典型的mysql配置:

hibernate.dialect net.sf.hibernate.dialect.MySQLDialect

hibernate.connection.driver_class org.gjt.mm.mysql.Driver

hibernate.connection.driver_class com.mysql.jdbc.Driver

hibernate.connection.url jdbc:mysql://192.162.125.3:3306/mydb

hibernate.connection.username root

hibernate.connection.password 12345678



我們需要修改的就是下面3行:

url是指jdbc串連描述符,格式為jdbc:mysql://資料庫IP:連接埠號碼/資料庫名

username是指用於登陸該資料庫的使用者名稱

password該使用者密碼



8. 將hibernate和resin的資料庫連接池綁定。在目錄裡建立一個hibernate.cfg.xml檔案,檔案內容如下




PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">





java:comp/env/jdbc/mysql

true

net.sf.hibernate.dialect.MySQLDialect









該元素告訴hibernate使用resin中定義的jndi來串連資料庫, 子項目用於描述每一個映射資料庫表的設定檔路徑,

其聲明了User.hbm.xml是一個Hibernate XML對應檔,對應持久化類User。這個檔案包含了把POJO類映射到資料庫表(或多個資料庫表)的中繼資料。我們稍後就回來看這個檔案。讓我們先編寫這個POJO類,再看聲明它的映射中繼資料。

9. 在mysql中建立user表該表的格式如下:

User_id Password Nick_name E_mail
1 6666 hamal hamal@sohu.com
2 6666 vampire vampire@sina.com
3 6666 ande ande@yahoo.com

在resin3 mydomainWEB-INFclasses目錄下分別建立3個java檔案:Test.java、HibernateUtil.java、User.java。



HibernateUtil.java原始碼如下:該類是一個輔助類,用於獲得一個靜態SessionFactory,SessionFactory負責一個資料庫,也只對應一個XML設定檔(hibernate.cfg.xml)。



import net.sf.hibernate.*;

import net.sf.hibernate.cfg.*;



public class HibernateUtil {



private static final SessionFactory sessionFactory;



static {

try {

sessionFactory = new Configuration().configure().buildSessionFactory();

} catch (HibernateException ex) {

throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);

}

}



public static final ThreadLocal session = new ThreadLocal();



public static Session currentSession() throws HibernateException {

Session s = (Session) session.get();

// Open a new Session, if this Thread has none yet

if (s == null) {

s = sessionFactory.openSession();

session.set(s);

}

return s;

}



public static void closeSession() throws HibernateException {

Session s = (Session) session.get();

session.set(null);

if (s != null)

s.close();

}

}



User.java 原始碼如下:Hibernate讓普通的Java對象(Plain Old Java Objects ,就是POJOs,有時候也稱作Plain Ordinary Java Objects)變成持久化類。一個POJO很像JavaBean,屬性通過getter和setter方法訪問,對外隱藏了內部實現的細節。



public class User {



private Integer id;

private String nick;

private String password ;

private String email;



public User() {

}



public Integer getId() {

return id;

}



public void setId(Integer id) {

this.id = id;

}



public String getNick() {

return nick;

}



public void setNick(String nick) {

this.nick = nick;

}



public String getPassword() {

return password;

}



public void setPassword(String password) {

this.password = password;

}



public String getEmail() {

return email;

}



public void setEmail(String email) {

this.email = email;

}



}



Test.java 原始碼如下:



import javax.naming.*;

import net.sf.hibernate.*;

import java.util.*;



public class Test{

void Test(){



}

public static void insert(){

try{

Session hSession = HibernateUtil.currentSession();

Transaction tx= hSession.beginTransaction();



User newp = new User();

Integer id = new Integer("4");

newp.setId(id);

newp.setNick("love");

newp.setPassword("123");

newp.setEmail("test@sohu.com");



hSession.save(newp);

tx.commit();

HibernateUtil.closeSession();

}catch(Exception e){

e.printStackTrace();

}

}

}





10. 編寫Hibernate XML對應檔

在resin3 mydomainWEB-INFclasses目錄下建立User.hbm.xml檔案,檔案內容如下:






PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

















































簡單說明:

元素中的name屬性代表的是User類的全路徑名(即包名+類名的形式),table屬性代表的是該User類映射的資料庫表名。

元素代表該表的主鍵,name屬性代表在User類中對應的類屬性名稱。

元素代表該id屬性對應的資料庫user表中的user_id欄位。





11. 介面測試:

我們在resin3mydomain目錄下建立一個test.jsp檔案如下:



<%@ page contentType="text/html; charset=gb2312" %>





This is a test!





This is a test!

<%Test.insert();%>









12. 測試

好了,現在所有的準備工作都已經做完了,開始測試看看。

我們啟動rensin伺服器,開機檔案為resin3binhttpd.exe,雙擊該檔案即可。

在測試前我們看到的mysql中user表的內容如下。

User_id Password Nick_name E_mail
1 6666 hamal hamal@sohu.com
2 6666 vampire vampire@sina.com
3 6666 ande ande@yahoo.com

現在我們開啟IE,在地址欄中輸入http://localhost:8080/test.jsp

當介面上正常顯示出This is a test!之後,我們再查看資料庫內容如下:

User_id Password Nick_name E_mail
1 6666 hamal hamal@sohu.com
2 6666 vampire vampire@sina.com
3 6666 ande ande@yahoo.com
4 123 love test@sohu.com


恭喜,你已經完成了本次樣本!

http://www.bkjia.com/PHPjc/630945.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630945.htmlTechArticle在resin3.0中配置hibernate2.1.2連mysql 在resin3.0中配置hibernate2.1.2連mysql Author : hamal 約定: resin3 代表resin3.0的安裝根目錄 hibernate2 代表hibernate2...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.