以我看来Hibernate的使用频率其实还是比较可观的,但都说Hibernate比较笨重,这里来学习一波,做个 简单记录。
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>
<?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> </session-factory> </hibernate-configuration>
核⼼配置: session-factory
SessionFactory:针对单个数据库映射经过编译的内存镜像⽂件,将数据库转换为⼀个 Java 可以识别的
镜像⽂件。
构建 SessionFactory ⾮常耗费资源,所以通常⼀个⼯程只需要创建⼀个 SessionFactory 。
import lombok.Data; import java.util.Set; @Data public class Customer { private Integer id; private String name; private Set<Orders> orders; }
import lombok.Data; @Data public class Orders { private Integer id; private String name; private Customer customer; }
import lombok.Data; @Data public 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 DTD 3.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>
<mapping resource="com/nice0e3/entity/People.hbm.xml"></mapping>
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Test { public static void main(String[] args) { //创建Configuration Configuration configuration = new Configuration().configure("hibernate.xml"); //获取SessionFactory SessionFactory sessionFactory = configuration.buildSessionFactory(); //获取Session Session session = sessionFactory.openSession(); People people = new People(); people.setName("张三"); people.setMoney(1000.0); session.save(people); session.beginTransaction().commit(); session.close(); } }
配置到映射文件配置到src目录下会读取不到。想要读取到需要在pom.xml下进行配置。
<build><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources></build>
Hibernate并不是本次主要目的,这里用于做简单记录。