Hibernate框架使用教程
1、通过 new project 创建一个 Hibernate 项目,点击 next 后命名项目名称,之后点击 finish :
【如果勾选 “Create default hibernate configuration and main class”, 后续不用再手动创建 hibernate.cfg.xml配置文件】
2、创建如图所示的一个数据库 hibernate 以及表 user:
3、IDEA 里连接数据库:
自定义 Name ,输入 mysql 的用户以及密码,然后输入要连接的数据库 Database,之后点击 Test Connection,如图所示:
【如果显示 Server returns invalid timezone 报错,则是因为 mysql 时区默认是 UTC 时区,需要修改:在 mysql 命令模式下,输入 set global time_zone='+8:00';
即可】
4、添加 jdbc 的 jar 包,右键点击 jar 包,然后点击 Add as Library:
5、在 src 下新建目录 com.example,另外,在项目名称下新建目录 test,并将 test 目录设为 Test Sources Root ,如图:
6、创建 hibernate.cfg.xml主配置文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration><session-factory><property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.username">root</property><property name="connection.password">123456</property><!-- DB schema will be updated if needed --><!-- <property name="hibernate.hbm2ddl.auto">update</property> --><!-- 配置数据库方言--><property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 执行操作时是否在控制台打印 SQL--><property name="show_sql">true</property>
<!-- 是否对 SQL 进行格式化--><property name="format_sql">true</property>
<!-- 指定自动生成数据表的策略--><property name="hibernate.hbm2ddl.auto">update</property></session-factory>
</hibernate-configuration>
7、根据数据库中的user表,生成UserEntity类:
点击侧边栏 Persistence ,选中数据库样式的 hibernate.cfg.xml 文件,右键 Generate Persistence Mapping —> By Database Schema:
注:将生成的 UserEntity.hbm.xml 文件放到与 UserEntity 同一个目录下
UserEntity.java:
package com.example;import javax.persistence.*;
import java.util.Objects;@Entity
@Table(name = "user", schema = "hibernate", catalog = "")
public class UserEntity {private int uid;private String username;private String password;@Id@Column(name = "uid", nullable = false)public int getUid() {return uid;}public void setUid(int uid) {this.uid = uid;}@Basic@Column(name = "username", nullable = true, length = 12)public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}@Basic@Column(name = "password", nullable = true, length = 8)public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;UserEntity that = (UserEntity) o;return uid == that.uid &&Objects.equals(username, that.username) &&Objects.equals(password, that.password);}@Overridepublic int hashCode() {return Objects.hash(uid, username, password);}
}
UserEntity,hbm.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping><class name="com.example.UserEntity" table="user" schema="hibernate"><id name="uid"><column name="uid" sql-type="int(4)"/></id><property name="username"><column name="username" sql-type="varchar(12)" length="12" not-null="true"/></property><property name="password"><column name="password" sql-type="varchar(8)" length="8" not-null="true"/></property></class>
</hibernate-mapping>
8、在 test 目录下新建一个测试类 UserTest:
import com.example.UserEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;public class UserTest {public static void main(String[] args) {Configuration configuration=new Configuration();configuration.configure("hibernate.cfg.xml");SessionFactory sessionFactory=configuration.buildSessionFactory();Session session=sessionFactory.openSession();Transaction transaction=session.beginTransaction();UserEntity userEntity=new UserEntity();
// userEntity.setUid(1);//此处设不设id都行userEntity.setUsername("ximu");userEntity.setPassword("123456");session.save(userEntity);transaction.commit();session.close();System.out.println("Successful saved.");}
}
9、运行 UserTest.java 文件:
如果出以下 bug——
可能是因为 hibernate.cfg.xml 文件中的内容已经不知不觉中发生了改变——
- com.mysql.jdbc.Driver 竟变成了 com.mysql.cj.jdbc.Driver
- 下面两行连接数据库的用户名、以及密码的代码也消失了
<property name="connection.username">root</property><property name="connection.password">123456</property>
解决方式自然是恢复成原来的样子即可!
10、IDEA运行结果,以及 mysql 数据库表中的更新:
♥ 喜 欢 请 点 赞 哟 ♥ |
(●ˇ∀ˇ●) |
Hibernate框架使用教程相关推荐
- c++框架有哪些_Java Mybatis框架入门教程_v20200726
MyBatis 的前身是 Apache 的开源项目 iBatis.MyBatis 几乎可以代替 JDBC,是一个支持普通 SQL 查询,存储过程和高级映射的基于 Java 的优秀持久层框架.MyBat ...
- hibernate官方新手教程 (转载)
hibernate官方新手教程第一部分 - 第一个Hibernate程序 首先我们将创建一个简单的控制台(console-based)Hibernate程序.我们使用内置数据库(in-memory d ...
- gwt-2.8.2下载_GWT 2 Spring 3 JPA 2 Hibernate 3.5教程– Eclipse和Maven 2展示
gwt-2.8.2下载 不久前,我的一个朋友和同事向我飞过,说"世界上只有一半在使用Maven ". 当我意识到最受欢迎的文章(到目前为止) GWT 2 Spring 3 JPA ...
- gwt-2.8.2下载_GWT 2 Spring 3 JPA 2 Hibernate 3.5教程
gwt-2.8.2下载 本分步指南将介绍如何使用以下方法开发简单的Web应用程序 Google的网络工具包 (GWT)用于富客户端,而Spring作为后端服务器端框架. 该示例Web应用程序将提供对数 ...
- GWT 2 Spring 3 JPA 2 Hibernate 3.5教程– Eclipse和Maven 2展示
不久前,我的一个朋友和同事向我飞过,说"世界上只有一半在使用Maven ". 当我意识到最受欢迎的文章(到目前为止) GWT 2 Spring 3 JPA 2 Hibernate ...
- GWT 2 Spring 3 JPA 2 Hibernate 3.5教程
本分步指南将介绍如何使用开发一个简单的Web应用程序 Google的网络工具包 (GWT)用于富客户端,而Spring作为后端服务器端框架. 该示例Web应用程序将提供对数据库执行CRUD(创建检索更 ...
- Spring Hibernate集成示例教程
Spring Hibernate集成示例教程(Spring 4 + Hibernate 3和Hibernate 4) Spring是最常用的Java EE Framework之一,而Hibernate ...
- Struts2 Hibernate集成示例教程
Struts2 Hibernate集成示例教程 Struts2和Hibernate都是各自领域中广泛使用的框架.今天我们将学习如何将Struts2 Web应用程序框架与Hibernate ORM框架集 ...
- JSF Spring Hibernate集成示例教程
JSF Spring Hibernate集成示例教程 欢迎使用JSF Spring Hibernate Integration示例教程.在上一篇教程中,我们了解了如何将JSF和Spring框架集成在一 ...
- Primefaces Spring和Hibernate集成示例教程
Primefaces Spring和Hibernate集成示例教程 欢迎使用Spring Primefaces和Hibernate Integration示例.框架之间的集成是一项复杂的任务,而且大多 ...
最新文章
- Java项目命名规范
- 「要拼就拼运维」5分钟一台?它让我爱上工作了!
- magento获取判断当前页或句柄handles
- (转)ArcEngine读取数据(数据访问)
- “北航Clubs” Beta版本开发目标
- ITK:使用写访问权访问图像中的迭代区域
- 深度学习(四)——RNN, LSTM, 神经元激活函数进阶
- 绝望:对不完的数,加不完的班
- 将字符串中的大写字母变成小写字母
- 开源当自强:我们不是“便宜货”
- java在控制台输出sql_logger4j配置数据库sql输出控制台
- 提高数据质量的方法有哪些
- [USACO08NOV]时间管理Time Management
- 网络工程师HCNA认证学习笔记Day1
- 【转录调控网络】基因转录调控网络——转录因子调控网络分析
- Flutter Ink,InkWell,InkResponse水波纹实现(2.3)
- 【微信小程序】--注册小程序账号安装开发者工具(一)
- 【调试】sysRq按键使用方法
- 【mq】从零开始实现 mq-09-消费者拉取消息 pull message
- unity查找物体方法
热门文章
- C语言实现flappy bird(可视化编程)
- Polycom高清视频会议桌面系统HDX 4000
- windows7文本文档换成c语言,win10电脑新建文本文档默认编码是UTF-8怎么修改成ANSI编码...
- 为什么我的devc 调试不了c 语言,devc++为什么调试不了,求大神帮看看!
- 常见的一些SQL注入漏洞类型
- C语言图书出入库管理系统
- 资源工具分享(第1期):后端架构师技术图谱
- ajax上传文件时显示进度
- 基于springboot的高校失物招领系统毕业设计源码111731
- 给火山安卓软件开发平台娃娃#2版提点意见