您的位置:首页 > 数据库

通过Hibernate的hbm.xml文件生成数据库表

2016-03-24 14:38 656 查看
首先,在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.
这样子,就完成了数据表的创建。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息