【SSH】Hibernate:配置文件解析

来源:互联网 发布:女寝还魂知乎 编辑:程序博客网 时间:2024/06/08 06:34


    hibernate基础配置选项主要围绕着SessionFacotry 展开的。我们可以通过配置文件或编码方式这两种方式来实现hibernate 的基础配置。


配置文件方式:使用configuration类:

      在实例化时自动加载CLASSPATHE中hibernate.cfg.xml的配置信息(DBURL/DBUSER/DBPASSWORD)。

      在实例化时手动加载:

configuration cfg=newconfiguration().configure(“myHibernate.cfg.xml”);  



编码方式:

      在xml配置时,我们可以根据mapping 节点配置需要加载的hibernate 映射文件,但是如果使用Properties形式的配置文件(hibernate.properties),就需要进行编码进行加载了。

    

<span style="font-family:Verdana;font-size:14px;">   Configuration  cfg = newConfiguration ().addFile(“XXX.xml”).addClass(***.class);</span>

意思是添加CLASSPATH目录下的需要映射的xml文件和映射实例类***.class。

 
完整的xml文件:

<span style="font-family:Verdana;font-size:14px;"><span style="font-family: Verdana; font-size: 14px;"><!DOCTYPE hibernate-configuration PUBLIC     "-//Hibernate/Hibernate Configuration DTD//EN"    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>            <!-- 配置MySQL 数据库 -->        <property name="dialect">net.sf.hibernate.dialect.MySqlDialect</property>        <property name="connection.driver_class">org.git.mm.mysql.driver</property>        <property name="connection.url">jdbc:mysql://localhost/shop</property>        <property name="connection.username">root</property>        <property name="connection.password">***</property>        <!-- 使用 C3P0 连接池. -->        <property name="c3p0.min_size">3</property>        <property name="c3p0.max_size">5</property>        <property name="c3p0.timeout">1800</property>            <!-- 事务管理类型 -->        <property name="hibernate.transaction.factory_class">         net.sf.hibernate.transaction.JDBCTransactionFactory        </property>        <!-- 映射文件配置,配置文件名必须包含于其相对于根的全路径 -->         <mapping resource="***/**/.xml"/>    </session-factory></hibernate-configuration></span></span>



此外hibernate中关于数据库连接配置方式分为两种:JDBC配置和JNDI配置:


Hibernate 数据库连接配置:


    JDBC配置:

1.      数据库适配器(dialect)

2.      数据库JDBC驱动类

3.      数据库URL

4.      数据库用户

5.      数据库用户密码

     在做网上商城是就用的jdbc的配置:

<span style="font-family:Verdana;font-size:14px;"><span style="font-family:Verdana;font-size:14px;">Hibernat.dialectnet.sf.hibenate.dialect.MySQLDialectHibernate.connection.driver_classcom.mysql.jdbc.DriverHibernate.connection.urljdbc:mysql://localhost/shopHibernate.connection.username rootHibernate.connection.password *****</span></span>

     当我们使用JDBC方式,还可以为指定数据库连接池实现,包括pool、c3po、dbcp、proxool四种,这里不多说,以后介绍。

 

 

    JNDI配置:

1.      JNDI连接的名称

2.      数据库用户

3.      数据库用户密码

4.      数据库适配器(dialect)

<span style="font-family:Verdana;font-size:14px;"><span style="font-family:Verdana;font-size:14px;">Hibernate.connection.datasource jdbc/testHibernate.connection.username userHibernate.connection.password pwdHibernate.dialect net.sf.hibernate.dialect.MySQLDialect</span></span>

           



0 0