前言
核心配置文件的编写,Hibernate的Mapping文件可以随便定义名字,但是默认是—数据名.hbx.xml。而核心配置文件的名字是固定的—hibernate.cfg.xml。而且要放在src目录下
核心配置
其中数据库的方言,和必须要配置的参数。都在下载的jar包里,有个project项目包中的etc中,有个配置文件hibernate.properties中有说明。
<?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>
<!-- 记住:先配置SessionFactory标签,一个数据库对应一个SessionFactory标签 -->
<session-factory>
<!-- 必须要配置的参数有5个,4大参数,数据库的方言 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 数据库的方言 -->
<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>
<!-- 生成数据库的表结构
update:如果没有表结构,创建表结构。如果存在,不会创建,添加数据
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 映射配置文件,需要引入映射的配置文件 -->
<mapping resource="com/itheima/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>