温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在eclipse中对hibernate框架进行配置

发布时间:2020-12-05 17:10:09 来源:亿速云 阅读:253 作者:Leah 栏目:编程语言

本篇文章为大家展示了如何在eclipse中对hibernate框架进行配置,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

一、ORM

O:object 对象

R:Realtion 关系(关系型数据库

M:Mapping 映射

ORM:对象关系型映射

目前流行的编程语言,如Java、C# ,它们都是面向对象的编程语言,而目前主流的数据库产品例如Oracle、DB2等,依然是关系型数据库。编程语言和底层数据库发展的不协调(阻抗不匹配,例如数据库中无法直接实现存储继承、多态、封装等特征和行为),催生出了ORM框架。ORM框架可以作为面向对象语言和关系型数据库之间的桥梁。

二、Hibernate

Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。

三、hibernate框架在eclipse下的配置方法,这里我们以hibernate3.2为例,介绍一下hibernate3.2在eclipse里的配置方法:

(1)打开eclipse,设置其工作空间,点击OK,进入eclipse主界面。

(2)首先我们创建一个java项目,File->new->java Project->创建项目名称,这里我们以ones为例。

如何在eclipse中对hibernate框架进行配置

(3)导入我们所需要的JAR包,这里我们需要导入3类jar包,首先是hibernate3.jar,是使用hibernate时必备的库。lib文件中的所有文件。数据库连接jar包,这里以mysql数据库文件,我们需要导入的jar包是mysql.jar。这里我们创建一个用户自己的类库,可以将我们的jar包直接导入user library中,当我们再建立其他的项目时,就避免了再重新一个一个的引入jar包。

创建步骤如图所示:

如何在eclipse中对hibernate框架进行配置

如何在eclipse中对hibernate框架进行配置

(4)点击Add External JARs... 以此导入上述jar包,点击OK,finish完成操作。此时,项目名下可看到名为first的用户自定义类库。

(5)我们在src文件目录下导入hibernate.cfg.xml文件。这里我们所需要更改的内容为第7行,localhost/ones(ones更改为自己的数据库名)

第9行为mysql用户名,第10行为mysql数据库的密码。第14行代码删掉。

<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/ones</property>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">88888888</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>
  
  <mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

(6)在src下建立用户类以及映射文件。Src右键->New->Class->选择类名,这里我们创建名为User的类。

(7)编写用户类代码(这里eclipse支持批量自动写入set/get方法)点击Source->Generate Ftters and Setters 选择全部,导入。User类已经编写完成,接下来我们编写映射文件。

package ones;

public class User {
 private String id;
 private String name;
 private String password;
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }

}

(8)选择User.hbm.xml文件,拷入Src文件夹下的包中,文件位置在hiberate\rg\org\hiberate\auction中,这里我们所要修改的代码是第6行,org.hibernate.auction改为自己项目的包名。第八行代码,可以只保留<Class name="User">,其余部分可以删掉。第九行代码删掉。将第12行的native删掉,native是配置整形数据的,我们之前设置的id为字符型,所以这里我们改为uuid,15行至50行,删掉。在<class>中编写属性,属性值等于User.java中定义的属性(不包括id)。

<&#63;xml version="1.0"&#63;>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping 
 package="org.hibernate.auction">

 <class name="User" table="AuctionUser" lazy="true">
  <comment>Users may bid for or sell auction items.</comment>
  
  <id name="id">
   <generator class="native"/>
  </id>
  
  <natural-id mutable="true">
   <property name="userName"
     length="10"/>
  </natural-id>
  
  <property name="password" 
    not-null="true"
    length="15"
    column="`password`"/>
  
  <property name="email"/>
  
  <component name="name">
   <property name="firstName"
     length="50"
     not-null="true"/>
   <property name="initial" 
     column="`initial`"/>
   <property name="lastName"
     length="50"
     not-null="true"/>
  </component>
  
  <bag name="bids"
    inverse="true" 
    cascade="save-update,lock">
   <key column="bidder"/>
   <one-to-many class="Bid"/>
  </bag>
  
  <bag name="auctions"
    inverse="true" 
    cascade="save-update,lock">
   <key column="seller"/>
   <one-to-many class="AuctionItem"/>
  </bag>
  
 </class>
 
</hibernate-mapping>

(9)编写后的User.hbm.xml文件如图所示:

<&#63;xml version="1.0"&#63;>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping 
 package="ones">

 <class name="User" >
  
  
  <id name="id">
   <generator class="uuid"/>
  </id>
  
  
  <property name="name"></property>
  <property name="password"></property>
 </class>
 
</hibernate-mapping>

(10)编写导入类,建立名为ExportDB的类,直接产生它的主方法

package ones;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;



public class ExportDB {

 public static void main(String[] args) {
  //读取文件的配置
  Configuration cfg = new Configuration().configure();
  SchemaExport export = new SchemaExport(cfg);
  export.create(ture, ture);
 }

}

(11)修改hibernate.cfg.xml中的第14行代码,将路径改为ones(包名)/User.hbm.xml

(12)在mysql数据库 中建立测试表,运行eclipse中的ExportDB文件,右键->Run As->java Application

上述内容就是如何在eclipse中对hibernate框架进行配置,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI