在反转之前,首先需要将HibernateSpring配置好(并非必须,后面会提到),需要在applicationContext.xml文件中配置以下内容:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver">
        </property>
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3306/zq?useUnicode=true&amp;characterEncoding=UTF-8">
        </property>
        <property name="username" value="root"></property>
        <property name="password" value=""></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="show_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <!-- 在反转之后,将所有表的Hibernate配置*.hbm.xml导入 -->
                <!-- 如<value>com/jsu/po/Area.hbm.xml</value> -->
            </list>
        </property>
    </bean>
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="hibernateTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>


上面是常用的Spring配置内容,但如果想要先测试反转的效果,最少只需要配置好sessionFactory就可以了。

配置好Spring之后,我们来利用Hibernate进行数据库反转。使用的是MyEclipse9


   第一步:打开MyEclipse Hibernate Perspective

第二步:在DB Browser窗口空白处单击右键->New,弹出Database Driver对话框,依次填入信息,如下图。

[Driver template:驱动模型(笔者选择的是MySQL

Driver name:驱动名称,显示在DB Browser窗口中的名称

Connection URL:数据库连接字符串

User name:数据库用户名

Password:对应用户的密码

Driver JARsAdd JARs->导入连接DBjar包即可]

手把手教你在MyEclipse下反转数据库生成Hibernate配置

信息填完后,点击Test Driver测试是否成功连接。连接成功如下图:

手把手教你在MyEclipse下反转数据库生成Hibernate配置

点击Finish,即可在DB Browser看到新建的连接:

手把手教你在MyEclipse下反转数据库生成Hibernate配置

第三步:选中所有需要反转的数据表,右键选择Hibernate Reverse Engineering,弹出相应对话框,按照下面这样选择:

手把手教你在MyEclipse下反转数据库生成Hibernate配置

   文章开头提到需要配置Spring,这并非是必需的。如果正确配置了Spring,上面的DAO type就能够选择生成Spring DAO,下面的Spring config fileSessionFactory Id才可用。这样子反转之后,所有表的Hibernate配置文件(*.hbm.xml)都能够自动插入到applicationContext.xml中的对应位置。但如果没有配置Spring,那么DAO type中的Spring DAO就不可选。这样子的话生成的DAO会与Spring DAO有所区别,这个笔者不太熟悉。

   另一个需要注意的是,上边的Java package的包路径是保存反转后生成的PODAO以及.hbm.xml文件的地方。如果表太多,注意生成后的文件数目会比较大,不要同已有文件冲突。

   点击FinishMyEclipse此时可能会警告存放包路径下有重名,确认无误后确定即可自动反转。看具体开发环境,快的话几秒钟时间就能够完成。

   此时切换回MyEclipse Java Enterprise Perspective视图,打开存放包下面就能够看到生成好的PODAO.hbm.xml配置文件了!

   因为在第三步选择了Spring DAO,所以MyEclipse还会帮你在applicationContext.xml中自动创建好每个DAO的对应bean标签,真是太方便了~