通过Hibernate的hbm.xml文件生成数据库表
2015-08-18 08:59
323 查看
首先,在src目录下,有一个文件,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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hxview?useUnicode=true&characterEncoding=gbk
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.password">download</property>
<property name="connection.driver_class">
org.gjt.mm.mysql.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="org/hx/model/projectList/ProjectList.hbm.xml" />
<mapping resource="org/hx/model/about/About.hbm.xml" />
<mapping resource="org/hx/model/gongshi/Gongshi.hbm.xml" />
<mapping resource="org/hx/model/projectinfo/ProjectInfo.hbm.xml" />
<mapping resource="org/hx/model/message/Message.hbm.xml" />
<mapping resource="org/hx/model/newsList/Newslist.hbm.xml" />
<mapping resource="org/hx/model/userList/Userlist.hbm.xml" />
</session-factory>
</hibernate-configuration>
?
其中,数据库名为:hxview ,org/hx/model/projectList/ProjectList.hbm.xml为包路径下的实体配置文件。
接下来,就是编写生成表的代码了,很简单,代码如下:
package utils;
import java.io.File;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class HibernateSchemaExport {
static Session session;
static Configuration config = null;
static Transaction tx = null;
public static void main(String[] args) {
try {
config = new Configuration().configure(new File(
"src/hibernate.cfg.xml"));
System.out.println("Creating tables...");
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);
System.out.println("Table created.");
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
} finally {
}
}
}
在运行前,请先确定数据库服务已经开启,并且数据库中存在名称为hxview的库,运行后,会将库里面的表删除,然后重新建表,所以,必须注意重要的数据是否已经保存!
运行结果如下:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Creating tables...
drop table if exists Message
drop table if exists ProjectInfo
drop table if exists about
drop table if exists gongshi
drop table if exists newslist
drop table if exists projectList
drop table if exists userlist
create table Message (id varchar(50) not null, msg text, adddate varchar(45) not null, ip varchar(45) not null, isflog varchar(2) not null, primary key (id))
create table ProjectInfo (id varchar(50) not null, names varchar(100) not null, info text not null, img text not null, primary key (id))
create table about (id varchar(50) not null, names varchar(45), tel varchar(45) not null, telcz varchar(45) not null, email varchar(100) not null, url varchar(200) not null, address text, primary key (id))
create table gongshi (id varchar(10) not null, info text not null, info2 text not null, primary key (id))
create table newslist (id varchar(50) not null, title varchar(100) not null, txt text not null, img text not null, adddate varchar(45) not null, downcount varchar(45) not null, ly varchar(45) not null, isflog varchar(45) not null, primary key (id))
create table projectList (id varchar(50) not null, img text not null, adddate varchar(45) not null, primary key (id))
create table userlist (id varchar(50) not null, names varchar(45) not null, pwd varchar(100) not null, regdate varchar(45) not null, flog varchar(2) not null, primary key (id))
Table created.
这样子,就完成了数据表的创建。
最后,七夕节就要到了,祝天下有情人终成眷属,已经工作的工作顺利,尚未工作的假期快乐!
?
<?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="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hxview?useUnicode=true&characterEncoding=gbk
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.password">download</property>
<property name="connection.driver_class">
org.gjt.mm.mysql.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="org/hx/model/projectList/ProjectList.hbm.xml" />
<mapping resource="org/hx/model/about/About.hbm.xml" />
<mapping resource="org/hx/model/gongshi/Gongshi.hbm.xml" />
<mapping resource="org/hx/model/projectinfo/ProjectInfo.hbm.xml" />
<mapping resource="org/hx/model/message/Message.hbm.xml" />
<mapping resource="org/hx/model/newsList/Newslist.hbm.xml" />
<mapping resource="org/hx/model/userList/Userlist.hbm.xml" />
</session-factory>
</hibernate-configuration>
?
其中,数据库名为:hxview ,org/hx/model/projectList/ProjectList.hbm.xml为包路径下的实体配置文件。
接下来,就是编写生成表的代码了,很简单,代码如下:
package utils;
import java.io.File;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class HibernateSchemaExport {
static Session session;
static Configuration config = null;
static Transaction tx = null;
public static void main(String[] args) {
try {
config = new Configuration().configure(new File(
"src/hibernate.cfg.xml"));
System.out.println("Creating tables...");
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);
System.out.println("Table created.");
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
} finally {
}
}
}
在运行前,请先确定数据库服务已经开启,并且数据库中存在名称为hxview的库,运行后,会将库里面的表删除,然后重新建表,所以,必须注意重要的数据是否已经保存!
运行结果如下:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Creating tables...
drop table if exists Message
drop table if exists ProjectInfo
drop table if exists about
drop table if exists gongshi
drop table if exists newslist
drop table if exists projectList
drop table if exists userlist
create table Message (id varchar(50) not null, msg text, adddate varchar(45) not null, ip varchar(45) not null, isflog varchar(2) not null, primary key (id))
create table ProjectInfo (id varchar(50) not null, names varchar(100) not null, info text not null, img text not null, primary key (id))
create table about (id varchar(50) not null, names varchar(45), tel varchar(45) not null, telcz varchar(45) not null, email varchar(100) not null, url varchar(200) not null, address text, primary key (id))
create table gongshi (id varchar(10) not null, info text not null, info2 text not null, primary key (id))
create table newslist (id varchar(50) not null, title varchar(100) not null, txt text not null, img text not null, adddate varchar(45) not null, downcount varchar(45) not null, ly varchar(45) not null, isflog varchar(45) not null, primary key (id))
create table projectList (id varchar(50) not null, img text not null, adddate varchar(45) not null, primary key (id))
create table userlist (id varchar(50) not null, names varchar(45) not null, pwd varchar(100) not null, regdate varchar(45) not null, flog varchar(2) not null, primary key (id))
Table created.
这样子,就完成了数据表的创建。
最后,七夕节就要到了,祝天下有情人终成眷属,已经工作的工作顺利,尚未工作的假期快乐!
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统