鍍金池/ 教程/ Java/ Hibernate與Spring 配合生成表結(jié)構(gòu)
Spring 事務(wù)管理
Struts2 上傳文件
Hibernate 對(duì)象的三種狀態(tài)
Struts2 攔截器
Hibernate與Spring 配合生成表結(jié)構(gòu)
Struts2 內(nèi)部是如何工作的
Spring 容器IOC解析及簡(jiǎn)單實(shí)現(xiàn)
Hibernate動(dòng)態(tài)模型+JRebel實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建表
Hibernate 核心接口
Hibernate——Session之save()方法
提高用戶體驗(yàn)之404處理
Struts2 國(guó)際化自動(dòng)檢測(cè)瀏覽器語(yǔ)言版
Struts2 國(guó)際化手動(dòng)切換版
Spring jar包詳解
Spring 容器AOP的實(shí)現(xiàn)原理——?jiǎng)討B(tài)代理
Struts實(shí)現(xiàn)簡(jiǎn)單登錄(附源碼)
Hibernate之SchemaExport+配置文件生成表結(jié)構(gòu)
基于注解的SSH將配置精簡(jiǎn)到極致
簡(jiǎn)單模擬Hibernate實(shí)現(xiàn)原理

Hibernate與Spring 配合生成表結(jié)構(gòu)

前幾天向大家介紹了一種用工具類生成數(shù)據(jù)表的方法,不過(guò)之前的方法需要使用一個(gè)跟項(xiàng)目關(guān)系不大的工具類。不免讓人覺(jué)得有些多余,所以呢,今天再向大家介紹一種方法。即Hibernate與Spring配合生成表結(jié)構(gòu)。

首先,要將Spring的信息配置的web.xml,配置Spring用于初始化容器對(duì)象的監(jiān)聽器。

web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  <display-name>oa_01</display-name>  

  <!-- 配置Spring用于初始化容器對(duì)象的監(jiān)聽器 -->  
  <listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener>  
  <context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath:applicationContext*.xml</param-value>  
  </context-param>  

  <welcome-file-list>  
    <welcome-file>index.jsp</welcome-file>  
  </welcome-file-list>  
</web-app>  

然后,將Hibernate的信息配置到Spring的配置文件中,將Hibernate交由Spring來(lái)管理。

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:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  

    <!-- 自動(dòng)掃描與裝配bean -->  
    <context:component-scan base-package="com.tgb.oa"></context:component-scan>  

    <!-- 導(dǎo)入外部的properties文件 -->  
    <context:property-placeholder location="classpath:jdbc.properties"/>  

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        <!-- 指定hibernate配置文件的位置 -->  
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>  
        <!-- 配置c3p0數(shù)據(jù)庫(kù)連接池 -->  
        <property name="dataSource">  
            <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">  
                <!-- 數(shù)據(jù)連接信息 -->  
                <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3307/myoa"></property>  
                <property name="driverClass" value="com.mysql.jdbc.Driver"></property>  
                <property name="user" value="root"></property>  
                <property name="password" value="123456"></property>  

                <!-- 初始化時(shí)獲取三個(gè)連接(取值應(yīng)在minPoolSize與maxPoolSize之間。默認(rèn)值: 3) -->  
                <property name="initialPoolSize" value="3"></property>  
                <!-- 連接池中保留的最小連接數(shù),默認(rèn)值:3 -->  
                <property name="minPoolSize" value="3"></property>  
                <!-- 連接池中保留的最大連接數(shù),默認(rèn)值:15 -->  
                <property name="maxPoolSize" value="5"></property>  
                <!-- 當(dāng)連接池中的連接數(shù)耗盡的時(shí)候,c3p0一次同時(shí)獲取的連接數(shù),默認(rèn)值:3 -->  
                <property name="acquireIncrement" value="3"></property>  
                <!-- 控制數(shù)據(jù)源內(nèi)加載的PreparedStatements數(shù)量。如果maxStatements與maxStatementsPerConnection均為0,則緩存被關(guān)閉。Default: 0 -->  
                <property name="maxStatements" value="8"></property>  
                <!--maxStatementsPerConnection定義了連接池內(nèi)單個(gè)連接所擁有的最大緩存statements數(shù)。Default: 0 -->  
                <property name="maxStatementsPerConnection" value="5"></property>  
                <!--最大空閑時(shí)間,1800秒內(nèi)未使用則連接被丟棄。若為0則永不丟棄。Default: 0 -->  
                <property name="maxIdleTime" value="1800"></property>  
            </bean>  
        </property>         
    </bean>  

</beans>  

這里我將數(shù)據(jù)庫(kù)連接信息以及連接池都配置到了Spring中,當(dāng)然你也可以將數(shù)據(jù)庫(kù)連接信息寫到Hibernate的配置文件里,Hibernate會(huì)有自己默認(rèn)的連接池配置,但是它沒(méi)有Spring的好用。具體寫到哪看你需要吧。

接下來(lái)就是Hibernate的配置了,里面包括數(shù)據(jù)庫(kù)方言、是否顯示sql語(yǔ)句、更新方式以及實(shí)體映射文件。當(dāng)然也可按上面說(shuō)的將數(shù)據(jù)庫(kù)連接信息寫到里面。

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">  

<hibernate-configuration>  
    <session-factory>  

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

        <property name="show_sql">true</property>  
        <property name="hbm2ddl.auto">update</property>  

        <mapping resource="com/tgb/oa/domain/User.hbm.xml"/>  

    </session-factory>  
</hibernate-configuration>  

實(shí)體映射文件,不做過(guò)多解釋。

User.hbm.xml

<?xml version="1.0" <span style="font-family: Arial, Helvetica, sans-serif;">encoding="UTF-8"?</span>>  
<!DOCTYPE hibernate-mapping PUBLIC  
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

<hibernate-mapping package="com.tgb.oa.domain">  

    <class name="User" table="T_User">  
        <id name="id">  
            <generator class="native"/>  
        </id>  
        <property name="name" />  
    </class>  

</hibernate-mapping>  

實(shí)體類 User.java

package com.tgb.oa.domain;  

public class User {  

    private String name;  
    private  Long id;  

    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public Long getId() {  
        return id;  
    }  
    public void setId(Long id) {  
        this.id = id;  
    }  

}  

當(dāng)tomcat啟動(dòng)的時(shí)候,會(huì)先找到web.xml,然后根據(jù)web.xml的配置,會(huì)找到spring中的applicationContext.xml的配置文件,在applicationContext.xml中有相應(yīng)的SessionFactory的配置,里面有Hibernate的相關(guān)信息,接著就會(huì)找到Hibernate-cfg.xml,讀取該文件,并找到實(shí)體映射文件User.hbm.xml,最后根據(jù)User.hbm.xml的配置映射成相應(yīng)的表結(jié)構(gòu)。

Tomcat啟動(dòng)以后,表結(jié)構(gòu)也跟著生成了,生成表結(jié)構(gòu)后的效果:

http://wiki.jikexueyuan.com/project/ssh-noob-learning/images/12.1.jpeg" alt="" />

兩種生成表結(jié)構(gòu)的方式其實(shí)也沒(méi)有哪種好,哪種不好之說(shuō)。用工具類生成的方式不需要Spring的參與,但是需要一個(gè)工具類來(lái)支持;與Spring配合的方式不需要多余的東西,但是需要與Spring配合才能用。如果你只需要Hibernate那就用第一種,如果正好是配合Spring來(lái)使用那毫無(wú)疑問(wèn)就用第二種。具體看情況吧。