2017-07-18 194 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