在使用单纯hibernate文件映射时,项目应包含以下文件:
hibernate.cfg.xml 放在src/或根目录下
1 <?xml version='1.0' encoding='utf-8'?>
2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 8 <session-factory> 9 10 <!--数据库连接设置 --> 11 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 12 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 13 <property name="connection.username">root</property> 14 <property name="connection.password">123456</property> 15 16 17 <!-- 方言 指定MySQL5Dialet类--> 18 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 19 20 <!-- 控制台显示SQL --> 21 <property name="show_sql">true</property> 22 23 <!-- 自动更新表结构 --> 24 <property name="hbm2ddl.auto">update</property> 25
<!-- 引入映射文件,写全路径--> 26 <mapping resource="party/infoo/model/Student.hbm.xml"/> 27 <mapping resource="party/infoo/model/Course.hbm.xml"/> 28 31 </session-factory> 32 33 </hibernate-configuration>
***..hbm.xml 放在实体类包内
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="party.infoo.model"> <!--name为类名,table为数据库中的表 --> <class name="Course" table="t_course"> <!--name为属性,column为数据库中的字段 --> <!--id 主键 --> <id name="id" column="courseId"> <generator class="native"></generator> </id> <!--配置其他的属性-表字段的映射 --> <property name="name" column="courseName"></property> </class> </hibernate-mapping>