首先,在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.
这样子,就完成了数据表的创建。
|
相关推荐
通过myeclipse自动生成Hibernate类文件和hbm.xml文件,链接数据库库自动生成
在eclipse上通过hibernate工具,将数据库表生成Java类和xml配置文件 总共有2个版本,根据自己的需求使用版本3或者5: 1. hibernate-tools-3.4.0.CR2.jar是修改完成的jar包,直接替换原来的jar包, 重启eclipse即可...
CodeSmith针对NHibernate的.hbm.xml映射文件的生成模板,有单个表和整个数据库的模板
如何使用Myeclipse生成映射文件和实体类? 映射文件和实体类《=========》...选择要生成的文件hbm.xml,POJO,DAO(一般不选) j.选择映射类型,一般选择Hibernate Types k.点表名可以设置实体类名和主键生成方法 l.完成
hibernate-tool.jar 这个jar文件用来和ANT搭配由hbm.xml自动生成数据库schema,和java类
Hibernate中根据数据库中各表,使用myEclipse的返向工程,自动生成hibernate.cfg.xml 配置文件 与person.hbm.xml映射文件和持久化类 (图文介绍)
Hibernate中根据数据库中各表,使用myEclipse的返向工程,自动生成hibernate.cfg.xml 配置文件 与person.hbm.xml映射文件和持久化类 (图文介绍)
Hibernate 是一个开源的O/R mappimg的框架,基于JDBC提供了一种持久性数据管理的方案,相对于EntityBean来说是相当轻量级的。...还有一个ddl2hbm,是根据数据库来导出表结构,并生成映射文件和POJO class。
使用Hibernate对数据进行操作时会牵涉到3种类型的文件:Java类(实体类),映射文件(test.hbm.xml),数据库的表结构(表结构的创建).从理论上说只要知道其中一个,另外两个就可以从知道的那个文件中生成出来,这就涉及到先...
一般在项目开发过程中,使用比较多的就是先建好表,再利用hibernate反向工程生成*.hbm.xml文件跟POJO类,个人认为由于目前所使用的数据库都是关系数据库,而hibernate作为一个ORM,把对数据库的操作都对象化了,更...
包含了两个生成工具, hibernate-extensions是通过xml生成Java的 Middlegen 是通过数据库生成hbm.xml的
Hibernate多数据库支持...二、 如何动态加载.hbm.xml文件 三、 配置文件的加载 四、 大字段处理 五、 hibernate主键生成器(数值型主键操作方法) 六、 Oracle 转换 SQL Server 2000注意 七、 对UTF-8数据库的特殊处理
这个映射文件反映了POJO类中的各个属性和数据库后台表中的字段的对应关系,它是非常重要的,通过逆向工程操作,MyEclipse可以根据已经建好的数据库表,为我们自动生成这个xml文件 开发中要记住以下几点 对应每个...
rar中包含整个项目的源码和数据库生成脚本,采用jax-ws发布Web Service服务,支持java客户端和.Net客户端调用,数据库采用oracle10g,里面有创建数据库脚本文件createTable_Oracle10g.sql,由于Hibernate映射表中会...
配置hibernate.cfg.xml文件:该文件需放在src目录下 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" ...
课程内容 5 1 HelloWorld 5 2 Hibernate原理模拟 - 什么...7 建立Student 映射文件 Student.hbm.xml 6 8 将映射文件加入到hibernate.cfg.xml中 6 9 写测试类Main,在Main中对Student对象进行直接的存储测试 6 10 FAQ: 6
数据库的表结构是自动生成。在运行前先在mysql自己新建一个名为db_news的数据库。编码为gb2312 然后就可以部署了。找到下面这句话(还是在刚才那个文件里面) <prop key="hibernate.hbm2ddl.auto">update 第一次运行时...
每个数据表对应的其实是一个实体类,每个实体类有一个对应的hbm.xml配置文件匹配,myeclipse中有个MyEclipse Database Explorer视图,它提供了myeclipse与数据库直接连接的内置窗口,并且通过此窗口可以生成...
Service Builder是Liferay IDE(SDK)提供的一种代码生成方案,开发人员只需要编辑一个数据库的实体描述文件,即可根据本XML文件生成Spring层代码、Hibernate层代码、SQL、SQL索引创建文件、Spring和hibernate的配置...
12.6.7 生成映射文件User.hbm.xml 12.6.8 生成POJO 12.6.9 编写注册的业务逻辑接口Regedit.java 12.6.10 编写注册的业务逻辑类RegeditImpl.java 12.6.11 编写登录的业务逻辑接口Login.java 12.6.12 编写登录的业务...