微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

四、hibernate配置文件详细介绍 hibernate.cfg.xml

 1、hibernate核心配置文件讲解:hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<!-- session-factory  session工厂   -->
<session-factory>

	<!--	 JDBC4要素 配置信息 
			1、数据的Driver
			2、Url
			3、User
			4、Password	
	-->
	
	<!-- 1、根据使用的数据库类型和版本选择数据库Driver -->
	<property name="hibernate.connection.driver_class">
		com.MysqL.jdbc.Driver
	</property>
	
	<!-- 2、JDBC的URL 有2种配置方式
		2.1 jdbc:使用数据库类型://数据库IP:数据库端口号/数据库名称
		2.2 jdbc:使用数据库类型:///数据库名称
	 -->
	 <!-- 2.1 -->
	<property name="hibernate.connection.url">
		jdbc:MysqL://localhost:3306/test
	</property>	
	
	<!-- 2.2 -->
	<!-- 
	<property name="hibernate.connection.url">
		jdbc:MysqL:///test
	</property>
 	-->
 
	<!-- 指定数据库的编码格式,不是必须的,防止写入数据中文乱码 -->
	<property name="hibernate.connection.characterEncoding">true</property>
    <property name="hibernate.connection.characterEncoding">UTF-8</property>
    
    <!-- 数据库的User -->
	<property name="connection.user">root</property>
	
	<!-- 数据库的Psaaword -->
	<property name="connection.password">root</property>
	
	<!-- 数据库方言配置
		hibernate 支持多种数据库,
		通过设置方言 hibernate 才知道应该生成对应数据库sql 语句:hibernate 支持数据库的方言
		hibernate.properties 文件中都有
	-->
	<property name="dialect">
		org.hibernate.dialect.MysqL5Dialect
	</property>
	
	<!-- 打印hibernate生成sql语句 -->
	<property name="show_sql">true</property>
	
	<!-- 格式化打印的sql语句 -->
	<property name="format_sql">true</property>	
	
	<!-- 根据不同值,进行数据库表的操作
		create 每次执行 都删除原有表,然后创建新表
		create-drop 执行前创建表,执行后删除表
		update 如果有则不改变表,如果没有则创建
	-->
	<property name="hbm2ddl.auto">update</property>
	<!-- 将所有映射文件添加到这里 -->
	<mapping resource="vo/Student.hbm.xml" />

</session-factory>
</hibernate-configuration>

 2、*.hbm.xml 映射文件

          以我的Student表为例:

            

          Student.hbm.xml 

111

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