简体   繁体   English

Hibernate将数据库更改从oracle更改为mysql

[英]Hibernate changes database change from oracle to mysql

I am using Spring and Hibernate in my project. 我在项目中使用SpringHibernate My database has changed from Oracle 10g to MySQL . 我的数据库已从Oracle 10g更改为MySQL

Can you please tell me what changes do I need to make in my Hibernate configuration? 您能告诉我我需要在Hibernate配置中进行哪些更改吗?

Also are there any changes required in my Java code. 我的Java代码中也需要进行任何更改。

If you are using the hibernate.cfg.xml for your project to define the database property, then you need to change it to the below values for MySQL :- 如果您在项目中使用hibernate.cfg.xml定义数据库属性,则需要将其更改为MySQL的以下值:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/{db.name}</property>
        <property name="hibernate.connection.username">{db.name}</property>
        <property name="hibernate.connection.password">{db.password}</property>

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

Also you need to include the mysql-connector jar in your pom.xml for example 另外,例如,您还需要在pom.xml中包含mysql-connector jar

  <!--Mysql-Connector-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>

if you are not using the hibernate.cfg.xml and instead using some sessionFactory bean in your Spring ApplicationContext.xml . 如果您不使用hibernate.cfg.xml,而是在Spring ApplicationContext.xml使用一些sessionFactory bean。 Then you need to change it to below :- 然后,您需要将其更改为以下内容:

<bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="annotatedClasses">

        </property>
        <property name="hibernateProperties">
            <props>
                <!-- As of now not using hibernate.cfg.xml -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.validator.apply_to_ddl">false</prop>
                <prop key="hibernate.validator.autoregister_listeners">false</prop>
                <prop key="show_sql">true</prop>
                <prop key="format_sql">true</prop>
            </props>
        </property>
    </bean>

<bean id="dataSource"
    class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
      <property name="driverClassName" value="${jdbc.driverClassName}" />
      <property name="url" value="${jdbc.url}" />
      <property name="username" value="${jdbc.user}" />
      <property name="password" value="${jdbc.pass}" />
   </bean>

Please follow http://www.baeldung.com/hibernate-4-spring for complete example on how to setup hibernate with spring. 请参阅http://www.baeldung.com/hibernate-4-spring,以获取有关如何通过spring设置休眠模式的完整示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM