1.导入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.10.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
2.编写hibernate.xml文件
<?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>
<!-- 数据源配置 -->
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</property>
<!-- C3P0 -->
<property name="hibernate.c3p0.acquire_increment">10</property>
<property name="hibernate.c3p0.idle_test_period">10000</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_size">30</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_statements">10</property>
<!-- 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 打印SQL -->
<property name="show_sql">true</property>
<!-- 格式化SQL -->
<property name="format_sql">true</property>
<!-- 是否自动生成数据库 -->
<property name="hibernate.hbm2ddl.auto"></property>
<!-- 注册实体关系映射文件 -->
<mapping resource="com/southwind/entity/People.hbm.xml"></mapping>
<mapping resource="com/southwind/entity/Customer.hbm.xml"></mapping>
<mapping resource="com/southwind/entity/Orders.hbm.xml"></mapping>
<mapping resource="com/southwind/entity/Account.hbm.xml"></mapping>
<mapping resource="com/southwind/entity/Course.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
3.编写Orders类
package com.southwind.entity;
import lombok.Data;
@Data
public class Orders {
private Integer id;
private String name;
//一个订单只对应一个顾客
private Customer customer;
}
4.编写Orders.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.southwind.entity.Orders" table="orders">
<id name="id" type="java.lang.Integer">
<column name="id"></column>
<generator class="identity"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name"></column>
</property>
<many-to-one name="customer" class="com.southwind.entity.Customer" column="cid"></many-to-one>
</class>
</hibernate-mapping>
注:
1.class中的name属性是映射类的全类名,table对应的是数据库中的表名
2.id中的name是主键对应的属性,type对应的是该属性的类型。 column中的name属性对应的是数据库表中的属性。
3.property中的name值对应的是映射类的属性, column中的name属性是与其对应的是数据库表中的属性。
4.many-to-one是多对一映射,即一个顾客可以有多个订单。
5.编写customer类
package com.southwind.entity;
import lombok.Data;
import java.util.Set;
@Data
public class Customer {
private Integer id;
private String name;
//一个顾客可以有多个订单
private Set<Orders> orders;
}
6.编写Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.southwind.entity.Customer" table="customer">
<id name="id" type="java.lang.Integer">
<column name="id"></column>
<generator class="identity"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name"></column>
</property>
<set name="orders" table="orders" lazy="extra">
<key column="cid"></key>
<one-to-many class="com.southwind.entity.Orders"></one-to-many>
</set>
</class>
</hibernate-mapping>
注:
1.set属性主要用于一对多的映射关系中,其中从顾客的角度来说,订单可以多个,故在顾客中类中用Set集合作为属性,key属性代表orders表中的外键,one-to-many属性代表一对多的关系,从顾客的角度来说订单可以多个,与many-to-one相对应。
7.测试
package com.southwind.test;
import com.southwind.entity.Customer;
import com.southwind.entity.Orders;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Test2 {
public static void main(String[] args) {
//创建 Configuration
Configuration configuration = new Configuration().configure("hibernate.xml");
//获取 SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
//获取 Session
Session session = sessionFactory.openSession();
//创建 Customer
Customer customer = new Customer();
customer.setName("张三");
//创建 Orders
Orders orders = new Orders();
orders.setName("订单1");
//建立关联关系
orders.setCustomer(customer);
//保存
session.save(customer);
session.save(orders);
//提交事务
session.beginTransaction().commit();
//关闭session
session.close();
}
}