一、不同数据类型映射
<!-- name是类名,table是对应的表名 -->
<class name="cn.itcast.d_hbm_property.User" table="user">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<!--
name:对象中的属性名,必须要有
type:数据的类型,不写时会自动检测
column:对应的列名,不写时默认为属性的名称
not-null:true/false,是否有非空约束,默认为false
length:长度,默认为255
-->
<!--
<property name="name" type="string" column="name" not-null="true" length="35"/>
<property name="name" type="string">
<column name="name_" not-null="true" length="55"></column>
</property>
-->
<property name="name"></property>
<property name="gender"></property>
<!-- 日期要指定什么类型 -->
<property name="birthday" type="date"></property>
<!-- 大文本类型,最好指定长度 -->
<property name="desc" column="`desc`" type="text" length="5000"></property>
<!-- 二进制类型,最好指定长度 -->
<property name="photo" type="binary" length="512000"></property>
</class>
-------------------------------------------------------
private Integer id;
private String name; // 姓名
private boolean gender; // true表示男,false表示女
private Date birthday; // 生日
private String desc; // 一大段说明,最多为5000字
private byte[] photo; // 照片
二、主键映射
<hibernate-mapping>
<class name="cn.itcast.e_hbm_id.User" table="user">
<!-- 映射主键,generator子元素是指定主键生成策略的 -->
<id name="id">
<!-- identity,使用数据库的自动增长,在保存时会忽略手工指定的主键值而由数据库生成
要求此属性要是数字类型
<generator class="identity"/>
-->
<!-- assigned,手工指定,比如指定UUID
<generator class="assigned"/>
-->
<!-- uuid,由Hibernate生成UUID并指定为主键值,要求此属性要是String型
<generator class="uuid"/>
-->
<!-- hilo,高低位算法生成主键,需要用到一个额外的表,所有的数据库都可以使用这种类型
<generator class="hilo">
<param name="table">hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
</generator>
-->
<!-- native,根据底层数据库的能力选择 identity、sequence 或者 hilo 中的一个 -->
<generator class="native"/>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
三、联合主键映射
<class name="cn.itcast.e_hbm_id_2.User" table="user">
<!-- 映射联合主键 -->
<composite-id>
<key-property name="firstName" type="string"></key-property>
<key-property name="lastName" type="string"></key-property>
</composite-id>
<property name="gender"></property>
</class>
-----------------------------------------------------
private String firstName;
private String lastName;
private Boolean gender;
四、组成关系映射
<hibernate-mapping>
<class name="cn.itcast.f_hbm_component.User" table="user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<!-- 组成关系映射 -->
<component name="userAddress" class="cn.itcast.f_hbm_component.UserAddress">
<property name="address"></property>
<property name="code"></property>
<property name="phone"></property>
</component>
</class>
</hibernate-mapping>
--user---------------------------------
private Integer id;
private String name;
private UserAddress userAddress;
--UserAddress --------------------------
private String address;
private String code;
private String phone;