Hibernate提供了从xml生成数据表的数据功能。

示例:

Studnet.hbm.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<? 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.mzsx.model.Student"  table = "student"  catalog = "struts" >
< id  name = "id"  type = "java.lang.String" >
< column  name = "studentid"  />
< generator  class = "uuid"  />
</ id >
< property  name = "studentname"  type = "java.lang.String" >
< column  name = "studentname"  length = "50"  not-null = "true"  />
</ property >
< one-to-one  name = "cardid"  class = "com.mzsx.model.Card"  cascade = "all"   ></ one-to-one >
</ class >
</ hibernate-mapping >

Card.hbm.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<? 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.mzsx.model.Card"  table = "card"  catalog = "struts" >
< id  name = "id"  type = "java.lang.String" >
< column  name = "cardid"  />
< generator  class = "foreign" >
< param  name = "property" >studentid</ param >
</ generator >
</ id >
< property  name = "cardname"  type = "java.lang.String" >
< column  name = "cardname"  length = "50"  not-null = "true"  />
</ property >
< one-to-one  name = "studentid"  class = "com.mzsx.model.Student"  ></ one-to-one >
</ class >
</ hibernate-mapping >

hibernate.cfg.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<? 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">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
< hibernate-configuration >
< session-factory >
< property  name = "hbm2ddl.auto" >update</ property >
<!-- Mysql -->
< property  name = "dialect" >org.hibernate.dialect.MySQLDialect</ property >
< property  name = "connection.url" >jdbc:mysql://localhost:3306/struts</ property >
< property  name = "connection.username" >root</ property >
< property  name = "connection.password" >admin</ property >
< property  name = "connection.driver_class" >com.mysql.jdbc.Driver</ property >
< property  name = "myeclipse.connection.profile" >MySQL connection</ property >
<!-- Oracle -->
<!-- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.username">0804</property>
<property name="connection.password">0804</property>
<property name="connection.url">jdbc:oracle:thin:@192.168.0.200:1521:orcl</property> -->
<!-- 配置JDBC里batch的大小 -->
< property  name = "jdbc.fetch_size" >50</ property >
< property  name = "jdbc.batch_size" >25</ property >
<!-- 配置事务实现方式 -->
< property  name = "transaction.factory_class" >org.hibernate.transaction.JDBCTransactionFactory</ property >
<!-- 配置线程安全的session -->
< property  name = "current_session_context_class" >thread</ property >
< property  name = "javax.persistence.validation.mode" >none</ property >
<!-- 显示SQL -->
< property  name = "show_sql" >true</ property >
< property  name = "format_sql" >true</ property >
<!-- 配置连接池 -->
< property  name = "c3p0.max_size" >2</ property >
< property  name = "c3p0.min_size" >2</ property >
< property  name = "c3p0.timeout" >5000</ property >
< property  name = "c3p0.max_statements" >100</ property >
< property  name = "c3p0.idle_test_period" >3000</ property >
< property  name = "c3p0.acquire_increment" >2</ property >
< property  name = "c3p0.validate" >false</ property >
<!-- 指定hibernate管理的映射文件 -->
< mapping  resource = "com/mzsx/model/Card.hbm.xml"  />
< mapping  resource = "com/mzsx/model/Studnet.hbm.xml"  />
<!--
<mapping resource="com/mzsx/model/Blog.hbm.xml" />
<mapping resource="com/mzsx/model/Comment.hbm.xml" />
<mapping resource="com/mzsx/model/Category.hbm.xml" />
<mapping resource="com/mzsx/model/Link.hbm.xml" />
<mapping resource="com/mzsx/model/Admin.hbm.xml" /> -->
</ session-factory >
</ hibernate-configuration >

TableTest:

1
2
3
4
5
6
7
8
9
10
package  com.mzsx.test;
import  org.hibernate.cfg.Configuration;
import  org.hibernate.tool.hbm2ddl.SchemaExport;
public  class  TableTest {
public  static  void  main(String[] args) {
Configuration configuration= new  Configuration().configure();
SchemaExport sc= new  SchemaExport(configuration);
sc.create( true false );
}
}
执行的结果:
1
2
3
4
5
6
7
8
9
10
11
12
drop  table  if exists struts.card
drop  table  if exists struts.student
create  table  struts.card (
cardid  varchar (255)  not  null ,
cardname  varchar (50)  not  null ,
primary  key  (cardid)
)
create  table  struts.student (
studentid  varchar (255)  not  null ,
studentname  varchar (50)  not  null ,
primary  key  (studentid)
)