Java学习之Hibernate框架使用
0x00 前言
以我看来Hibernate的使用频率其实还是比较可观的,但都说Hibernate比较笨重,这里来学习一波,做个 简单记录。
0x01 使用流程
流程
1、导⼊相关依赖2、创建 Hibernate 配置⽂件3、创建实体类4、创建实体类-关系映射⽂件5、调⽤ Hibernate API 完成操作
具体操作
<dependencies><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></dependencies>
- 配置hibernate.cfg.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><propertyname="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></session-factory></hibernate-configuration>
核⼼配置: session-factorySessionFactory:针对单个数据库映射经过编译的内存镜像⽂件,将数据库转换为⼀个 Java 可以识别的镜像⽂件。
构建 SessionFactory ⾮常耗费资源,所以通常⼀个⼯程只需要创建⼀个 SessionFactory 。
- 创建实体类
import lombok.Data;import java.util.Set;@Datapublic class Customer {private Integer id;private String name;private Set<Orders> orders;}
import lombok.Data;@Datapublic class Orders {private Integer id;private String name;private Customer customer;}
- 创建实体关系映射⽂件
import lombok.Data;@Datapublic class People {private Integer id;private String name;private Double money;}
<?xml version="1.0"?>5、实体关系映射⽂件注册到 Hibernate 的配置⽂件中。6、使⽤ Hibernate API 完成数据操作。<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.southwind.entity.People" table="people"><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><property name="money" type="java.lang.Double"><column name="money"></column></property></class></hibernate-mapping>
- 实体关系映射⽂件注册到 Hibernate 的配置⽂件中。
<mapping resource="com/nice0e3/entity/People.hbm.xml"></mapping>
- 使⽤ Hibernate API 完成数据操作
import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class Test {public static void main(String[] args) {//创建ConfigurationConfiguration configuration = newConfiguration().configure("hibernate.xml");//获取SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取SessionSession session = sessionFactory.openSession();People people = new People();people.setName("张三");people.setMoney(1000.0);session.save(people);session.beginTransaction().commit();session.close();}}
配置到映射文件配置到src目录下会读取不到。想要读取到需要在pom.xml下进行配置。
- pom.xml 中需要配置 resource。
<build><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources></build>
0x02 结尾
Hibernate并不是本次主要目的,这里用于做简单记录。