这里有新鲜出炉的Eclipse教程,程序狗速度看过来!
Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。
下面小编就为大家带来一篇基于hibernate框架在eclipse下的配置方法(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
一、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为例。
(3)导入我们所需要的JAR包,这里我们需要导入3类jar包,首先是hibernate3.jar,是使用hibernate时必备的库。lib文件中的所有文件。数据库连接jar包,这里以mysql数据库文件,我们需要导入的jar包是mysql.jar。这里我们创建一个用户自己的类库,可以将我们的jar包直接导入user library中,当我们再建立其他的项目时,就避免了再重新一个一个的引入jar包。
创建步骤如图所示:
(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)。
- <?xml version="1.0"?>
- <!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文件如图所示:
- <?xml version="1.0"?>
- <!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
以上这篇基于hibernate框架在eclipse下的配置方法(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持PHPERZ。
来源: http://www.phperz.com/article/17/1211/353318.html