疯了的Hibernate(3) -- 另一种配置文件

来源:互联网 发布:phpmyadmin备份数据库 编辑:程序博客网 时间:2024/05/02 01:03
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

    在(2)中说到Hibernate有两种配置文件Hibernate.properties和Hibernate.cfg.xml文件,使用哪种都可以。使用Hibernate.cfg.xml文件的好处是使配置文件更易读,更强的扩展能力。并且可以直接对映射文件加以配置。(映射文件:O/R Mapping的配置文件,将PO的属性与数据库表字段映射。它是个xml文件。)下面,我们就看一个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-2.0.dtd">

<Hibernate-configuration>

  <!--SessionFactory的配置-->

  <session-factory>

    <!--数据库的URL,这里使用MYSQL-->

    <property name="Hibernate.connection.url">

      jdbc:mysql:///sports

    </property>

 

    <!--数据库JDBC驱动程序-->

    <property name="Hibernate.connection.driver_class">

      org.gjt.mm.mysql.Driver

    </property>

 

    <!--用户名-->

    <property name="Hibernate.connection.username">

      hiswing

    </property>

 

    <!--密码-->

    <property name="Hibernate.connection.password">

      123

    </property>

 

    <!--方言,针对不同的数据库所特有的特性进行匹配,这里用的是MYSQL-->

    <property name="dialect">

      net.sf.Hibernate.dialect.MySQLDialect

    </property>

 

    <!--是否在执行期在控制台上输出SQL语句-->

    <property name="Hibernate.show_sql">

      true

    </property>

    <!--映射文件配置,配置文件名必须包含其相对于根的全路径-->

    <!--我在配置这里的时候,发现只要配置在相同包中的其中一个映射,其它映射的就可以不用配置了。-->

    <mapping resource="com/cuitao/sports/po/EmployeePo.hbm.xml"/>

    <!--mapping resource="com/cuitao/sports/po/TeamPo.hbm.xml"/-->

    <!--mapping resource="com/cuitao/sports/po/JobPo.hbm.xml"/-->

    <!--mapping resource="com/cuitao/sports/po/TeamMemberPo.hbm.xml"/-->

  </session-factory>

</Hibernate-configuration>

 

 

以上配置列出了其中比较重要的几个,还有一些其它的配置,这里就不一一列出了。需要说明的是,这个配置文件应该放在ClassPath中,简单点说,如果你使用的是eclipse,那么就将这个文件与工程文件(.classpath和.project)放在一起。对于Web应用而言,你应该将它放在/WEB-INF/classes目录下。

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>