一:配置 文件
<?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 package = "userTable">
<!-- 简历类与表的映射 -->
<class name = "userTable.user" table="user">
<!-- 建立类中的属性与表中的主键对应 -->
<id name = "userid" column="userid">
<generator class = "native"/>
</id>
<!-- 建立类中的普通属性和表的字段的对应 -->
<property name = "username" column = "username"/>
<property name = "password" column = "password"/>
</class>
<!-- 简历类与表的映射 -->
<class name = "userTable.data" table="data">
<!-- 建立类中的属性与表中的主键对应 -->
<id name = "id" column="id">
<generator class = "native"/>
</id>
<!-- 建立类中的普通属性和表的字段的对应 -->
<property name = "entity1" column = "entity1"/>
<property name = "entity2" column = "entity2"/>
<property name = "relation" column = "relation"/>
</class>
</hibernate-mapping>
<?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">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bysj?useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<!-- 打印SQL -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 自动创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置Hibernate的方言 -->
<property name = "hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="userTable/user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
二. 特别注意!!
![](https://i-blog.csdnimg.cn/blog_migrate/238d55d4a8d1ee01cb80c4e34d02a7df.png)
任何一个配置文件里的class都得配置主键!!!!为什么呢?
因为在Hibernate中必须要有一个条件来区分两条数据的不同,而在数据库中主键刚好起到这个作用