Example tutorial of configuration files in Hibernate
First of all, let’s take a look at hibernate’s main configuration file
1 nbsp;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>com.mysql.jdbc.Driver</property>11 <property>jdbc:mysql:///day17</property>12 <property>root</property>13 <property>root</property>14 <!-- 15 数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql16 -->17 <property>org.hibernate.dialect.MySQL5Dialect</property>18 19 20 <!-- 2. 其他相关配置 -->21 <!-- 2.1 显示hibernate在运行时候执行的sql语句 -->22 <property>true</property>23 <!-- 2.2 格式化sql -->24 <property>true</property>25 <!-- 2.3 自动建表 -->26 <property>update</property>27 28 29 <!-- 3. 加载所有映射 -->30 <mapping></mapping>31 32 </session-factory>33 </hibernate-configuration>
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></mapping> 这是添加一个映射文件,意思就是你要使用的数据库中的表 映射文件为:
<span style="color: #0000ff"></span><span style="color: #ff00ff">xml version="1.0"</span><span style="color: #0000ff">?></span><span style="color: #0000ff"><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 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 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 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 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 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 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></span></span></span></span></span></span></span>
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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software