Hibernate 下载、安装和使用

一、下载 Hibernate 下载地址:http://hibernate.org/orm/downloads/

二、将解压缩路径中 lib 路径下的 required、jpa 子目录下所有 JAR 包添加到应用的类加载路径中。(数据库操作别忘了加入 JDBC 驱动)

三、Hibernate 的数据库操作

    1. 低侵入式设计  PO (persistant object) 持久对象类:  src\hsl\domain\News.java 代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package hsl.domain;  
  2.   
  3. public class News {  
  4.     // 消息类的标识属性  
  5.     private Integer id;  
  6.     // 消息标题  
  7.     private String title;  
  8.     // 消息内容  
  9.     private String content;  
  10.   
  11.     // id属性的setter和getter方法  
  12.     public void setId(Integer id) {  
  13.         this.id = id;  
  14.     }  
  15.   
  16.     public Integer getId() {  
  17.         return this.id;  
  18.     }  
  19.   
  20.     // title属性的setter和getter方法  
  21.     public void setTitle(String title) {  
  22.         this.title = title;  
  23.     }  
  24.   
  25.     public String getTitle() {  
  26.         return this.title;  
  27.     }  
  28.   
  29.     // content属性的setter和getter方法  
  30.     public void setContent(String content) {  
  31.         this.content = content;  
  32.     }  
  33.   
  34.     public String getContent() {  
  35.         return this.content;  
  36.     }  
  37. }  

    2. 为使以上 PO 类具备持久化操作的能力,这里 Hibernate 采用 XML 映射文件 src\hsl\domain\News.hbm.xml  代码如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="gb2312"?>  
  2.     <!-- 指定Hiberante3映射文件的DTD信息 -->  
  3. <!DOCTYPE hibernate-mapping PUBLIC   
  4.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  5.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  6.     <!-- hibernate-mapping是映射文件的根元素 -->  
  7. <hibernate-mapping package="hsl.domain">  
  8.     <!-- 每个class元素对应一个持久化对象 -->  
  9.     <class name="News" table="news_table">  
  10.         <!-- id元素定义持久化类的标识属性 -->  
  11.         <id name="id">  
  12.             <!-- 指定主键生成策略 -->  
  13.             <generator class="identity" />  
  14.         </id>  
  15.         <!-- property元素定义常规属性 -->  
  16.         <property name="title" />  
  17.         <property name="content" />  
  18.     </class>  
  19. </hibernate-mapping>  

    3. Hibernate 配置文件 src \hibernate.cfg.xml 代码如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <!-- 指定Hibernate配置文件的DTD信息 -->  
  3. <!DOCTYPE hibernate-configuration PUBLIC  
  4.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  5.     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  6. <!-- hibernate- configuration是连接配置文件的根元素 -->  
  7. <hibernate-configuration>  
  8.     <session-factory>  
  9.         <!-- 指定连接数据库所用的驱动 -->  
  10.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  11.         <!-- 指定连接数据库的url,hibernate连接的数据库名 -->  
  12.         <property name="connection.url">jdbc:mysql://localhost/hibernate</property>  
  13.         <!-- 指定连接数据库的编码 -->  
  14.         <property name="connection.characterEncoding">utf8</property>  
  15.         <!-- 指定连接数据库的用户名 -->  
  16.         <property name="connection.username">root</property>  
  17.         <!-- 指定连接数据库的密码 -->  
  18.         <property name="connection.password">8656216</property>  
  19.         <!-- 指定连接池里最大连接数 -->  
  20.         <property name="hibernate.c3p0.max_size">20</property>  
  21.         <!-- 指定连接池里最小连接数 -->  
  22.         <property name="hibernate.c3p0.min_size">1</property>  
  23.         <!-- 指定连接池里连接的超时时长 -->  
  24.         <property name="hibernate.c3p0.timeout">5000</property>  
  25.         <!-- 指定连接池里最大缓存多少个Statement对象 -->  
  26.         <property name="hibernate.c3p0.max_statements">100</property>  
  27.         <property name="hibernate.c3p0.idle_test_period">3000</property>  
  28.         <property name="hibernate.c3p0.acquire_increment">2</property>  
  29.         <property name="hibernate.c3p0.validate">true</property>  
  30.         <!-- 指定数据库方言 -->  
  31.         <!--<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>-->  
  32.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  33.           
  34.         <!-- 根据需要自动创建数据表 -->  
  35.         <property name="hbm2ddl.auto">update</property>  
  36.         <!-- 显示Hibernate持久化操作所生成的SQL -->  
  37.         <property name="show_sql">true</property>  
  38.         <!-- 将SQL脚本进行格式化后再输出 -->  
  39.         <property name="hibernate.format_sql">true</property>  
  40.         <!-- 罗列所有的映射文件 -->  
  41.         <mapping resource="hsl/domain/News.hbm.xml"/>  
  42.     </session-factory>  
  43. </hibernate-configuration>  

    4. 由于上面的程序需要使用 C3P0 连接池,因此我们还需要将下载的 hibernate-release-4.3.5.Final\lib\optional\c3p0  目录下的JAR 包也添加到系统的类加载路径下。

