对应关系
首先,要使用持久化类和映射文件创建表,要先认识到持久化类与映射文件是如何一一对应的,如下:
配置文件
存放连接的数据库信息等,具体信息看以下hibernate.cfg.xml中代码注释解释:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!--
一个sessionFactory就代表一个数据库的描述
-->
<session-factory>
<!-- 链接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 链接数据库的密码 -->
<property name="connection.password">root</property>
<!-- 链接数据库的驱动 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 链接数据库的url -->
<property name="connection.url">
jdbc:mysql://localhost:3306/it11_hibernate
</property>
<!--
方言
告诉hibernate用什么样的数据库,将来会生成什么样的sql语句
-->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!--
hibernate对表的策略
validate 在hibernate容器启动的时候,根据映射文件和持久化类校验表
create 每次当hibernate启动的时候,都会根据持久化类和映射文件创建表
create-drop 每次当hibernate启动的时候,都会根据持久化类和映射文件创建表,销毁的时候删除表
update 检查,如果和映射文件不一致,则更新表的结构,如果没有表,则会创建表
-->
<property name="hbm2ddl.auto">update</property>
<mapping
resource="com/it11/hibernate/crud/domain/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
持久化类和映射文件
对类实现序列化才能在网络进行传输。
在一个持久化类中必须有一个默认的构造函数。
如下,小编要由持久化和映射文件创建Person表,则其持久化类文件:
package com.it11.hibernate.createtable;
import java.io.Serializable;
public class Person implements Serializable{
private Long pid;
private String name;
private String sex;
public Person(String name){
this.name = name;
}
public Person(){}
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
映射文件中描述了类与表的关系,如下Person类映射文件(具体内容见代码注释):
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<!--
class元素代表持久化类
name属性为类的全名
table 表名 默认值:类名
catalog 数据库的名字
-->
<class name="com.it11.hibernate.utils.Person" table="person" catalog="it11_hibernate">
<!--
id为主键元素
name 表示符属性
length 数据库中pid的长度
column pid属性对应的字段
type 类型
-->
<id name="pid" type="java.lang.Long">
<column name="pid" />
<!--
主键的生成器
需要通过什么样的方式产生主键
-->
<generator class="increment" />
</id>
<!--
代表一般的属性
-->
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" length="20" />
</property>
</class>
</hibernate-mapping>
客户端
产生表,CreateTableTest
package com.it11.hibernate.createtable;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class CreateTableTest {
@Test
public void testCreateTable(){
/**
* 加载了hibernate的配置文件
*/
Configuration configuration = new Configuration();
/**
* 要求配置文件:
* 1、必须是classpath的根目录
* 2、文件的名称必须是hibernate.cfg.xml
*/
configuration.configure();
//configuration.configure("");//第二种方式,参数为hibernate配置文件的路径及名称
SessionFactory sessionFactory = configuration.buildSessionFactory();
}
}
配置文件与映射文件的关系,在配置文件中的如下内容已经给出:
<mapping
resource="com/it11/hibernate/crud/domain/Person.hbm.xml" />
如果需要修改表结构,最好是将原来的表删除,然后重新生成,否则很容易造成错误,比如经常出现的两个重复主键的现象。