NHibernate 配置文件详解

说到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目录下。


posted @ 2010-04-12 12:16  你妹的sb  阅读(776)  评论(0编辑  收藏  举报
百度一下