“Hibernate笔记 1:入门(开发步骤)”的版本间差异
跳到导航
跳到搜索
(创建页面,内容为“category:Hibernate == 入门 == 以下内容展示一个 Hibernate 项目的入门过程。 === 环境搭建 === ---- 步骤: # 导入相关 JAR 包; ## Hibernate 包; ## 日志包;(hibernate 本身没有日志输出相关的 jar 包) ## mysql 驱动包; # 创建实体类; #* 使用 hibernate 时候,不需要自己手动创建表,hibernate 帮把表创建。 # '''配置映射关系''';(如:“User.hbm.xml”) #* 名称和位…”) |
(→环境搭建) |
||
(未显示同一用户的33个中间版本) | |||
第5行: | 第5行: | ||
=== 环境搭建 === | === 环境搭建 === | ||
步骤: | 步骤: | ||
# 导入相关 JAR 包; | # 导入相关 JAR 包; | ||
## Hibernate | ## Hibernate 依赖包:(位于 Hibernate 源文件中:Hibernate/lib/required/) | ||
## | ##: [[File:Hibernate:JAR包:Hibernate依赖包.png|400px]] | ||
## mysql | ## 日志相关包:(hibernate 本身没有日志输出相关的 jar 包) | ||
# | ##: [[File:Hibernate:JAR包:日志相关包.png|400px]] | ||
#* | ## mysql 驱动包: | ||
##: [[File:Hibernate:JAR包:数据库驱动包.png|400px]] | |||
# '''创建持久化类'''(实体类); | |||
#* 见:<big>'''[[Hibernate笔记_4:核心知识#持久化类的编写规则]]'''</big> | |||
# 创建数据表; | |||
#* 如果配置“hibernate.hbm2ddl.auto”为“update”,则不需要自己手动创建表。 | |||
# '''配置映射关系''';(如:“User.hbm.xml”) | # '''配置映射关系''';(如:“User.hbm.xml”) | ||
# | #: 示例: | ||
#: <syntaxhighlight lang="xml" highlight=""> | |||
# | |||
: <syntaxhighlight lang="xml" highlight=""> | |||
... | ... | ||
<hibernate-mapping> | <hibernate-mapping> | ||
<!-- 实体类和数据库表对应 --> | <!-- 实体类和数据库表对应 --> | ||
<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> | ||
<property name="username" column="username"></property> | <property name="username" column="username"></property> | ||
<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> | |||
# '''创建 Hibernate 核心配置文件'''(“<span style="color: blue">'''hibernate.cfg.xml'''</span>”); | |||
#: 示例: | |||
#: <syntaxhighlight lang="xml" highlight=""> | |||
... | |||
<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> | |||
</syntaxhighlight> | </syntaxhighlight> | ||
==== 创建 | === 代码实现 === | ||
步骤: | |||
# 加载 hibernate 核心配置文件: | |||
#: <syntaxhighlight lang="Java" highlight=""> | |||
// 到src下面找到名称是hibernate.cfg.xml | |||
// 在hibernate里面封装对象 | |||
Configuration cfg = new Configuration(); | |||
cfg.configure(); | |||
</syntaxhighlight> | |||
# 创建 '''SessionFactory''' 对象: | |||
#: <syntaxhighlight lang="Java" highlight=""> | |||
// 读取 hibernate 核心配置文件内容,创建 sessionFactory | |||
// 在过程中,根据映射关系,在配置数据库里面把表创建 | |||
SessionFactory sessionFactory = cfg.buildSessionFactory(); | |||
</syntaxhighlight> | |||
# 使用 SessionFactory 创建 '''session''' 对象: | |||
#: <syntaxhighlight lang="Java" highlight=""> | |||
// 类似于连接 | |||
Session session = sessionFactory.openSession(); | |||
</syntaxhighlight> | |||
# 开启事务: | |||
#: <syntaxhighlight lang="Java" highlight=""> | |||
Transaction tx = session.beginTransaction(); | |||
</syntaxhighlight> | |||
# 写具体逻辑 crud 操作: | |||
#: <syntaxhighlight lang="Java" highlight=""> | |||
User user = new User(); | |||
user.setUsername("小马"); | |||
user.setPassword("123"); | |||
user.setAddress("美国"); | |||
session.save(user); | |||
</syntaxhighlight> | |||
# 提交事务: | |||
#: <syntaxhighlight lang="Java" highlight=""> | |||
tx.commit(); | |||
</syntaxhighlight> | |||
# 关闭资源: | |||
#: <syntaxhighlight lang="Java" highlight=""> | |||
session.close(); | |||
sessionFactory.close(); | |||
</syntaxhighlight> |
2022年6月11日 (六) 07:10的最新版本
入门
以下内容展示一个 Hibernate 项目的入门过程。
环境搭建
步骤:
- 导入相关 JAR 包;
- 创建持久化类(实体类);
- 创建数据表;
- 如果配置“hibernate.hbm2ddl.auto”为“update”,则不需要自己手动创建表。
- 配置映射关系;(如:“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();