hibernate学习:hibernate.cfg.xml文件中与数据库的两种连接

来源:互联网 发布:淘宝上领的红包怎么用 编辑:程序博客网 时间:2024/05/17 08:53
 

hibernate学习:hibernate.cfg.xml文件中与数据库的两种连接
(1)Hibernate自行管理连接池
   编写Hibernate配置文件(hibernate.cfg.xml)
  
 <?xml version='1.0' encoding='GBK'?>
 <!DOCTYPE hibernate-configuration
     PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
     "
http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
 
 <hibernate-configuration>
 
     <session-factory>
         <property name="show_sql">true</property>
 
         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
   
         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
   
         <property name="connection.url">
       jdbc:mysql://localhost:3306/HibernateTest?useUnicode=true&amp;characterEncoding=GBK
         </property>
 
         <property name="connection.username">root</property>
   
         <property name="connection.password">root</property>
 
         <mapping resource="com/hibernate/User.hbm.xml"/>      
 
     </session-factory>
 
 </hibernate-configuration>


(2)使用外部服务器提供的数据库连接池(以Tomcat为例)
     分为两步:Tomcat会提供经过池处理的JDBC连接(用它内置的DBCP连接池),Hibernate通过JNDI方式来请求获得JDBC连接。
     a.Tomcat把连接池绑定到JNDI,我们要在Tomcat的主配置文件(TOMCAT/conf/server.xml)中加一个资源声明:

        <Context path="/quickstart" docBase="quickstart" reloadable="true">
        <Resource name="jdbc/quickstart" scope="Shareable" type="javax.sql.DataSource"/>
        <ResourceParams name="jdbc/quickstart">
            <parameter>
                <name>factory</name>
                <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
            </parameter>
    
            <!-- DBCP database connection settings -->
            <parameter>
                <name>url</name>
                <!--HibernateTest 是mysql中的一个schema-->
                <value>jdbc:mysql://localhost:3306/HibernateTest?useUnicode=true&amp;characterEncoding=GBK</value> 
            </parameter>
            <parameter>
                <name>driverClassName</name><value>com.mysql.jdbc.Driver</value>
            </parameter>
            <parameter>
                <name>username</name>
                <value>root</value>
            </parameter>
            <parameter>
                <name>password</name>
                <value>root</value>
            </parameter>
    
            <!-- DBCP connection pooling options -->
            <parameter>
                <name>maxWait</name>
                <value>3000</value>
            </parameter>
            <parameter>
                <name>maxIdle</name>
                <value>100</value>
            </parameter>
            <parameter>
                <name>maxActive</name>
                <value>10</value>
            </parameter>
        </ResourceParams>
    </Context>

   我们在这个例子中要配置的上下文叫做quickstart,它位于TOMCAT/webapp/quickstart目录下。
 Tomcat现在通过JNDI的方式:java:comp/env/jdbc/quickstart来提供连接。


     b.下一步就是配置Hibernate。首先Hibernate必须知道它如何获得JDBC连接,在这里我们使用基于XML格式的Hibernate配置文件(hibernate.cfg.xml)。

  <?xml version='1.0' encoding='utf-8'?>
  <!DOCTYPE hibernate-configuration
      PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
      "
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  
  <hibernate-configuration>
  
      <session-factory>
  
          <property name="connection.datasource">java:comp/env/jdbc/quickstart</property>
          <property name="show_sql">false</property>
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  
          <!-- Mapping files -->
          <mapping resource="com/jason/User.hbm.xml"/>
  
      </session-factory>
  
  </hibernate-configuration>


 配置好了,我们就可以编写一个servlet来测试。

参考资料
  hibernate-3.0/doc/reference/zh-cn

原创粉丝点击