2017-07-18 39 views
0

我把我的java web應用程序打包成一個包含所有依賴關係和其他東西的zip文件..使用maven assembly插件,我寫了一個批處理腳本以部署war和運行它沒有打開日食...從戰爭外部化hibernate.cfg.xml maven

這裏的問題是,我想運行這個批處理文件其他計算機的數據庫,在這裏我需要你的幫助告訴我,如果有一種方法來外化hibernate.cfg .xml直接配置我的應用程序將鏈接到的數據庫。

在此先感謝

回答

0

我發現,我必須指定連接屬性(用戶名,密碼,connection.url,方言...)在hibernate.properties。 而類映射必須保留在hibernate.cfg.xml文件中。通過將它們無論是在resources 而且,Hibernate會找到他們倆,並提取他所需要的:d

0

需要配置property文件外部化hibernate.cfg.xml。創建hibernate.properties到類路徑中,併爲hibernate的配置設置變量和值。你可以參考波紋管的示例代碼吧:

的hibernate.cfg.xml:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="hibernateProperties"> 
     <map> 
      <entry key="connection.driver_class" value="${hibernate.connection.driver_class}" /> 
      <entry key="connection.username" value="${hibernate.connection.username}" /> 
      <entry key="connection.password" value="${hibernate.connection.password}" /> 
      <entry key="transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" /> 
     </map> 
    <property> 
</bean> 

hibernate.properties:

hibernate.connection.driver_class = com.mysql.jdbc.Driver 
hibernate.connection.url = jdbc:mysql://localhost:3306/test 
hibernate.connection.username = root 
hibernate.connection.password = root 
hibernate.dialect = org.hibernate.dialect.MySQLDialect 
hibernate.current_session_context_class=thread 
+0

有沒有辦法做到這一點,而不包括春天?或者它是強制性的? – ZiOS

+0

不,不是強制性的。並適當的配置,你可以參考這個鏈接:https://stackoverflow.com/questions/45173127/externalize-hibernate-cfg-xml-from-war-maven/45173364?noredirect = 1#comment77316235_45173364,在這個鏈接,你可以得到konw關於配置。將此配置用於屬性文件。 – Sharma