hibernate入门笔记

2024-01-03 11:30:01

如何使用

导入依赖
<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配置文件

SessionFactory:针对单个数据库映射经过编译的内存镜像?件,将数据库转换为?个 Java 可以识别的镜像?件。
构建 SessionFactory ?常耗费资源,所以通常?个?程只需要创建?个 SessionFactory。

<?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&amp;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>

文章来源:https://blog.csdn.net/weixin_44609676/article/details/135358113
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。