映射配置:
例:
<?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">
<hibernate-mapping>
<!-- 建立类与表的映射 -->
<class name="com.test.Customer" table="cst_customer">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="cust_id" column="cust_id" type="java.lang.Long" catalog="" >
<generator class="sequence" >
<param name="sequence_name">cust_id_sequence</param>
</generator>
</id>
<!-- 建立类中的普通的属性和表的字段的对应 -->
<property name="cust_name" column="cust_name" type="java.lang.String" length="32" />
<property name="cust_source" column="cust_source" type="java.lang.String" length="32" />
<property name="cust_industry" column="cust_industry" type="java.lang.String" length="32" />
<property name="cust_level" column="cust_level" type="java.lang.String" length="32" />
<property name="cust_phone" column="cust_phone" type="java.lang.String" length="64" />
<property name="cust_mobile" column="cust_mobile" type="java.lang.String" length="16" />
</class>
</hibernate-mapping>
【class标签】
■class标签用来建立表的映射关系
■属性:
▲name :类的路径。
▲table :表名(类名与表名 可省了 table)
▲catalog : 数据库名(可以不填)
■【id标签】
■标签用建立类中的属性与表中的主键的对应关系
■属性:
▲name :类中的属性名。
▲column :表中的字段。(类中的属性名 和 表中的字段名 一样 可省略 column)
▲length :长度。
▲type :数据类型。
■【id标签】
■标签用来建立类中普通属性与表字段对应关系:
■属性:
▲name :类中的属性名。
▲column :表中的字段。
▲length :长度。
▲type :数据类型。
▲not-null :不为空。
▲unique :设置唯一。
type 和length 是hibernate 自动创建表使用
hibernate 核心配置
列子:
<?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">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1522:orcl</property>
<property name="hibernate.connection.username">hncdsite</property>
<property name="hibernate.connection.password">power123</property>
<!-- 配置Hibernate的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</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/test/customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
●必须配置
■连接数据库的基本参数
▲驱动类
▲url路径
▲用户名
▲密码
■ 方言
● 可选配置
■打印sql : hibernate.show_sql
■格式化sql : hibernate.format_sql
■自动创建表 : hibernate.hbm2ddl.auto
▲none :不使用。
▲create :如果数据库有表删除表再新建表,如果没有表新建表(测试)。
▲create-drop:(测试)如果数据库有表删除表,执行代码,在删除这个表。如果没有表新建,使用完成再删除表
(java需要sesionFactory.close();)
▲update:如果数据库有表使用原有表,如果没有表创建新表,和表结构的更新操作。
▲validate:校验,如果没有表不会创建表,只会使用数据库的表,但会校验映射。
● 映射文件引入:
<mapping resource="com/test/customer.hbm.xml" />
hibernate 配置文件有两种方式:
一 . 属性文件的方式 :hibernate.properties
配置用等于号连接 hibernate.connection.driver_class=com.mysql.jdbc.Driver;
属性文件不能引入映射文件(自己编写映射文件)
二 . XML文件 方式 :hibernate.cfg.xml