Hibernate(二)----Hibernate前期准备

版权声明: https://blog.csdn.net/ysdsxry/article/details/79496408

前言:使用hibernate之前,需要进行一系列的准备工作,主要包括:下载hibernate、导入hibernate的相关jar包、导入hibernate-configuration.dtd文件、编写配置文件、配置映射文件的路径等;

1. 下载hibernate

使用hibernate是需要一些支撑文件的,所以我们需要去下载hibernate,可以去hibernate官网上下载,也可以到我的百度云盘下载。(链接:https://pan.baidu.com/s/1zEDpno32C-wisFQyAthuMg 密码:6on0)

下载完毕解压后,打开文件,会看到hibernate-release-5.0.7.Final目录结构如下:

 

接下来对这些目录做一下介绍:

documentation文件:存放了hibernate的相关文档,包括参考文档的api文档。

lib文件:存放hibernate编译和运行的依赖jar包,其中的required子目录是hibernate运行必须依赖的jar包。

project文件:存放hibernate的各种相关源代码。

2. 导入hibernate的相关jar包

运行hibernate必须依赖的jar包在lib/required文件夹下;在myeclipse中的web项目导入jar包的方式:复制jar包,然后把复制的jar包粘贴到项目的WebRoot/WEB-INF/lib路径下就可以了。

3. 导入hibernate-configuration.dtd文件

hibernate-configuration.dtd文件是一种dtd类型的文件,这类文件可以在不联网的情况在编写配置文件的时候有代码提示功能。(如果在联网的情况下,会自动缓存);所需要的dtd文件可以去我的百度云盘里下载(链接:https://pan.baidu.com/s/1QoPI7dAffgNAhh1ocIkf-Q 密码:igio)

dtd文件夹里面有两个文件,如下图:

 

一个是hibernate-configuration.dtd文件,另一个是hibernate-mapping.dtd文件,第一个文件是进行基本配置的代码提示文件;第二个文件是后面进行对象与数据库表的映射的代码提示文件,第二个文件可以先忽略。

接下来看一下如何导入hibernate-configuration.dtd文件?首先,点击myeclipse的Windows-->Preferences,在输入框里搜索 catalog,如下图所示:

 

然后,点击 add 进行文件的添加,选择 Catalog Entry,在Location一栏,点击File System去选择hibernate-configuration.dtd文件;Key type选择URI;Key中填写“http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd”(关于这一地址可以在Web App Librarias/hibernate-core-5.0.7.Final.jar/org.hibernate/hibernate-configuration-3.0.dtd中找到),然后点击OK就可以了。hibernate的配置dtd文件就被成功添加了。

 

4. 编写hibernate.cfg.xml配置文件

hibernate-configuration.dtd文件已经成功导入了,现在我们来编写与数据库连接的配置文件,在src目录下新建一个名为“hibernate.cfg.xml”的xml类型的文件(也可以命名为其他名字,但是“hibernate.cfg.xml”是默认的名字)。在上文中说过的hibernate-configuration-3.0.dtd文件中,有一段如下图所示的配置,将这段配置复制下来,粘贴到刚刚新建的hiberbate.cfg.xml文件中。

 

hibernate的基本配置文件的结构是

<hibernate-configuration>

<session-factory>

<!-- 基本配置 -->

</session-factory>

</hibernate-configuration>

所以,要在配置文件把<hibernate-configuration>和<session-factory>标签添加上。接下来可以通过hibernate-release-5.0.7.Final\project\etc\hibernate.properties来看下具体的配置,在hibernate.properties文件中搜索“MySQL”,可以搜到如下图所示的配置:

 

其中:hibernate.connection.driver_class是数据库驱动类的配置;

hibernate.connection.url是连接数据库的URL;

hibernate.connection.username是数据库的名字;

hibernate.connection.password是数据库的密码;

hibernate.dialect是数据库方言;

每一个配置空格的后面是选项;

好了,到此我们已经大体上知道了数据库配置的内容是什么了。那就来学习以下如何书写数据库配置吧。

<session-factory>标签下书写

<property name=”hibernate.connection..driver_class”>com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">pgmx0835</property>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

很明显,数据库配置的格式是<property name=配置名>具体配置内容</property>

5. 配置映射文件的路径

<session-factory>标签下继续编写配置,书写如下内容:

<mapping resource=.../.../XXX.hbm.xml/>

因为我们后面还会进行java对象与数据库表的映射,所以需要知道具体的映射路径。(在后续的讲解中会讲到这一部分,暂时不理解没关系,这里先这样写。)

6. 总结

这次我们讲解了使用hibernate之前必须要的一些配置,可以知道,hibernate与数据库连接的配置文件的结构大致如下:

<hibernate-configuration>

<session-factory>

<!-- 与数据库的配置 -->

<property name=配置名>具体的配置内容</property>

<!-- 映射配置文件路径设置 -->

<mapping resource=.../.../XXX.hbm.xml>

</session-factory>

</hibernate-configuration>

 

 

猜你喜欢

转载自blog.csdn.net/ysdsxry/article/details/79496408