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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 类的映射文件信息 -->
<!-- package指定类的包名 可以不配置 如果不配置 那么在配置class的name时需要指定该类所在包-->
<hibernate-mapping>
<!-- 实体类对应的表名称 class 配置类 name 指类名 table 指定表名 如果不写,默认类名为表名-->
<class name="vo.Student" table="Student">
<!-- 表中的字段的名称name 、类型 type-->
<!-- id 主键的配置 name 配置类的属性名column 数据库字段名 不写和属性名一致type 指定属性的类型length 指定字段的长度-->
<id name="id" column="id" type="int">
<!-- 主键的生成策略increment用于为 long, short 或者 int 类型生成 唯一标识。
只有在没有其他进程往同一张表中插入数据时才能使用。
在集群下不要使用identity对 DB2,MySQL, MS SQL Server, Sybase 和HypersonicSQL的内置标识字段提供支持。
返回的标识符是 long, short 或者 int 类型的。
native -(如果是 mysql 自增,那么 native 和 identity 是一样)根据底层数据库的能力选择 identity, sequence 或者hilo 中的一个。
sequence在 DB2,PostgreSQL, Oracle, SAP DB, McKoi 中使用列(sequence),而在 Interbase 中使用生成器(generator)。返回的标识符是 long, short 或者 int 类型的。
<generator class="sequence">
<param name="sequence" >user_seq</param>
</generator>assigned让应用程序在 save()之前为对象分配一个标示符。这是<generator>元素没有指定时的默认生成策略。
-->
<generator class="native"/>
</id>
<!-- 表中的字段的名称name 、类型 type-->
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
<property name="address" column="address" type="string"/>
<property name="phone" column="phone" type="string"/>
</class>
</hibernate-mapping>