hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库

今天就来演示一下Hibernate最初级的操作,使用SchemaExport创建数据表。

1.首先建立POJO类

  1. package com.bjpowernode.hibernate;
  2. import java.util.Date;
  3. /**
  4. * 用户
  5. * @author Longxuan
  6. *
  7. */
  8. public class User {
  9. private String  id;
  10. private String name;
  11. private String password;
  12. private Date createTime;
  13. private Date expireTime;
  14. public String getId() {
  15. return id;
  16. }
  17. public void setId(String id) {
  18. this.id = id;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. public String getPassword() {
  27. return password;
  28. }
  29. public void setPassword(String password) {
  30. this.password = password;
  31. }
  32. public Date getCreateTime() {
  33. return createTime;
  34. }
  35. public void setCreateTime(Date createTime) {
  36. this.createTime = createTime;
  37. }
  38. public Date getExpireTime() {
  39. return expireTime;
  40. }
  41. public void setExpireTime(Date expireTime) {
  42. this.expireTime = expireTime;
  43. }
  44. }

2、根据POJO类里面里面相关的字段,在包中创建User.hbm.xml映射文件

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="com.bjpowernode.hibernate.User" >
  7. <!--hibernate为我们生成主键id-->
  8. <id name="id">
  9. <generator class="uuid" />
  10. </id>
  11. <!--默认把类的变量映射为相同名字的表列,当然我们使用column属性修改表字段-->
  12. <property name="name" column="name"></property>
  13. <property name="password"></property>
  14. <property name="createTime"></property>
  15. <property name="expireTime"></property>
  16. </class>
  17. </hibernate-mapping>

3、在src中建立hibernate.cfg.xml

  1. <!DOCTYPE hibernate-configuration PUBLIC
  2. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  4. <hibernate-configuration>
  5. <session-factory name="foo">
  6. <!-- 数据库的连接也可以直接使用hibernate.properties文件 -->
  7. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  8. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_test</property>
  9. <property name="hibernate.connection.username">root</property>
  10. <property name="hibernate.connection.password">root</property>
  11. <property name="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property><!-- 指定sql方言 -->
  12. <property name="hibernate.show_sql">true</property><!-- 设置是否显示生成sql语句 -->
  13. <property name="hibernate.format_sql">true</property><!-- 设置是否格式化sql语句-->
  14. <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"  />
  15. </session-factory>
  16. </hibernate-configuration>

4、建立ExportDB类

  1. package com.bjpowernode.hibernate;
  2. import org.hibernate.cfg.Configuration;
  3. import org.hibernate.tool.hbm2ddl.SchemaExport;
  4. /**
  5. * 将hbm生成ddl
  6. * @author Longxuan
  7. *
  8. */
  9. public class ExportDB {
  10. /**
  11. * @param args
  12. */
  13. public static void main(String[] args) {
  14. // 默认读取hibernate.cfg.xml文件
  15. Configuration cfg = new Configuration().configure();
  16. // 生成并输出sql到文件(当前目录)和数据库
  17. SchemaExport export = new SchemaExport(cfg);
  18. // true 在控制台打印sql语句,true 导入sql语句到数据库,即可执行
  19. export.create(true, true);
  20. }
  21. }

5、建立log4j.properties日志文件

  1. ### direct log messages to stdout ###
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.Target=System.out
  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  5. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
  6. ### set log levels - for more verbose logging change 'info' to 'debug' ###
  7. log4j.rootLogger=warn, stdout

现在可以测试了。首先在MySQL中创建hibernate_test数据库:

运行ExportDB的main方法,结果如图:

Hibernate 自动生产表的更多相关文章

  1. hibernate自动建表采用UTF-8字符编码

    hibernate自动建表采用UTF-8字符编码 hibernate建表默认为UTF-8编码 >>>>>>>>>>>>>& ...

  2. Springboot 之 Hibernate自动建表(Mysql)

    Springboot 之 Hibernate自动建表(Mysql) 2016年10月21日 10:39:44 阅读数:8180 本文章来自[知识林] 引入Maven依赖包 <dependency ...

  3. hibernate自动建表之engine设置

    1.MYSQL的数据库引擎中,只有InnoDB和BDB(Berkley DB )包括了对事务处理和外键的支持.如果数据引擎建为MyISAM则rollback无效. 2.而hibernate自动建表的时 ...

  4. Hibernate自动建表问题

    自动见表配置 <property key="hibernate.hbm2ddl.auto">update</property> 运行时出现了一下错误 org ...

  5. Hibernate自动创建表

    只要在hibernate.cfg.xml添加这句话,就可以自动生成数据表 <property name="hibernate.hbm2ddl.auto">update& ...

  6. hibernate自动创建表报表不存在

    在hibernate.cfg.xml配置了<property name="hibernate.hbm2ddl.auto">update</property> ...

  7. Hibernate 自动创建表bug问题解决

    我在hibernate.cfg.xml配置文件中添加了自动创建表的的属性:(这样当数据库中没有此表是,hibernate就会自动帮我们创建一张表) <property name="hb ...

  8. Hibernate 自动更新表出错 More than one table found in namespace

    报错:Caused by: org.hibernate.tool.schema.extract.spi.SchemaExtractionException: More than one table f ...

  9. hibernate自动建表技术_采用数据库反向生成技术

    1.首先使用oracle创建一个用户: 登陆sqlplus,并以sysdba登陆到数据库: 2.创建一个用户,并对此用户授予connect,resource两个角色的权限: 3.连接到hibernat ...

随机推荐

  1. Mac里安装配置Jdk

    #下载jdk7的mac版 #官网下载地址http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.h ...

  2. mysql 索引学习--多条件等值查询,顺序不同也能应用联合索引啦

    以前学习这一块的时候,是说:假设建立了联合索引a+b,那么查询语句也一定要是这个顺序才能应用该索引. 那么实际是怎样呢,经过mysql这么多次版本升级,相信mysql已经给我们做了某些优化. 下面是我 ...

  3. Mysql官方文档翻译系列-7.3.1 Establishing a Backup Policy

    原文链接 (https://dev.mysql.com/doc/refman/5.7/en/backup-policy.html) 正文 To be useful, backups must be s ...

  4. 关于HTML

    我的PHP学习之旅 学习PHP已经有一段时间了,今天才想好好的总结一下这一路走来的点点滴滴,也想把我的学习方法及经验分享给大家,希望能对你有所帮助. 首先学习的是PHP前端部分,这里需要学习的知识有H ...

  5. 解决Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Student_recruit]]

    查看web.xml文件的书写,特别注意路径与命名一致

  6. bs4解析要获取被注掉的部分需先将注释符号去掉

    <div class="xzcf-content"> <div id="sfxz"> <div class="main- ...

  7. IdentityServer4-介绍大纲(译文)

    简介 IdentityServer4是一个基于ASP.NET CORE2使用OAuth2.0协议和OpenID Connect的框架 特性如下: Authentiaction作为一个Service 集 ...

  8. Asp.net Core2.0 缓存 MemoryCache 和 Redis

    自从使用Asp.net Core2.0 以来,不停摸索,查阅资料,这方面的资料是真的少,因此,在前人的基础上,摸索出了Asp.net Core2.0 缓存 MemoryCache 和 Redis的用法 ...

  9. js switch判断 三目运算 while 及 属性操作

    三 目运算:如var a = 10: var b= 12: c = a>b ?a:b; 若成立执行a否则执行b var isHide = true; 若用if判断语句如下 if(isHide) ...

  10. Ubuntu 16下安装64位谷歌Chrome浏览器

    Ubuntu 16下安装64位谷歌Chrome浏览器 1.将下载源加入到系统的源列表 在终端中,输入以下命令: sudo wget https://repo.fdzh.org/chrome/googl ...