hibernate通过配置文件生成数据库信息
hibernate可以通过配置文件在数据库生成相应的数据库信息。也可以把数据库的信息生成相应的代码(实体类操作类和映射文件)
下面是通过代码默认对hibernate.cfg.xml信息在数据库生成信息。事先要建好空的数据库
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="show_sql">true</property>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/guesswhat?useUnicode=true&characterEncoding=utf8</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">1234</property>
- <mapping resource="com/us/google/domains/TUser.hbm.xml"/>
- <mapping resource="com/us/google/domains/TUrl.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
hibernate.cfg.xml
列出一个表的单独配置信息
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.us.google.domains">
- <class name="TUser" table="t_user">
- <id name="id" column="id">
- <generator class="identity"></generator>
- </id>
- <property name="username" column="username"></property>
- <property name="password" column="password"></property>
- </class>
- </hibernate-mapping>
TUser.hbm.xml
执行操作
- package com.us.google.util;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- public class GenerateSql {
- public static void main(String[] args) {
- //默认读取hibernate.cfg.xml
- Configuration cfg = new Configuration().configure();
- //生成并输出sql到文件(当前目录)和数据库
- SchemaExport export = new SchemaExport(cfg);
- //true 在控制台打印sql语句,true 导入sql语句到数据库,即可执行
- export.create(true, true);
- }
- }
GenerateSql
运行截图如下,在控制台打印出了执行的sql语句(建立表)
即在数据生成了信息。但是这样比较麻烦,等下说建好表如何生成代码
发现上面的写法过时了 现在是hibernate5 推荐下面的写法
- ServiceRegistry serviceRegistry = (ServiceRegistry) new StandardServiceRegistryBuilder()
- .configure().build();
- MetadataImplementor metadataImplementor = (MetadataImplementor) new MetadataSources(
- serviceRegistry).buildMetadata();
- SchemaExport export = new SchemaExport(serviceRegistry,
- metadataImplementor);
- export.create(true, true);
Test.java
hibernate通过配置文件生成数据库信息的更多相关文章
- hibernate正向工程生成数据库
hibernate正向工程生成数据库 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 2 ...
- hibernate+mysql 自动生成数据库问题
Hibernate Entity类 表名注解大写时,在windows下mysql自动生成的表都为小写(不区分大小写),在linux下mysql自动生成区分大小写.导致数据库问题. 原因(window下 ...
- 菜鸟学SSH(十一)——Hibernate之SchemaExport+配置文件生成表结构
今天说点基础的东西,说说怎样通过SchemaExport跟Hibernate的配置文件生成表结构.事实上方法很easy,仅仅须要两个配置文件,两个Java类就能够完毕. 首先要生成表,得先有实体类,以 ...
- Hibernate之配置文件
可持久化对象有以下三种状态: 临时状态(Transient):对象在保存进数据库之前为临时状态,这时数据库中没有该对象的信息,如果没有持久化,程序退出后临时状态的对象信息将会丢失.随时可能被垃圾回收器 ...
- Hibernate 由实体类与配置文件的配置关系生成数据库中的表
import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class ...
- Hibernate之SchemaExport+配置文件生成表结构
首先要生成表,得先有实体类,以Person.java为例: /** * * @author Administrator * @hibernate.class table="T_Person& ...
- 用eclipes 添加jboss tools中的hibernate tool进行反向工程生成数据库对应的BOJO(Javabean)
用eclipes 添加jboss tools中的hibernate tool进行反向工程生成数据库对应的BOJO(Javabean) 安装: 在help中eclise marksplace中查询JBo ...
- hibernate笔记--通过SchemaExport生成数据库表
方法比较简单,项目中只需要两个java类(一个实体类,如User,一个工具类),两个配置文件(hibernate必须的两个配置文件hibernate.cfg.xml,与User.hbm.xml),即可 ...
- Hibernate生成数据库表
首先创建实体类 import java.util.Date; public class ProductionEntity { public Integer getId() { return id; } ...
随机推荐
- 限制Inactive物料录入BOM
应用 Oracle Bill Of Materiel 层 Level Function 函数名 Funcgtion Name CUX_BOMFDBOM 表单名 Form Name CUXBOMFD ...
- ICON图标文件解析
icon是一种图标格式,用于系统图标.软件图标等,这种图标扩展名为*.icon.*.ico.常见的软件或windows桌面上的那些图标一般都是ICON格式的. ICON文件格式比较简单,包含文件头段. ...
- 《我是一只IT小小鸟》 读后感
<我是一只IT小小鸟>一只是我想读list中一个本,但是上次去当当买的时候,竟然缺货了...昨天监考,实在无聊,就上网看电子书了,一天就看完了,看得有点仓促,所以理解估计不深. 1.刘帅: ...
- Sql Server 2008 卸载重新安装失败的解决办法!(多次偿试,方法均有效!)
Sql Server 2008 卸载重新安装失败的解决办法!(多次偿试,方法均有效!) 1.控制面板中卸载所有带sql server的程序. 2.在C盘C:\Program Files中sqlserv ...
- SQL高级查询
高级查询: 一.多表链接 1,普通查询 select * from 表名,表名 where 表名.列名 = 表名.列名 2,join链接 select * from 表名 join 表名 on 表名. ...
- saiku中过滤窗口优化及隐藏异常报错
问题一:当取消自动查询后,点击该维度应弹出过滤条件窗口,实际无反应,只有执行一次查询后再点击该维度,才能弹出过滤条件窗口 解决办法:打开WorkspaceDropZone.js文件,找到selecti ...
- css学习笔记三
总结一下水平居中和垂直居中的方法,欢迎交流指正,共同进步! 1.水平居中 1.1):行内元素水平居中,在其父类设置text-align:center; 1.2): 块级元素水平居中有三种 第一种:定宽 ...
- ACdream群赛1112(Alice and Bob)
题意:http://acdream.info/problem?pid=1112 Problem Description Here is Alice and Bob again ! Alice and ...
- HDU 5062 Beautiful Palindrome Number(数学)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...
- 初窥C++11:自己主动类型推导与类型获取
auto 话说C语言还处于K&R时代,也有auto a = 1;的写法.中文译过来叫自己主动变量.跟c++11的不同.C语言的auto a = 1;相当与 auto int a = 1;语句. ...