映射配置如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 建立类与表的映射 -->
<class name="com.heshihua.hibernate.demo1.Customer" table="cst_customer">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="cust_id" column="cust_id" >
<generator class="native"/>
</id>
<!-- 建立类中的普通的属性和表的字段的对应 -->
<property name="cust_name" column="cust_name" length="32" />
<property name="cust_source" column="cust_source" length="32"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>
class标签的配置
标签用来建立类与表的联系
属性
name ;类的全路径
table ;表名(类名和表名一致可以省略)
catalog ;数据库名
id标签的配置
类中的属性和表中主键的对应关系
属性
name ;类中的属性名
column ;表中的字段名(属性名和表中的字段名一致可以省略)
length ;长度 当你需要框架给你在数据库中自动生成表时就需要指定长度默认使用Java中数据类型的长度
type ;数据类型
有三种写法分别是Java类型 例如Java.lang.string
hibernate类型 例如string
数据库类型需要把column写成子标签例如下
<property name="cust_name" length="32" />
<column name="cust_name" sql-type></property>
</property>
大部分时候不用自己写 框架会自动帮你转换
property标签的配置
类中的普通属性和表中的字段对应关系
属性
name ;类中的属性名
column ;表中的字段名
length ;长度 当你需要框架给你在数据库中自动生成表时就需要指定长度默认使用Java中数据类型的长度
type
not-null ;不为空
unique ;唯一
结尾
如有错误请大佬指点