Home Java javaTutorial Example tutorial of configuration files in Hibernate

Example tutorial of configuration files in Hibernate

Jun 25, 2017 pm 01:28 PM
hibernate study Configuration file

First of all, let’s take a look at hibernate’s main configuration file

 1 <!DOCTYPE hibernate-configuration PUBLIC 2     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 3     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 4  5 <hibernate-configuration> 6     <!-- 通常,一个session-factory节点代表一个数据库 --> 7     <session-factory> 8      9         <!-- 1. 数据库连接配置 -->10         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>11         <property name="hibernate.connection.url">jdbc:mysql:///day17</property>12         <property name="hibernate.connection.username">root</property>13         <property name="hibernate.connection.password">root</property>14         <!-- 15             数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql16          -->17         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>18         19         20         <!-- 2. 其他相关配置 -->21         <!-- 2.1 显示hibernate在运行时候执行的sql语句 -->22         <property name="hibernate.show_sql">true</property>23         <!-- 2.2 格式化sql -->24         <property name="hibernate.format_sql">true</property>25         <!-- 2.3 自动建表  -->26         <property name="hibernate.hbm2ddl.auto">update</property>27         28         29         <!-- 3. 加载所有映射 -->30         <mapping resource="cn/itcast/entity/Employee.hbm.xml"/>31         32     </session-factory>33 </hibernate-configuration>
Copy after login

The main codes in it are all commented out, and everyone will understand them at a glance. In the nearest part of the xml file, we see There is a code:

 <!-- 3. 加载所有映射 -->
3<mapping resource="cn/itcast/entity/Employee.hbm.xml"/>
这是添加一个映射文件,意思就是你要使用的数据库中的表
映射文件为:
Copy after login
<span style="color: #0000ff"><?</span><span style="color: #ff00ff">xml version="1.0"</span><span style="color: #0000ff">?></span><span style="color: #0000ff"><!</span><span style="color: #ff00ff">DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">hibernate-mapping </span><span style="color: #ff0000">package</span><span style="color: #0000ff">="cn.itcast.entity"</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">class </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="Employee"</span><span style="color: #ff0000"> table</span><span style="color: #0000ff">="employee"</span><span style="color: #0000ff">></span><span style="color: #008000"><!--</span><span style="color: #008000"> 主键 ,映射</span><span style="color: #008000">--></span><span style="color: #0000ff"><</span><span style="color: #800000">id </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="empId"</span><span style="color: #ff0000"> column</span><span style="color: #0000ff">="id"</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">generator </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="native"</span><span style="color: #0000ff">/></span><span style="color: #0000ff"></</span><span style="color: #800000">id</span><span style="color: #0000ff">></span><span style="color: #008000"><!--</span><span style="color: #008000"> 非主键,映射 </span><span style="color: #008000">--></span><span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="empName"</span><span style="color: #ff0000"> column</span><span style="color: #0000ff">="name"</span><span style="color: #0000ff">></</span><span style="color: #800000">property</span><span style="color: #0000ff">></span><span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="workDate"</span><span style="color: #ff0000"> column</span><span style="color: #0000ff">="workDate"</span><span style="color: #0000ff">></</span><span style="color: #800000">property</span><span style="color: #0000ff">></span><span style="color: #0000ff"></</span><span style="color: #800000">class</span><span style="color: #0000ff">></span><span style="color: #0000ff"></</span><span style="color: #800000">hibernate-mapping</span><span style="color: #0000ff">><br><br></span>
Copy after login

This configuration file is associated with an entity class Employee.java. The value in name is the attribute in the class, and the value in column is the database table employee. The field names in are related through mapping.

The corresponding attribute description in the entity class is:

The corresponding field of the database table employee is:

Associated through mapping files.

As mentioned above, it is a simple hibernate configuration process. If novice readers want to learn, you can just change the part in the above file. The main configuration file mainly involves the database connection, including Database driver, connected database name, database username and password, and the following mapping file to be loaded.

Regarding the modification of the mapping file, you can modify it according to my example above and then apply it to your own example.

If you have any questions, please comment below.

The above is the detailed content of Example tutorial of configuration files in Hibernate. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Learn to completely uninstall pip and use Python more efficiently Learn to completely uninstall pip and use Python more efficiently Jan 16, 2024 am 09:01 AM

No more need for pip? Come and learn how to uninstall pip effectively! Introduction: pip is one of Python's package management tools, which can easily install, upgrade and uninstall Python packages. However, sometimes we may need to uninstall pip, perhaps because we wish to use another package management tool, or because we need to completely clear the Python environment. This article will explain how to uninstall pip efficiently and provide specific code examples. 1. How to uninstall pip The following will introduce two common methods of uninstalling pip.

Where is the win10 user profile? How to set the user profile in Win10 Where is the win10 user profile? How to set the user profile in Win10 Jun 25, 2024 pm 05:55 PM

Recently, many Win10 system users want to change the user profile, but they don’t know how to do it. This article will show you how to set the user profile in Win10 system! How to set up user profile in Win10 1. First, press the "Win+I" keys to open the settings interface, and click to enter the "System" settings. 2. Then, in the opened interface, click "About" on the left, then find and click "Advanced System Settings". 3. Then, in the pop-up window, switch to the "" option bar and click "User Configuration" below.

Revealing the appeal of C language: Uncovering the potential of programmers Revealing the appeal of C language: Uncovering the potential of programmers Feb 24, 2024 pm 11:21 PM

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Feb 19, 2024 pm 10:10 PM

Learn Pygame from scratch: complete installation and configuration tutorial, specific code examples required Introduction: Pygame is an open source game development library developed using the Python programming language. It provides a wealth of functions and tools, allowing developers to easily create a variety of type of game. This article will help you learn Pygame from scratch, and provide a complete installation and configuration tutorial, as well as specific code examples to get you started quickly. Part One: Installing Python and Pygame First, make sure you have

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

Install Helm on Ubuntu Install Helm on Ubuntu Mar 20, 2024 pm 06:41 PM

Helm is an important component of Kubernetes that simplifies the deployment of Kubernetes applications by bundling configuration files into a package called HelmChart. This approach makes updating a single configuration file more convenient than modifying multiple files. With Helm, users can easily deploy Kubernetes applications, simplifying the entire deployment process and improving efficiency. In this guide, I'll cover different ways to implement Helm on Ubuntu. Please note: The commands in the following guide apply to Ubuntu 22.04 as well as all Ubuntu versions and Debian-based distributions. These commands are tested and should work correctly on your system. in U

Learn the main function in Go language from scratch Learn the main function in Go language from scratch Mar 27, 2024 pm 05:03 PM

Title: Learn the main function in Go language from scratch. As a simple and efficient programming language, Go language is favored by developers. In the Go language, the main function is an entry function, and every Go program must contain the main function as the entry point of the program. This article will introduce how to learn the main function in Go language from scratch and provide specific code examples. 1. First, we need to install the Go language development environment. You can go to the official website (https://golang.org

Quickly learn pip installation and master the skills from scratch Quickly learn pip installation and master the skills from scratch Jan 16, 2024 am 10:30 AM

Learn pip installation from scratch and quickly master the skills. Specific code examples are required. Overview: pip is a Python package management tool that can easily install, upgrade and manage Python packages. For Python developers, it is very important to master the skills of using pip. This article will introduce the installation method of pip from scratch, and give some practical tips and specific code examples to help readers quickly master the use of pip. 1. Install pip Before using pip, you first need to install pip. pip

See all articles