Hibernate(2.0)_Hibernate框架的配置
LanyuanXiaoyao's Blog ヽ(✿゚▽゚)ノ

Hibernate(2.0)_Hibernate框架的配置

2016-09-05

操作平台

MyEclipse Enterprise Workbench 2015 Stable 2.0 Java Development kit 1.6 Hibernate 4.1.4 MySQL Server 5.7 Navicat Premium 11.2.7 Window10 专业版 64-bit (所有文章无特殊说明均在此平台下操作)

MyEclipse中创建带Hibernate项目

  然后Next->

  得到一个正常的Web项目,然后加入Hibernate框架   在项目上点击右键->Myeclipse->Project Facets [Capabilities]->Install Hibernate Facet

  然后再弹出的对话框中开始进行简单的框架相关设置

  选择默认设置

  默认设置之下,需要指定包名,如果已经创建了包,点击Browse选择已有包

  如果没有就点击New进行新建,不再赘述   然后,如果像笔者这样使用已经搭建好的外部MySQL数据库,此时直接点击Finish即可完成创建,如果需要使用MyEclipse自带的数据库创建工具创建数据库则点击Next进行下一步设置   (一般很少有人用MyEclipse自带工具搭建数据库,所以此处直接跳过如何用自带工具创建的过程)   点击Finish之后弹出是否进入Hibernate设置的对话框,点击Cancel,之后我们自己再手动配置 此时可以看到项目目录里面,Hibernate相关的库已经被添加到项目中

  然后开始配置Hibernate配置文件   打开hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
    <!-- 数据库登陆名 -->
    <property name="connection.username">root</property>
    <!-- 数据库登陆密码 -->
    <property name="connection.password">root</property>
    <!-- 数据库连接URL -->
    <property name="connection.url">
        jdbc:mysql://127.0.0.1:3306/sunjob4hibernate
    </property>
    <!-- 数据库驱动文件 -->
    <property name="connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <!-- 设置Hibernate方言 -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- 设置Hibernate显示sql语句 -->
    <property name="show_sql">true</property>
    
    <!-- 指定pojo映射文件 -->
    <mapping resource="com/pojo/Student.hbm.xml"/>
</session-factory>

</hibernate-configuration>

注:

  关于其中的属性,前面四个不再赘述   方言(dialect)   对于不同的数据库,其中的数据库的操作语句也不同,在Hibernate也要指定不同的配置文件,于是由

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

  这个语句来指定,其中支持绝大多数主流的数据库,具体支持情况如下表(来源截止至2016.9.6官方文档)

  Cache71  Support for the Caché database, version 2007.1   CUBRID  Support for the CUBRID database, version 8.3. May work with later versions.   DB2  Support for the DB2 database   DB2390  Support for DB2 Universal Database for OS/390, also known as DB2/390.   DB2400  Support for DB2 Universal Database for iSeries, also known as DB2/400.   DerbyTenFive  Support for the Derby database, version 10.5   DerbyTenSix  Support for the Derby database, version 10.6   DerbyTenSeven  Support for the Derby database, version 10.7   Firebird  Support for the Firebird database   FrontBase  Support for the Frontbase database   H2  Support for the H2 database   HSQL  Support for the HSQL (HyperSQL) database   Informix  Support for the Informix database   Ingres  Support for the Ingres database, version 9.2   Ingres9  Support for the Ingres database, version 9.3. May work with newer versions   Ingres10  Support for the Ingres database, version 10. May work with newer versions   Interbase  Support for the Interbase database.   JDataStore  Support for the JDataStore database   McKoi  Support for the McKoi database   Mimer  Support for the Mimer database, version 9.2.1. May work with newer versions   MySQL5  Support for the MySQL database, version 5.x   MySQL5InnoDB  Support for the MySQL database, version 5.x preferring the InnoDB storage engine when exporting tables.   MySQL57InnoDB  Support for the MySQL database, version 5.7 preferring the InnoDB storage engine when exporting tables. May work with newer versions   Oracle8i  Support for the Oracle database, version 8i   Oracle9i  Support for the Oracle database, version 9i   Oracle10g  Support for the Oracle database, version 10g   Pointbase  Support for the Pointbase database   PostgresPlus  Support for the Postgres Plus database   PostgreSQL81  Support for the PostgrSQL database, version 8.1   PostgreSQL82  Support for the PostgreSQL database, version 8.2   PostgreSQL9  Support for the PostgreSQL database, version 9. May work with later versions.   Progress  Support for the Progress database, version 9.1C. May work with newer versions.   SAPDB  Support for the SAPDB/MAXDB database.   SQLServer  Support for the SQL Server 2000 database   SQLServer2005  Support for the SQL Server 2005 database   SQLServer2008  Support for the SQL Server 2008 database   Sybase11  Support for the Sybase database, up to version 11.9.2   SybaseAnywhere  Support for the Sybase Anywhere database   SybaseASE15  Support for the Sybase Adaptive Server Enterprise database, version 15   SybaseASE157  Support for the Sybase Adaptive Server Enterprise database, version 15.7. May work with newer versions.   Teradata  Support for the Teradata database   TimesTen  Support for the TimesTen database, version 5.1. May work with newer versions

  具体用法为:

<property name="dialect">org.hibernate.dialect.(这里写对应数据库的名字)</property>

  显示sql语句(show_sql)   因为Hibernate隐藏了数据库具体语句的实现,所以我们当然也看不到隐藏的sql语句,通过设置这个属性,可以在控制台打印出正在使用的sql语句,帮助开发定位问题,当执行相关增删改查的操作的时候,相应的sql语句就会被打印出来

  最后再把使用的数据库驱动文件加进项目中(我用的是MySQL)

  Hibernate框架配置完成,接下来就可以开始写小的测试项目了。


评论