一個簡單的hibernate執行個體(之二)

來源:互聯網
上載者:User
上一篇我們編寫了用一個普通的java類來對我們的hibernate應用進行測試,現在我們繼續利用我們上次編寫的程式,
描述如何在web環境下對其進行使用:

一、在Tomcat的根目錄下的common/lib目錄下引入mysql的驅動及dbcp的支援jar。
二、在webapps目錄下建立hibernate目錄作為我們的web應用要目錄。在其中建立WEB-INF目錄,並在這個目錄下建立
classes與lib目錄,在lib中引入我們上一篇介紹的hibernate3.2使用者庫中的所有jar,並從我們在eclipse中建立的工
程的根目錄下的bin目錄中引入我們程式編譯後的class檔案到classes目錄中。
三、建立串連池
1、修改conf目錄下的server.xml在 段中加入

  <Resource name="jdbc/hibernate" auth="Container" type="javax.sql.DataSource"           maxActive="100" maxIdle="30" maxWait="10000"           username="root" password="" driverClassName="com.mysql.jdbc.Driver"           url="jdbc:mysql://127.0.0.1:3306/myproject?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf8"/> 


四、在conf/Catalina/localhost目錄下建立一個設定檔


hibernate.xml

  <Context   displayName="hibernate"   docBase="E:/Tomcat 5.5/webapps/hibernate"   path="/hibernate"   privileged="true">     <ResourceLink   name="jdbc/hibernate"   type="javax.sql.DataSource"   global="jdbc/hibernate"/>     </Context>  

五、在WEB-INF目錄下建立:

web.xml
  <resource-ref>         <description>DB</description>         <res-ref-name>jdbc/hibernate</res-ref-name>         <res-type>javax.sql.DataSource</res-type>         <res-auth>Container</res-auth>    </resource-ref>  

六、在/WEB-INF/classes上好中建立hibernate的設定檔:

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.datasource">java:comp/env/jdbc/hibernate</property>               <property name="show_sql">true</property>              <property name="dialect">org.hibernate.dialect.MySQLDialect</property>           <mapping resource="px/malijun/User.hbm.xml"/>          </session-factory>      </hibernate-configuration>  
所有準備工作做好了,我們現在又可以寫測試代碼了,不過這次寫的是jsp檔案:(我們寫的很簡陋)

insert.html
  <form action="insertok.jsp" method="post">   name:<input type=text name="name"><br>   password:<input type=text name="password"><br>   email:<input type=text name="email"><br>   <input type=submit value="submit"><br>   </form>  
insertok.jsp

   <%@ page import="java.sql.*,java.util.*,px.malijun.*" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>     "user" class="px.malijun.User" scope="page">         "user" property="*"/>       "userBusiness" class="px.malijun.UserBean" scope="page"/>              "Content-Type" content="text/html;charset=UTF-8" >               <%     try    {      userBusiness.addUser(user);    %>    }    catch(Exception e)    {    out.println("Error!"+e.getMessage());    e.printStackTrace();    }     %>            

ok,在web環境下如何對hibernate進行測試也完成了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.