The Hibernate database connection information is loaded from the configuration file.
The configuration file of hibernate can be in either XML or properties.
1) hibernate. cfg. xml
In addition to the basic hibernate configuration information, you can also specify a persistence class ing file in the XML configuration file, which avoids hard coding of the persistence class configuration file in the program. The default file name of the configuration file in XML format is hibernate. cfg. xml. Location: src/hibernate. cfg. xml.
Example:
<? 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>
<! -- Display the executed SQL statement -->
<Property name = "show_ SQL"> true </property>
<! -- Connection string -->
<Property name = "connection. url"> JDBC: mysql: // localhost: 3306/STU </property>
<! -- Database connection username -->
<Property name = "connection. username"> root </property>
<! -- Database User Password -->
<Property name = "connection. Password"> root </property>
<! -- Database-driven -->
<Property name = "connection. driver_class"> com. MySQL. JDBC. Driver </property>
<! -- Select the dialect to use -->
<Property name = "dialect"> org. hibernate. dialect. mysqldialect </property>
<! -- Ing file -->
<Mapping Resource = "com/stuman/domain/admin. HBM. xml"/>
<! -- Ing file -->
<Mapping Resource = "com/stuman/domain/student. HBM. xml"/>
</Session-factory>
</Hibernate-configuration>
2) hibernate. Properties
The default file name of the configuration file in properties form is hibernate. properties. The configuration file contains a series of attribute configurations. hibernate will connect to the Database Based on these attributes. Location: src/hibernate. properties.
The configuration file is as follows:
# Specify the driver class used by the database
Hibernate. Connection. driver_class = com. MySQL. JDBC. Driver R
# Specifying database connection strings
Hibernate. Connection. url = JDBC: mysql: // localhost: 3306/DB
# Specify the database connection User Name
Hibernate. Connection. Username = user
# Specify the database connection password
Hibernate. Connection. Password = Password
# Specify the dialect used by the database
Hibernate. dialect = net. SF. hibernate. dialect. mysqldialect
# Specify whether to print SQL statements
Hibernate. show_ SQL = true
3)
Properties configuration files and xml configuration files can be used at the same time. When two types of configuration files are used at the same time, the settings in the XML configuration file overwrite the same properties in the properties configuration file.
Source: http://hanshuo528.bokee.com/viewdiary.23028288.html