hibernate 在tomcat7.X 下配置mysql資料來源

來源:互聯網
上載者:User

標籤:

先說一點題外話,LZ近期學習java web。

今天剛看到hibernate,發如今hibernate配置資料來源時網上的資料都太久遠了,一般以tomcat 5 版本號碼下的配置居多。而tomcat 7下的配置略有變化,新手找資料困難,可能會略受打擊,故整理資料與大家共用。也可作備忘之用。若有不當之處。還請指教!



1.加入資料庫驅動包mysql-connector-java-5.1.13-bin.jar加入到tomcat檔案夾下的lib包中。


注意:網上差點兒全部版本號碼都說須要加入commons-dbcp-1.4.jar,commons-pool-1.5.4.jar和commons-collections.jar。

可是tomcat 7 已經不用加入這些jar檔案了。由於tomcat 7的lib包以下已經整合了這些庫。

官方整合文檔上有:These libraries are located in a single JAR at $CATALINA_HOME/lib/tomcat-dbcp.jar. 

大概意思就是:這些庫檔案都位於一個單獨的JAR檔案裡,該檔案的路徑是/lib/tomcat-dbcp.jar。

2.改動Tomcat_Home/conf/server.xml,</host>標籤之前增加:

   <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/javatest"/>

3.在context.xml中增加引用


<ResourceLink name="jdbc/TestDB" global="jdbc/TestDB"    
      type="javax.sql.DataSource"/> 

4.在你的project項目的web.xml中增加<resource-ref>。

    <resource-ref>  
        <description>tomcat datasource test,one mysql datasource</description>  
        <res-ref-name>jdbc/TestDB</res-ref-name>  
        <res-type>javax.sql.DataSource</res-type>  
        <res-auth>Container</res-auth>  
    </resource-ref>

5.在hibernate.cfg.xml檔案裡設定資料庫串連,這個有非常多教程,此處便不再詳述。

另外,注意資料來源僅僅有在web項目中才幹夠使用。

再次出來寫個簡單的servlet來輸出hibernate 的session對象:

代碼例如以下:

package com.amaker.servlet;


import java.io.IOException;

import java.io.PrintWriter;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.hibernate.Session;


import com.amaker.util.HibernateUtil;


@SuppressWarnings("serial")

public class testServletextends HttpServlet {


/**

* Constructor of the object.

*/

public testServlet() {

super();

}


/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy();// Just puts "destroy" string in log

// Put your code here

}


/**

* The doGet method of the servlet.<br>

*

* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

HibernateUtil util = new HibernateUtil();

//HibernateUtil 類用來返回一個Session

對象。非常easy,相信大家能夠搞定。 Session session = util.getSession();

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");

out.println("  <BODY>");

out.println(session);

out.println("  </BODY>");

out.println("</HTML>");

out.flush();

out.close();

}


public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


doGet(request, response);

}


/**

* Initialization of the servlet.<br>

*

* @throws ServletException if an error occurs

*/

public void init()throws ServletException {

// Put your code here

}


}

訪問結果。返回一個session對象。





hibernate 在tomcat7.X 下配置mysql資料來源

聯繫我們

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