We say that Hibernate is an application specifically used for databases, and some packages are imported when Hibernate is downloaded. Some people think that even if it is usable, the mapping and core configuration have not yet been completed. It's like you've built a frame, but it's still missing the matching parts. Below we will explain each of these two aspects of Hibernate environment configuration .
1.hibernate mapping configuration
class tag, used to establish the relationship between classes and tables name: class name, table: table name
ID tag, the corresponding relationship between the attribute being established and the primary key in the table
property, establishes the corresponding relationship between the common properties in the class and the fields of the table
(1) First of all, we need to learn how to write a mapping configuration file. Everyone must know that the written mapping configuration file should be in the same package as the entity class, and the name should be class name.hbm.xml, so we need to create it in com.meimeixia Create a Customer.hbm.xml file under the .hibernate.demo01 package, but how should its constraints be written? You can find the hibernate-mapping-3.0.dtd file under the org.hibernate package of Hibernate's core jar package - hibernate-core-5.0.7.Final.jar.
(2) Copy and paste it into the Customer.hbm.xml file. The content of the Customer.hbm.xml file is first given here, but the content will not be introduced too much:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- Establish mapping between classes and tables --> <class name="com.meimeixia.hibernate.demo01.Customer" table="cst_customer"> <!-- Create attributes in the class corresponding to the primary key in the table --> <id name="cust_id" column="cust_id"> <!-- The primary key generation strategy will be discussed later. The local generation strategy is currently used --> <generator /> </id> <!-- Establish common attributes in the class to correspond to fields in the table --> <property name="cust_name" column="cust_name" /> <property name="cust_source" column="cust_source" /> <property name="cust_industry" column="cust_industry" /> <property name="cust_level" column="cust_level" /> <property name="cust_phone" column="cust_phone" /> <property name="cust_mobile" column="cust_mobile" /> </class> </hibernate-mapping>
2. hibernate core configuration
Necessary configuration: basic parameters for connecting to the database: driver class, URL path, user name, password, dialect configuration and introduction of mapping files
Optional configuration of displayed SQL statements, formatted SQL statements, and automatic table creation
(1) The core configuration file is mainly used by the Hibernate framework. It mainly contains information related to connecting to the database and related configurations of Hibernate. Now we need to learn how to write Hibernate's core configuration file. Everyone must also know that the written core configuration file should be in the src directory, and its name should be hibernate.cfg.xml, so we need to create a hibernate.cfg in the src directory. .xml file, but how should its constraints be written? You can also find the hibernate-configuration-3.0.dtd file under the org.hibernate package of Hibernate's core jar package - hibernate-core-5.0.7.Final.jar
(2) Then copy and paste it into the hibernate.cfg.xml file. How should it be configured in this file? We can refer to the hibernate-release-5.0.7.Finalprojectetchibernate.properties file. The content of the hibernate.cfg.xml file is given here first, but the content will not be introduced too much:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- The following are three must-have configurations --> <!-- Configure the basic parameters for connecting to the MySQL database --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_demo01</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">liayun</property> <!-- Configure Hibernate dialect --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- The following two configurations are optional! --> <!-- Print sql statement --> <property name="hibernate.show_sql">true</property> <!-- Format sql statement --> <property name="hibernate.format_sql">true</property> <!-- Tell Hibernate's core configuration file which mapping file to load --> <mapping resource="com/meimeixia/hibernate/demo01/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>
The above is the environment configuration of Hibernate in java. After reading this article, as long as you figure out one of the configuration methods, the other configuration will be similar. After everyone learns it, hurry up and configure it .