Hibernate 自动生产表
hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。
今天就来演示一下Hibernate最初级的操作,使用SchemaExport创建数据表。
1.首先建立POJO类
- package com.bjpowernode.hibernate;
- import java.util.Date;
- /**
- * 用户
- * @author Longxuan
- *
- */
- public class User {
- private String id;
- private String name;
- private String password;
- private Date createTime;
- private Date expireTime;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public Date getCreateTime() {
- return createTime;
- }
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
- public Date getExpireTime() {
- return expireTime;
- }
- public void setExpireTime(Date expireTime) {
- this.expireTime = expireTime;
- }
- }
2、根据POJO类里面里面相关的字段,在包中创建User.hbm.xml映射文件
- <?xml version="1.0"?>
- <!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.bjpowernode.hibernate.User" >
- <!--hibernate为我们生成主键id-->
- <id name="id">
- <generator class="uuid" />
- </id>
- <!--默认把类的变量映射为相同名字的表列,当然我们使用column属性修改表字段-->
- <property name="name" column="name"></property>
- <property name="password"></property>
- <property name="createTime"></property>
- <property name="expireTime"></property>
- </class>
- </hibernate-mapping>
3、在src中建立hibernate.cfg.xml
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory name="foo">
- <!-- 数据库的连接也可以直接使用hibernate.properties文件 -->
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_test</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">root</property>
- <property name="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property><!-- 指定sql方言 -->
- <property name="hibernate.show_sql">true</property><!-- 设置是否显示生成sql语句 -->
- <property name="hibernate.format_sql">true</property><!-- 设置是否格式化sql语句-->
- <mapping resource="com/bjpowernode/hibernate/User.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
4、建立ExportDB类
- package com.bjpowernode.hibernate;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- /**
- * 将hbm生成ddl
- * @author Longxuan
- *
- */
- public class ExportDB {
- /**
- * @param args
- */
- 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);
- }
- }
5、建立log4j.properties日志文件
- ### direct log messages to stdout ###
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.Target=System.out
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
- ### set log levels - for more verbose logging change 'info' to 'debug' ###
- log4j.rootLogger=warn, stdout
现在可以测试了。首先在MySQL中创建hibernate_test数据库:
运行ExportDB的main方法,结果如图:
Hibernate 自动生产表的更多相关文章
- hibernate自动建表采用UTF-8字符编码
hibernate自动建表采用UTF-8字符编码 hibernate建表默认为UTF-8编码 >>>>>>>>>>>>>& ...
- Springboot 之 Hibernate自动建表(Mysql)
Springboot 之 Hibernate自动建表(Mysql) 2016年10月21日 10:39:44 阅读数:8180 本文章来自[知识林] 引入Maven依赖包 <dependency ...
- hibernate自动建表之engine设置
1.MYSQL的数据库引擎中,只有InnoDB和BDB(Berkley DB )包括了对事务处理和外键的支持.如果数据引擎建为MyISAM则rollback无效. 2.而hibernate自动建表的时 ...
- Hibernate自动建表问题
自动见表配置 <property key="hibernate.hbm2ddl.auto">update</property> 运行时出现了一下错误 org ...
- Hibernate自动创建表
只要在hibernate.cfg.xml添加这句话,就可以自动生成数据表 <property name="hibernate.hbm2ddl.auto">update& ...
- hibernate自动创建表报表不存在
在hibernate.cfg.xml配置了<property name="hibernate.hbm2ddl.auto">update</property> ...
- Hibernate 自动创建表bug问题解决
我在hibernate.cfg.xml配置文件中添加了自动创建表的的属性:(这样当数据库中没有此表是,hibernate就会自动帮我们创建一张表) <property name="hb ...
- Hibernate 自动更新表出错 More than one table found in namespace
报错:Caused by: org.hibernate.tool.schema.extract.spi.SchemaExtractionException: More than one table f ...
- hibernate自动建表技术_采用数据库反向生成技术
1.首先使用oracle创建一个用户: 登陆sqlplus,并以sysdba登陆到数据库: 2.创建一个用户,并对此用户授予connect,resource两个角色的权限: 3.连接到hibernat ...
随机推荐
- Python内置函数(33)——any
英文文档: any(iterable) Return True if any element of the iterable is true. If the iterable is empty, re ...
- BAT美团滴滴java面试大纲(带答案版)之三:多线程Lock
继续面试大纲系列文章. 这是多线程的第二篇. 多线程就像武学中对的吸星大法,理解透了用好了可以得道成仙,俯瞰芸芸众生:而滥用则会遭其反噬. 在多线程编程中要渡的第二个“劫”,则是Lock.在很多时候, ...
- 高级控件 popwindow 与gridview的组合应用
Gridview 的布局设置 <GridView android:layout_width="wrap_content" android:layout_height=&quo ...
- [LuoguP1113] 杂物 - 拓扑排序
其实只是纪念下第一篇洛谷题解? Description John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及 ...
- Windows10+Docker搭建分布式Redis集群(SSH服务镜像)(二)
前言:上篇文章我们搭建好了Docker,下面我们开始使用Docker创建镜像,Docker命令就不介绍了.这里宿主是Windows10,cmd的管理和后期文件的复制不是很方便,将创建支持SSH的Cen ...
- 俄罗斯方块(2D、3D)
声明:这篇文章主要是参考几个别人的博文及源代码学习.参考文章: 1)http://blog.csdn.net/qian_f/article/details/19758671 2)http://yaca ...
- view-xpath
https://addons.mozilla.org/en-US/firefox/ WebDriver Element Locator
- 如何解释vue的生命周期才能令面试官满意?
当面试官问:"谈谈你对vue的生命周期的理解",听到这句话你是不是心里暗自窃喜:这也太容易了吧,不就是beforeCreate.created.beforeMount.mounte ...
- 树莓派(1)- Raspberry Pi 3B 安装系统并联网
一.背景 昨天到手淘宝买的3B,既然买了就不能让它吃灰,动起来. 二.物料 名称 说明 硬件 树莓派3B 主体 树莓派电源 5V 2A sd卡 4G低速(推荐是16G class10),我手头只有这 ...
- NPM实用指北
npm作为下载node附送的大礼包,大家一定不会陌生. 然而关于npm,估计大量的只是用到npm install XXX以及npm run XXX. 其实这里边还有很多有意思的命令&参数.关于 ...