除此之外,由于Hibernate 3.6及以上版本都使用 SLF4J 作为日志工具,我们可以登录 http://www.slf4j.org/download.html,下载 SLF4J 日志工具slf4j-1.7.7.zip,将解压目录下的 slf4j-api-1.7.7.jar  和  slf4j-nop-1.7.7.jar  这两个JAR 包也添加到系统的类加载路径下。

    5. 测试信息数据插入数据库  src\hsl\NewsManager.java  代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package hsl;  
  2.   
  3. import org.hibernate.*;  
  4. import org.hibernate.cfg.*;  
  5. import hsl.domain.*;  
  6.   
  7. public class NewsManager {  
  8.     @SuppressWarnings("deprecation")  
  9.     public static void main(String[] args) throws Exception {  
  10.         // 实例化Configuration,  
  11.         Configuration conf = new Configuration()  
  12.         // 下面方法默认加载hibernate.cfg.xml文件  
  13.                 .configure();  
  14.         // 以Configuration创建SessionFactory  
  15.         SessionFactory sf = conf.buildSessionFactory();  
  16.         // 创建Session  
  17.         Session sess = sf.openSession();  
  18.         // 开始事务  
  19.         Transaction tx = sess.beginTransaction();  
  20.         // 创建消息实例  
  21.         News n = new News();  
  22.         // 设置消息标题和消息内容  
  23.         n.setTitle("中国风");  
  24.         n.setContent("中国风," + "网站地址hanshilei.3322.org");  
  25.         // 保存消息  
  26.         sess.save(n);  
  27.         // 提交事务  
  28.         tx.commit();  
  29.         // 关闭Session  
  30.         sess.close();  
  31.         sf.close();  
  32.     }  
  33. }  
注意:如果用的是 myEclipse 需要做如下修改配置

项目名上右键--〉myeclipse-->add hibernate capabilites -->next-->hibernate config file --> existing -->选择现有工程存在的hibernate配置文件--> next --> 不生成factory class --> end

    6. 在 mysql 数据库里创建对应数据库  hibernate 并运行程序 NewsManager.java


运行   src\hsl\NewsManager.java  文件得到结果


查看数据库如下:

0
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
3.6.10.Final 2012-02-09 3.6.9.Final 2011-12-15 3.6.8.Final 2011-10-27 3.6.7.Final 2011-08-17 3.6.6.Final 2011-07-21 3.6.5.Final 2011-06-09 3.6.4.Final 2011-05-05 3.6.3.Final 2011-04-06 3.6.2.Final 2011-03-10 3.6.1.Final 2011-02-03 3.6.0.Final 2010-10-14 3.6.0.CR2 2010-09-29 3.6.0.CR1 2010-09-16 3.5.6-Final 2010-09-15 3.6.0.Beta4 2010-09-02 3.5.5-Final 2010-08-18 3.6.0.Beta3 2010-08-18 3.6.0.Beta2 2010-08-04 3.5.4-Final 2010-07-22 3.6.0.Beta1 2010-07-21 3.5.3-Final 2010-06-17 3.5.2-Final 2010-05-14 3.5.1-Final 2010-04-15 3.5.0-Final 2010-03-31 3.5.0-CR-2 2010-02-25 3.5.0-CR-1 2010-02-10 3.5.0-Beta-4 2010-01-29 3.5.0-Beta-3 2010-01-14 3.5.0-Beta-2 2009-11-03 3.5.0.Beta-1 2009-08-21 3.3.2.GA 2009-06-24 3.2.7.ga 2009-06-03 3.3.1.GA 2008-09-11 3.3.0.SP1 2008-08-20 3.3.0.GA 2008-08-15 3.3.0.cr2 2008-08-01 3.3.0.cr1 2008-04-29 3.2.6.ga 2008-02-07 3.2.5.ga 2007-07-31 3.2.4.sp1 2007-05-18 3.2.4.ga 2007-05-09 3.2.3.ga 2007-04-02 3.2.2.ga 2007-01-24 3.2.1.ga 2006-11-17 3.2.0.ga 2006-10-16 3.2.0.cr5 2006-10-05 3.2.0.cr4 2006-08-25 3.2.0.cr3 2006-07-06 3.2 cr2 2006-05-06 3.2 cr1 2006-03-27 3.1.3 2006-03-20 3.2 alpha2 2006-03-16 3.2 alpha1 2006-03-01 3.1.2 2006-01-28 3.1.1 2006-01-18 3.1 2005-12-12 3.1 rc1 2005-12-12 3.1 rc3 2005-11-18 3.1 rc2 2005-10-17 3.1 beta 3 2005-09-13 3.1 beta2 2005-08-16 3.1 beta1 2005-07-21 3.1 alpha1 2005-06-24 3.0.5 2005-05-25 3.0.4 2005-05-23 3.0.3 2005-05-08 3.0.2 2005-04-27 3.0.1 2005-04-18 3.0 final 2005-03-31 3.0 rc 1 2005-02-28 3.0 beta 4 2005-02-11 3.0 beta 3 2005-01-30 3.0 beta 2 2005-01-24 3.0 beta 1 2004-12-20 3.0 alpha 2004-08-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值