obtain hibernate4.1.4

大家好,今天我们研究hibernate4.1.4,首先我们在http://www.hibernate.org/网站找最新版本hibernate,download在F:\专业\hibernate下,ok。

作为一个一无所知的菜鸟,我们首先去找hibernate帮助文档,F:\专业\hibernate\hibernate-release-4.1.4.Final\hibernate-release-4.1.4.Final\documentation\quickstart\en-US\html_single\index.html,打开它,英文不好的打开有道(做个广告,不知道又到以后会给我钱不)。

准备就绪,我们开始编写一个简单hibernate程序。

                              1、加.jar(这是必须)  

                                         index.html-->Release Bundle Downloads-->

  • The lib/required/ directory contains all the JARs Hibernate requires. All the jars in this directory must also be included in your project's classpath.

  • The /lib/jpa/ directory contains the hibernate-entitymanager jar and its dependencies beyond those in lib/required/. This defines Hibernate support for JPA.

  • The lib/envers directory contains the hibernate-envers jar and its dependencies beyond those in lib/required/

  • The lib/optional directory contains the jars needed for optional features of Hibernate.

2、添加配置文件

       a、The Hibernate configuration file

                  (1)、 F:\专业\hibernate\hibernate-release-4.1.4.Final\hibernate-release-4.1.4.Final\project\etc\hibernate.cfg.xml(放在classpath下,用于define JDBC connection information)

                  (2)、留下<session-factory>标签,在与之对应的property文件(F:\专业\hibernate\hibernate-release-4.1.4.Final\hibernate-release-4.1.4.Final\project\etc\hibernate.properties中找需要配置的内容,这里我们用的是mysql,找mysql的内容。在xml中,配置如下:

                        <hibernate-configuration>
                           <ession-factory name="foo">
                           <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
                          <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                          <property name="hibernate.connection.url">jdbc:mysql:///test</property>
                          <property name="hibernate.connection.username">root</property>
                          <property name="hibernate.connection.password">root</property>

 

                         <property name="hibernate.show_sql">true</property>                              <!-- 运行时显示sql语句 -->
                        <property name="hibernate.format_sql">true</property>                             <!--  按正规形式显示sql语句-->
                    </session-factory>
                     <ibernate-configuration>

       b、The mapping file

                  (1)、F:\专业\hibernate\hibernate-release-4.1.4.Final\hibernate-release-4.1.4.Final\project\documentation\src\main\docbook\quickstart\tutorials\basic\src\test\java

\org\hibernate\tutorial\hbm\Event.hbm.xml,copy。

                  (2)、放文件在与之对应的实体类包下,如图所示:                                       

                                                                                                

                                   c、将 mapping file   添加到 hibernate configuration file中              <mapping resource="com/wangpan/pojo/User.hbm.xml" />

 

                            3、在mysql中生成表

                                    (1)、包:com.wangpan.utils

                                    (2)、类:Hbm2DDLUtil.java

                                                        public class Hbm2DDLUtil {

 
 public static void main(String[] args) {

 

/**

 *  Configuration 默认读取名为hibernate配置文件

 *   configure()默认读取xml文件
  * */
  Configuration cf = new Configuration().configure();  
  SchemaExport se = new SchemaExport(cf);
  se.create(true, true);
 }

}

     

                             4、运行,输出sql语句。

                                            ok,战斗结束,当然有人会觉得:我不喜欢最新版本的,不稳定,想用3.5左右的。当然可,但有一个问题必须注意,3.5版本.jar包必须额外添加slf4j.jar,原因很简单,required文件夹下只有self4j-api.jar,并未对其进行实现,需要添加它的实现包(版本注意一样)。

                            

                          OK,但目前为止战斗结束。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值