Hibernate Configuration Files for Connecting Three Databases

  • 2021-07-13 05:20:50
  • OfStack

The configuration file for Hibernate to connect to the database is hibernate. cfg. xml, and the necessary configuration for hibernate. cfg. xml to connect to three databases (SQL, Server, Oracle, MySQL) is listed below.

Configuration of Connecting MySql


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!--  Configuration database driver  -->
    <property name="connection.driver_class">
      com.mysql.jdbc.Driver
    </property>
    <!--  Configure database connection URL -->
    <property name="connection.url">
      jdbc:mysql://localhost:3306/mysqldb
    </property>
    <!--  Database user -->
    <property name="connection.username">root</property>
    <!--  Database user Password  -->
    <property name="connection.password">admin</property>
    <!--  Configure JDBC Built-in connection pool  -->
    <property name="connection.pool_size">1</property>
    <!--  Configuration database dialect  -->
    <property name="dialect">
      org.hibernate.dialect.MySQLDialect
    </property>
    <!--  Output runtime generated SQL Statement  -->
    <property name="show_sql">true</property>
    <!--  Configure the SQL Statement is formatted  -->
    <property name="format_sql">true</property>
    <!--  Configuration mapping file  -->
    <mapping resource="com/model/User.hbm.xml" />
  </session-factory>
</hibernate-configuration>

Configuration of Connecting Oracle


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!--  Driver classes for the configuration database  -->
    <property name="connection.driver_class">
      oracle.jdbc.driver.OracleDriver
    </property>
    <!--  Configure the connection path of the database  -->
    <property name="connection.url">
      jdbc:oracle:thin:@127.0.0.1:1521:DBSQL
    </property>
    <!--  Configure the connection user name of the database  -->
    <property name="connection.username">PERSONNEL_MANAGE</property>
    <!--  Configure the connection password of the database. If the password is blank, you can also omit the configuration code of this line  -->
    <property name="connection.password">MWQ</property>
    <!--  Dialects used by the configuration database  -->
    <property name="dialect">
      org.hibernate.dialect.OracleDialect
    </property>
    <!--  Configure to display on the console SQL Statement  -->
    <property name="show_sql">true</property>
    <!--  Configure the SQL Statement is formatted  -->
    <property name="format_sql">true</property>
    <!--  Configured in the output SQL Add a prompt before the statement  -->
    <property name="use_sql_comments">true</property>
    <!--  Configure the Persistence Class Mapping File  -->
    <mapping resource="com/chen/entity/users.hbm.xml" />
  </session-factory>
</hibernate-configuration>

Connecting SQL Server Configuration


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
   <session-factory>
   <!-- Configuration database JDBC Drive -->
   <property name="hibernate.connection.url">jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=db_manpower</property>
   <!-- Configure database connection URL-->
   <property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
   <!-- Configuration database user name -->
   <property name="hibernate.connection.username">sa</property>
   <!-- Configure database password -->
   <property name="hibernate.connection.password"/>
   <!-- Output runtime generated SQL Statement -->
   <property name="show_sql">ture</property>
   <!-- Configuration database dialect -->
   <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
   <!-- Configure the number of connection pools -->
   <property name="hibernate.jdbc.batch_size">16</property>
   <!-- List mapping files -->
   <mapping resource="com/chen/entity/Users.hbm.xml"/>
   </session-factory>
 </hibernate-configuration>

Summarize


Related articles: