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>
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"/> 这是添加一个映射文件,意思就是你要使用的数据库中的表 映射文件为:
<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>
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!