“Hibernate笔记 1:入门(开发步骤)”的版本间差异
跳到导航
跳到搜索
(→环境搭建) |
(→配置文件详解) |
||
第102行: | 第102行: | ||
== 配置文件详解 == | == 配置文件详解 == | ||
=== | 配置文件是 Hibernate 中相当重要的部分,对于 Hibernate、数据库、orm 等内容的都依赖于配置文件。 | ||
1、配置文件格式均为 '''xml'''; | |||
2、配置文件中均需要引入 '''dtd''' 约束; | |||
- (xml 的约束格式包括:dtd、schema,而 Hibernate 的配置文件中均使用 dtd) | |||
=== “映射关系”配置文件 === | |||
“映射关系”,即:实体类和数据库表一一对应的关系。 | “映射关系”,即:实体类和数据库表一一对应的关系。 | ||
关于“''映射关系''配置文件”: | |||
1、''文件名称'' 和 ''位置'' 没有固定要求; | |||
- 建议:在实体类所在包里面创建,名称为“<实体类名称>.hbm.xml”; | |||
“xxx.hbm.xml”格式: | |||
: <syntaxhighlight lang="xml" highlight=""> | |||
... | |||
<hibernate-mapping> | |||
# | |||
#* | <class name="XXX.XXX.XXX" table="ZZZ"> | ||
#* | |||
# | <id name="xxx" column="zzz"> | ||
#* | <generator = "???"></generator> | ||
# | </id> | ||
<property name="xxx" column="zzz" type="???"></property> | |||
<property name="xxx" column="zzz" type="???"></property> | |||
... | |||
</class> | |||
</hibernate-mapping> | |||
</syntaxhighlight> | |||
# <code>'''<class>'''</code> 用于配置“实体类”与“表”的对应; | |||
#* “name”属性值为“实体类的全路径”; | |||
# <code>'''<id>'''</code> 用于配置“(具有唯一性的)实体类的属性”与“(具有唯一性的)表的字段”的对应; | |||
#* “column”属性可以省略(与“name”一致); | |||
#* <code>'''<generator>'''</code> 用于配置主键的“增长方式”; | |||
#*# '''uuid'''; | |||
#*# '''native'''(自增); | |||
# <code>'''<property>'''</code> 用于配置“实体类的属性”与“表的字段”的对应; | |||
#* “column”属性可以省略(与“name”一致); | |||
#* “type”属性用于设置生成表字段的类型(自动对应类型); | |||
第123行: | 第156行: | ||
<class name="cn.itcast.entity.User" table="t_user"> | <class name="cn.itcast.entity.User" table="t_user"> | ||
<id name="uid" column="uid"> | <id name="uid" column="uid"> | ||
<generator = "uuid"></generator> | <generator = "uuid"></generator> | ||
</id> | </id> | ||
第132行: | 第163行: | ||
<property name="password" column="password"></property> | <property name="password" column="password"></property> | ||
<property name="address" column="address"></property> | <property name="address" column="address"></property> | ||
</class> | </class> | ||
</hibernate-mapping> | </hibernate-mapping> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Hibernate 核心配置文件 === | |||
Hibernate 核心配置文件,即“<span style="color: blue">'''hibernate.cfg.xml'''</span>”文件,其名称与位置均是固定的。 | Hibernate 核心配置文件,即“<span style="color: blue">'''hibernate.cfg.xml'''</span>”文件,其名称与位置均是固定的。 | ||
关于“Hibernate 核心配置文件”: | 关于“Hibernate 核心配置文件”: | ||
第150行: | 第181行: | ||
“hibernate.cfg.xml”格式: | “hibernate.cfg.xml”格式: | ||
: <syntaxhighlight lang="xml" highlight=""> | : <syntaxhighlight lang="xml" highlight=""> | ||
... | |||
<hibernate-configuration> | <hibernate-configuration> | ||
<session-factory> | <session-factory> | ||
第165行: | 第198行: | ||
</hibernate-configuration> | </hibernate-configuration> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 配置数据库信息: | # 配置数据库信息: | ||
#: <syntaxhighlight lang="xml" highlight=""> | #* 使用不同的数据库驱动(不同版本驱动)时,配置信息可能有差别。【可以从类似“mysql-connector”的包里去找相应内容】 | ||
#: 示例: | |||
## 使用“mysql-connector-java-5.0.4-bin.jar”时: | |||
##: <syntaxhighlight lang="xml" highlight=""> | |||
<property name="hibernate.connection.driver.class">com.mysql.jdbc.Driver</property> | |||
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property> | |||
<property name="hibernate.connection.username">root</property> | |||
<property name="hibernate.connection.password">admin</property> | |||
</syntaxhighlight> | |||
## 使用“mysql-connector-java-8.0.11.jar”时: | |||
##: <syntaxhighlight lang="xml" highlight=""> | |||
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> | |||
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day02?useSSL=false&serverTimezone=UTC</property> | |||
<property name="hibernate.connection.username">root</property> | |||
<property name="hibernate.connection.password">admin</property> | |||
</syntaxhighlight> | </syntaxhighlight> | ||
# 配置 Hibernate 信息: | # 配置 Hibernate 信息: | ||
#: <syntaxhighlight lang="xml" highlight=""> | #: <syntaxhighlight lang="xml" highlight=""> | ||
<!-- 输出底层 sql 语句 --> | |||
<property name="hibernate.show_sql">true</property> | |||
<!-- 输出底层 sql 语句格式 --> | |||
<property name="hibernate.format_sql">true</property> | |||
<!-- hibernate 创建表(update:无则创建,有则更新) --> | |||
<property name="hibernate.hbm2ddl.auto">update</property> | |||
<!-- 配置数据库方言(让 hibernate 框架识别不同数据库特有的方言) --> | |||
<property name="hibernate.dialect">org.hibernate.dialect.MySqlDialect</property> | |||
</syntaxhighlight> | </syntaxhighlight> | ||
# 配置“映射关系配置文件”: | # 配置“映射关系配置文件”: | ||
#* 需要定位到(需要加载的)“映射关系配置文件”所在包的位置。 | |||
#: 示例: | |||
#: <syntaxhighlight lang="xml" highlight=""> | #: <syntaxhighlight lang="xml" highlight=""> | ||
<mapping resource = "cn/itcast/entity/User.hbm.xml"/> | |||
</syntaxhighlight> | |||
== 核心 API == |
2022年6月7日 (二) 06:21的版本
入门
以下内容展示一个 Hibernate 项目的入门过程。
环境搭建
步骤:
- 导入相关 JAR 包;
- Hibernate 包;
- 日志包;(hibernate 本身没有日志输出相关的 jar 包)
- mysql 驱动包;
- 创建实体类;
- 使用 hibernate 时候,不需要自己手动创建表,hibernate 帮把表创建。
- 配置映射关系;(如:“User.hbm.xml”)
- 示例:
... <hibernate-mapping> <!-- 实体类和数据库表对应 --> <class name="cn.itcast.entity.User" table="t_user"> <id name="uid" column="uid"> <generator = "uuid"></generator> </id> <property name="username" column="username"></property> <property name="password" column="password"></property> <property name="address" column="address"></property> </class> </hibernate-mapping>
- 创建 Hibernate 核心配置文件(“hibernate.cfg.xml”);
- 示例:
... <hibernate-configuration> <session-factory> <!-- 数据库信息 --> <property name="hibernate.connection.driver.class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">admin</property> <!-- hibernate信息 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.dialect">org.hibernate.dialect.MySqlDialect</property> <!-- 映射文件 --> <mapping resource = "cn/itcast/entity/User.hbm.xml"/> <session-factory> </hibernate-configuration>
代码实现
步骤:
- 加载 hibernate 核心配置文件:
// 到src下面找到名称是hibernate.cfg.xml // 在hibernate里面封装对象 Configuration cfg = new Configuration(); cfg.configure();
- 创建 SessionFactory 对象:
// 读取 hibernate 核心配置文件内容,创建 sessionFactory // 在过程中,根据映射关系,在配置数据库里面把表创建 SessionFactory sessionFactory = cfg.buildSessionFactory();
- 使用 SessionFactory 创建 session 对象:
// 类似于连接 Session session = sessionFactory.openSession();
- 开启事务:
Transaction tx = session.beginTransaction();
- 写具体逻辑 crud 操作:
User user = new User(); user.setUsername("小马"); user.setPassword("123"); user.setAddress("美国"); session.save(user);
- 提交事务:
tx.commit();
- 关闭资源:
session.close(); sessionFactory.close();
配置文件详解
配置文件是 Hibernate 中相当重要的部分,对于 Hibernate、数据库、orm 等内容的都依赖于配置文件。 1、配置文件格式均为 xml; 2、配置文件中均需要引入 dtd 约束; - (xml 的约束格式包括:dtd、schema,而 Hibernate 的配置文件中均使用 dtd)
“映射关系”配置文件
“映射关系”,即:实体类和数据库表一一对应的关系。 关于“映射关系配置文件”: 1、文件名称 和 位置 没有固定要求; - 建议:在实体类所在包里面创建,名称为“<实体类名称>.hbm.xml”;
“xxx.hbm.xml”格式:
... <hibernate-mapping> <class name="XXX.XXX.XXX" table="ZZZ"> <id name="xxx" column="zzz"> <generator = "???"></generator> </id> <property name="xxx" column="zzz" type="???"></property> <property name="xxx" column="zzz" type="???"></property> ... </class> </hibernate-mapping>
<class>
用于配置“实体类”与“表”的对应;- “name”属性值为“实体类的全路径”;
<id>
用于配置“(具有唯一性的)实体类的属性”与“(具有唯一性的)表的字段”的对应;- “column”属性可以省略(与“name”一致);
<generator>
用于配置主键的“增长方式”;- uuid;
- native(自增);
<property>
用于配置“实体类的属性”与“表的字段”的对应;- “column”属性可以省略(与“name”一致);
- “type”属性用于设置生成表字段的类型(自动对应类型);
示例:“User.hbm.xml”
... <hibernate-mapping> <!-- 实体类和数据库表对应 --> <class name="cn.itcast.entity.User" table="t_user"> <id name="uid" column="uid"> <generator = "uuid"></generator> </id> <property name="username" column="username"></property> <property name="password" column="password"></property> <property name="address" column="address"></property> </class> </hibernate-mapping>
Hibernate 核心配置文件
Hibernate 核心配置文件,即“hibernate.cfg.xml”文件,其名称与位置均是固定的。
关于“Hibernate 核心配置文件”:
1、文件名称和位置固定:
- 位置:必须位于 src 下。
- 名称:必须为“hibernate.cfg.xml”。
2、同样需要引入 dtd 约束;
3、hibernate 操作过程中,只会加载核心配置文件,其他配置文件不会加载。
- “映射关系配置文件”需要配置到“核心配置文件”中,才会被加载。
“hibernate.cfg.xml”格式:
... <hibernate-configuration> <session-factory> <!-- 第一部分 数据库信息 --> ... <!-- 第二部分 hibernate信息 --> ... <!-- 第三部分 映射文件 --> <mapping resource="..."/> <session-factory> </hibernate-configuration>
- 配置数据库信息:
- 使用不同的数据库驱动(不同版本驱动)时,配置信息可能有差别。【可以从类似“mysql-connector”的包里去找相应内容】
- 示例:
- 使用“mysql-connector-java-5.0.4-bin.jar”时:
<property name="hibernate.connection.driver.class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">admin</property>
- 使用“mysql-connector-java-8.0.11.jar”时:
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day02?useSSL=false&serverTimezone=UTC</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">admin</property>
- 配置 Hibernate 信息:
<!-- 输出底层 sql 语句 --> <property name="hibernate.show_sql">true</property> <!-- 输出底层 sql 语句格式 --> <property name="hibernate.format_sql">true</property> <!-- hibernate 创建表(update:无则创建,有则更新) --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置数据库方言(让 hibernate 框架识别不同数据库特有的方言) --> <property name="hibernate.dialect">org.hibernate.dialect.MySqlDialect</property>
- 配置“映射关系配置文件”:
- 需要定位到(需要加载的)“映射关系配置文件”所在包的位置。
- 示例:
<mapping resource = "cn/itcast/entity/User.hbm.xml"/>