【SSH】-Hibernate之SchemaExport+配置文件生成表结构
来源:互联网 发布:企业级的数据基础平台 编辑:程序博客网 时间:2024/05/03 02:55
天才第一步,雀氏纸尿裤,Hibernate第一步,连接数据库。
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 所以说,Hibernate讲白了就是简化了与数据库之间的操作,那么要玩转它自然就得连接上数据库咯。(与Hibernate对应的orm框架还有Mybatis,只不过Mybatis是半自动的,但是个人感觉更喜欢Mybatis,更自由,扩展性更强)。——引用网络
1、创建一个Java项目 :hibernate-test
2、创建User实体类:
package com.bjpowernode.hibernate;import java.util.Date;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;} }
3、创建User对应的配置文件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"><id name="id"><generator class="uuid"/></id><property name="name"/><property name="password"/><property name="createTime"/><property name="expireTime"/></class></hibernate-mapping>
4、创建实体类的映射文件,New》Other...输入Hibernate,弹出Hibernate Tools插件相关的内容,然后选中映射文件。
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><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">bjpowernode</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.show_sql">true</property><!-- <property name="hibernate.format_sql">true</property> --><mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/></session-factory></hibernate-configuration>
5、创建User Library ,加入依赖包
*HIBERNATE_HOME/lib/*.jar
*HIBERNATE_HOME/hibernate3.jar
*加入数据库驱动(mysql驱动)
6、创建工具类 ExportDB.java
package com.bjpowernode.hibernate;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;public class ExportDB {/** * 将hbm生成ddl * @param args */public static void main(String[] args) {//默认读取hibernate.cfg.xml文件Configuration cfg = new Configuration().configure();SchemaExport export = new SchemaExport(cfg);export.create(true, true);}}
7、首先在mysql中创建数据库 hibernate_test 数据库 ,然后 右击ExportDB-Run As -java Application ,此时就大功告成了!
友情链接: http://www.cnblogs.com/AlanLee/p/5839094.html
总结:hibernate它对JDBC进行了非常轻量级的对象封装。
0 0
- 【SSH】-Hibernate之SchemaExport+配置文件生成表结构
- Hibernate之SchemaExport+配置文件生成表结构
- 菜鸟学SSH(十一)——Hibernate之SchemaExport+配置文件生成表结构
- Hibernate使用SchemaExport和AnnotationConfiguration生成表结构
- 通过hibernate SchemaExport手动创建表结构
- Hibernate 使用SchemaExport自动生成表
- 使用schemaExport自动生成表结构
- Hibernate之SchemaExport
- Hibernate学习笔记之主键生成策略和SchemaExport
- 通过hibernate配置文件无法生成表结构
- 配置生成sessionFactory--ssh整合之带有hibernate配置文件
- Hibernate整合Spring后,如何使用SchemaExport生成数据库表
- 【Hibernate】——SchemaExport自动生成数据库表
- hibernate使用SchemaExport生成对应的数据库表
- javaee之hibernate的schemaExport
- 利用hibernate中的SchemaExport生成数据表
- 利用hibernate中的SchemaExport生成数据表
- hibernate使用SchemaExport自动生成数据表
- 1008-31 自动布局的标签/友盟中的坑/转Utf8编码/afn网络监测/多风格搜索器/账号区别
- 因特网协议第四版地址的分配
- 09---动态数组
- Android之SlidingPaneLayout侧滑效果扩展(SuperSlidingPaneLayout)
- 程序员遇到bug时的反应
- 【SSH】-Hibernate之SchemaExport+配置文件生成表结构
- 阿联酋是发达国家还是发展中国家
- nginx负载均衡ip-hash算法
- 分页插件 jquery.twbsPagination.js
- cygwin编译后脱离cygwin独立运行的包
- 取模和求余的区别
- centos yum配置
- HDU#1215:七夕节
- MySQL客户端工具推荐