1 必须的配置
-
连接数据库的基本参数
驱动类
url路径
用户名
密码 -
方言的配置
可选的配置
显示sql语句 ;hibernate.show_sql
格式化sql语句 ;hibernate.format_sql
自动建表 ;hibernate.hbm2ddl.auto
none ;不适用框架的建表功能
create ;如若数据库中已经有表框架会删除原有表创建新表;如果没有表直接新建(测试)
create-drop ;如果有表则先删除表再新建表执行操作接着删除;
如若没有表,新建表,执行操作再删除原有表(测试)
update ;数据库中有表使用原有表没有表会创建新表(更新表结构)
validate ;如果没有表不会创建表,如果有表直接使用表(自动校验衍射文件和表是否一致)
注意上边提到的更新表结构
例如你再映射配置文件中将数据库表中某一列字段信息修改了
如果这一列存在原有数据他会不动原有的这一列但是后边再添加的数据不会使用这一列他会一招你修改后的映射配置文件创建新的一列然后使用新的字段
映射文件的映入
引入映射文件的位置如下图
整个配置如下
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<!-- 连接数据库得的基本参数 -->
<property name = "hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name = "hibernate.connection.url">jdbc:mysql:///hibernate_day01?useUnicode=true&characterEncoding=utf-8</property>
<property name = "hibernate.connection.username">root</property>
<property name = "hibernate.connection.password">123123</property>
<!-- 配置hibernate的方言 -->
<property name = "hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 打印sql -->
<property name = "hibernate.show_sql">true</property>
<!-- 格式化sql -->
<property name = "hibernate.format_sql">true</property>
<!-- 自动创建表 -->
<property name = "hibernate.hbm2ddl.auto">update</property>
<!-- 映射文件的引入 -->
<mapping resource="com/heshihua/hibernate/demo1/Customer.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
核心文件的配置方式
属性文件的方式
hibernate.properties
不能引入映射文件,需要手动编写代码加载映射文件
xml文件的方式(常用)
hibernate.cfg.xml