现在的位置: 首页 > 综合 > 正文

Hibernate配置JNDI数据源

2018年05月21日 ⁄ 综合 ⁄ 共 1835字 ⁄ 字号 评论关闭
Hibernate使用Tmocat的连接池的配置过程:
1.在Tomcat的server.xml里加入
    <Context path="/ttt" docBase="ttt"
        debug="5" reloadable="true" crossContext="true">

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

     </Context>
2.把mysql的驱动程序导入的项目下的/WEB-INF/lib下,或直接导入到Tomcat的jar包文件里
3./WEB-INF下的web.xml修改成

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <resource-ref>
             <description>DB Connection</description>
             <res-ref-name>jdbc/TestDB</res-ref-name>
             <res-type>javax.sql.DataSource</res-type>
             <res-auth>Container</res-auth>
    </resource-ref>
   
     <welcome-file-list>
       <welcome-file>index.jsp</welcome-file>
     </welcome-file-list>
    </web-app>
4.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">

    <!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

   
    <session-factory>

            <property name="connection.datasource">java:comp/env/jdbc/TestDB</property>
            <property name="show_sql">true</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

            <!-- Mapping files -->
             <mapping resource="com/Student.hbm.xml" />

        </session-factory>

</hibernate-configuration>
5.其他的javabean类和session工厂可以使用自动生成

抱歉!评论已关闭.