码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate

时间:2017-07-13 20:38:57      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:配置   工具   cal   oca   word   conf   root   bsp   import   

来自CSDN的学习:http://blog.csdn.net/aboy123/article/details/10085635

1. 理论

Hibernate的核心是“关系对象模型-ORM”,以操作对象的形式进行数据的存取。

2. 主要文件

2.1 实体类 User.java

普通的Java文件,是用户的实体类,包含用户的各项属性。

2.2 实体类的映射文件 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.example.hibernate.User">      <!-- 实体类的物理路径 -->
        <id name="id">  
            <generator class="uuid"/>  
        </id>  
        <property name="username"/>  
        <property name="password"/>  
        <property name="createTime"/>  
        <property name="expireTime"/>  
    </class>  
</hibernate-mapping>

 

2.3 核心配置文件 hibernate.cfg.xml

<hibernate-configuration>  
    <session-factory >  
        <!-- mysql数据库驱动 -->  
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
        <!-- mysql数据库名称 -->  
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>  
        <!-- 数据库的登陆用户名 -->  
        <property name="hibernate.connection.username">root</property>  
        <!-- 数据库的登陆密码 -->  
        <property name="hibernate.connection.password">root</property>  
        <!-- 方言:为每一种数据库提供适配器,方便转换 -->  
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
          
        <span style="color:#ff0000;"><mapping resource="com/example/hibernate/User.hbm.xml"/></span>  
    </session-factory>  
</hibernate-configuration> 

3. 主要步骤

3.1 在mysql控制台生成数据库

 

3.2 编写工具类,以生成与 User 对应的数据表

该步即是由 hbm 生成 ddl。工具类名为ExportDB.java

import org.hibernate.cfg.Configuration;  
import org.hibernate.tool.hbm2ddl.SchemaExport;  
/** 
 * 将hbm生成ddl 
 * @author BCH 
 * 
 */  
public class ExoprtDB {  
  
    public static void main(String[] args) {  
        //默认读取hibernate.cfg.xml文件  
        Configuration cfr = new Configuration().configure();  
          
        SchemaExport export = new SchemaExport(cfr);  
        export.create(true, true);  
    }  
} 

 

3.3 向表中添加数据

 

Hibernate

标签:配置   工具   cal   oca   word   conf   root   bsp   import   

原文地址:http://www.cnblogs.com/zhengmengen/p/7159023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!