Hibernate 下载、安装和使用

来源:互联网 发布:联通网络信号怎么样 编辑:程序博客网 时间:2024/05/01 11:46

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

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

三、Hibernate 的数据库操作

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

[java] view plaincopy在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 plaincopy在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 plaincopy在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 plaincopy在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 0
原创粉丝点击