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 ...
随机推荐
- [转]安卓新一代多渠道打包工具Walle 解决渠道包V2签名问题
转自https://www.jianshu.com/p/572b59829a08 为什么要打多个渠道的包? 大家都知道,android应用商店大大小小有几百个,作为一个有志向的app,就需要做到统计各 ...
- 27.C++- 智能指针
智能指针 在C++库中最重要的类模板之一 智能指针实际上是将指针封装在一个类里,通过对象来管理指针. STL中的智能指针auto_ptr 头文件: <memory> 生命周期结束时,自动摧 ...
- Hadoop API:遍历文件分区目录,并根据目录下的数据进行并行提交spark任务
hadoop api提供了一些遍历文件的api,通过该api可以实现遍历文件目录: import java.io.FileNotFoundException; import java.io.IOExc ...
- hive:默认允许动态分区个数为100,超出抛出异常:
在创建好一个分区表后,执行动态分区插入数据,抛出了错误: Caused by: org.apache.hadoop.hive.ql.metadata.HiveFatalException: [Erro ...
- Java-NIO(六):Channel聚集(gather)写入与分散(scatter)读取
Channel聚集(gather)写入: 聚集写入( Gathering Writes)是指将多个 Buffer 中的数据“聚集”到 Channel. 特别注意:按照缓冲区的顺序,写入 positio ...
- linux查看日志文件内容命令tail、cat、tac、head、echo
linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...
- Checkbutton
#tkinter之Checkbutton篇 #Checkbutton又称为多选按钮,可以表示两种状态,On和Off,可以设置回调函数,每当点击此按钮时回调函数被调用. 1.一个简单的Checkbutt ...
- disabled OR readonly
1.对元素设置disabled以及readonly属性 $("#uid").attr("disabled",true); $("#uid") ...
- BZOJ 4551[Tjoi2016&Heoi2016]树(树链剖分+二分)
Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记 ...
- java设计模式之代理设计模式(Proxy)
一个简单Proxy代理模式范例 interface Subject{ //定义核心主题所要完成的功能 public void make(); } class RealSubject implement ...