zoukankan      html  css  js  c++  java
  • Eclipse下构建hibernate项目流程

        折腾了一晚上,搜了很多教程,发现很多教程上讲的在Eclipse下构建hibernate项目的方法还真不一样。仔细琢磨了一下,发现只要搞清楚了hibernate的本质,其实不管什么步骤,结果都一样,简单记录一下。

        1、首先在Eclipse中建立一个Java project,这个是最基本的;

        2、将hibernate所需的包全部导入到构建路径中;

     

        3、编写hibernate.cfg.xml文件,配置数据连接属性;这个步骤可以自己直接编写hibernate.cfg.xml也可以利用Eclipse的插件的图形化界面来完成。该文件放在src文件夹下

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     5 <hibernate-configuration>
     6     <session-factory>
     7         <!-- 指定连接数据库所需的驱动 -->
     8         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
     9      
    10         <!-- 指定数据库的URL -->
    11         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_learn</property>
    12         
    13         <!-- 指定数据库的用户名和密码 -->
    14         <property name="hibernate.connection.password">123456</property>
    15         <property name="hibernate.connection.username">root</property>
    16         
    17         <!-- 指定数据库方言,因为hibernate可以管理很多数据库,各种数据库之间的sql会有一些不同,所以要指定 方言-->
    18         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    19   
    20         <!-- 指定数据库连接池中的最大和最小连接数 -->
    21         <property name="hibernate.c3p0.max_size">20</property>
    22         <property name="hibernate.c3p0.min_size">1</property>
    23 
    24         <!-- 指定数据库连接的超时时长 -->
    25         <property name="hibernate.c3p0.timeout">5000</property>
    26  
    27         <!-- 指定数据库连接池中最大的statement数 -->
    28         <property name="hibernate.c3p0.max_statements">100</property>
    29         <property name="hibernate.c3p0.idle_test_period">3000</property>
    30         <property name="hibernate.c3p0.acquire_increment">2</property>
    31         <property name="hibernate.c3p0.validate">true</property>
    32         
    33         <!-- 指定是否根据hbm文件自动创建数据表 -->
    34         <property name="hbm2ddl.auto">update</property>
    35         
    36         <!-- 指定是否打印sql语句 -->
    37         <property name="hibernate.show_sql">true</property>
    38         
    39         <!-- 罗列所有的映射文件 -->
    40         <mapping resource="org/crazyit/app/domain/News.hbm.xml"/>
    41    
    42    
    43     </session-factory>
    44 </hibernate-configuration>

        4、编写po类;

     1 package org.crazyit.app.domain;
     2 
     3 public class News {
     4 
     5     //消息类的标识属性
     6     private Integer id;
     7     
     8     //消息的标题
     9     private String title;
    10     
    11     //消息的内容
    12     private String content;
    13     
    14     public Integer getId() {
    15         return id;
    16     }
    17     public void setId(Integer id) {
    18         this.id = id;
    19     }
    20     public String getTitle() {
    21         return title;
    22     }
    23     public void setTitle(String title) {
    24         this.title = title;
    25     }
    26     public String getContent() {
    27         return content;
    28     }
    29     public void setContent(String content) {
    30         this.content = content;
    31     }
    32     
    33     
    34 }

        5、编写映射文件,xxx.hbm.xml;该文件放置在po的同一个包下

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC
     3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     5 <hibernate-mapping package="org.crazyit.app.domain">
     6   
     7   <!-- 每个类对应一个表 -->
     8   <class name="News" table="news_table">
     9       
    10        <id name="id">
    11        <!-- 指定主键生成策略 -->
    12        <generator class="identity"></generator>
    13        </id>
    14        
    15        <!-- 指定一般的属性 -->
    16        <property name="title"></property>
    17        <property name="content"></property>
    18   </class>
    19 </hibernate-mapping>

        6、编写测试类

     1 package test;
     2 
     3 import org.crazyit.app.domain.News;
     4 import org.hibernate.Session;
     5 import org.hibernate.SessionFactory;
     6 import org.hibernate.Transaction;
     7 import org.hibernate.cfg.Configuration;
     8 
     9 public class NewsManger {
    10 
    11     /**
    12      * @param args
    13      */
    14     public static void main(String[] args) {
    15 
    16 
    17         //实例化configuration,后面的configure()方法是加载hibernate.cfg.xml文件
    18         Configuration conf = new Configuration().configure();
    19         //用configuration的对象创建sessionfactory
    20         SessionFactory sf = conf.buildSessionFactory();
    21         //创建session
    22         Session sess = sf.openSession();
    23         //开始事务
    24         Transaction tx = sess.beginTransaction();
    25         
    26         //创建消息实例
    27         News n = new News();
    28         
    29         n.setTitle("疯狂Java联盟成立了");
    30         n.setContent("网址是:www.crazyit.com");
    31         
    32         //保存消息
    33         sess.save(n);
    34         
    35         //提交事务
    36         tx.commit();
    37         
    38         //关闭session
    39         sess.close();
    40         sf.close();
    41 
    42     }
    43 
    44 }

    因为hbm2ddl.atuo为update,因此第一次运行该测试代码,会在数据库中创建相应的表,而第二次运行则只会更新该表。如果将该属性设置为create,那么每次都会创建新表,覆盖原表。

    【总结】:其实创建hibernate项目最本质的操作只有3步,第一步,导入hibernate的jar吧,这一步无论在什么时候完成都行;第二步,编写配置文件hibernate.cfg.xml;第三步编写po类和相应的映射文件。学知识要懂原理,不然很容易被网上各种参差不齐的教程搞糊涂,多看书本上比较系统的教程,不要自己在网上瞎搞。

  • 相关阅读:
    CodeSmith注册错误的解决方法
    我是“坚守者”还是"背叛者"?
    拿什么留住你,我的程序员
    去除HTML代码得函数
    页面之间传递参数得几种方法
    nhibernate source code analyzed (abstract classes in nhibernate2.0)
    Web 2.0时代RSS的.Net实现
    Visual Studio.net 2003安装提示重启问题
    开放思路,综合考虑,心胸开阔,做一个合格的项目经理
    了解实际开发中 Hashtable 的特性原理 .NET, JAVA, PHP
  • 原文地址:https://www.cnblogs.com/hewenwu/p/4120125.html
Copyright © 2011-2022 走看看