IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Hibernate入门 -> 正文阅读

[Java知识库]Hibernate入门

一、Hibernate的简介:

? ? ? ? ORM框架/持久层框架——jdbc的一个框架

????????ORM框架(Object reference mapping),也就是对象关系映射

通过管理对象来改变数据库中的数据

通过管理对象来操作数据库

优势:跨数据库的无缝移植

二、Hibernate的基础使用

操作:

? ? ? ? 1、新建一个maven项目

? ? ? ? 2、导Hibernate依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
? <modelVersion>4.0.0</modelVersion>
? <groupId>com.lv</groupId>
? <artifactId>lv_hibernate</artifactId>
? <packaging>war</packaging>
? <version>0.0.1-SNAPSHOT</version>
? <name>lv_hibernate Maven Webapp</name>
? <url>http://maven.apache.org</url>
? <properties>
?? ??? ?<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
?? ??? ?<maven.compiler.source>1.8</maven.compiler.source>
?? ??? ?<maven.compiler.target>1.8</maven.compiler.target>
?? ??? ?<junit.version>4.12</junit.version>
?? ??? ?<servlet.version>4.0.0</servlet.version>
?? ??? ?<hibernate.version>5.3.0.Final</hibernate.version>
?? ??? ?<mysql.driver.version>8.0.19</mysql.driver.version>
?? ?</properties>
??
? <dependencies>
?? ??? ?<dependency>
?? ??? ??? ?<groupId>junit</groupId>
?? ??? ??? ?<artifactId>junit</artifactId>
?? ??? ??? ?<version>${junit.version}</version>
?? ??? ??? ?<scope>test</scope>
?? ??? ?</dependency>

?? ??? ?<dependency>
?? ??? ??? ?<groupId>javax.servlet</groupId>
?? ??? ??? ?<artifactId>javax.servlet-api</artifactId>
?? ??? ??? ?<version>${servlet.version}</version>
?? ??? ??? ?<scope>provided</scope>
?? ??? ?</dependency>

?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.hibernate</groupId>
?? ??? ??? ?<artifactId>hibernate-core</artifactId>
?? ??? ??? ?<version>${hibernate.version}</version>
?? ??? ?</dependency>

?? ??? ?<dependency>
?? ??? ??? ?<groupId>mysql</groupId>
?? ??? ??? ?<artifactId>mysql-connector-java</artifactId>
?? ??? ??? ?<version>${mysql.driver.version}</version>
?? ??? ?</dependency>
?? ?</dependencies>
? <build>
? ? <finalName>lv_hibernate</finalName>
? ? ?<plugins>
? ? ?? ?<plugin>
?? ??? ??? ??? ?<groupId>org.apache.maven.plugins</groupId>
?? ??? ??? ??? ?<artifactId>maven-compiler-plugin</artifactId>
?? ??? ??? ??? ?<version>3.7.0</version>
?? ??? ??? ??? ?<configuration>
?? ??? ??? ??? ??? ?<source>1.8</source>
?? ??? ??? ??? ??? ?<target>1.8</target>
?? ??? ??? ??? ??? ?<encoding>UTF-8</encoding>
?? ??? ??? ??? ?</configuration>
?? ??? ??? ?</plugin>
? ? </plugins>
? </build>
</project>
?

? ? ? ? 3、找到框架的配置文件

在resource文件中新建xml文件,取名为hibernate.cfg.xml

?

然后根据以下找到后缀名为dtd文件,在hibernate.cfg.xml中进行仿写这些文件:

找到maven Dependencies文件--》hibernate的jar包--》org.hibernate包--》往下滑就有几个dtd后缀名的文件

?

使用外部引用的方法在hibernate.cfg.xml文件中引入dtd约束:

红色标注:属于方言,这个地方配什么决定了它配的框架底层,它按照什么数据库生成SQL语句

这就是为什么hibernate能够进行数据库无缝移植

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
?? ?"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
?? ?"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
?? ?<session-factory>
?? ??? ?<!-- 1. 数据库相关5.7 -->
<!-- ?? ??? ?<property name="connection.username">root</property> -->
<!-- ?? ??? ?<property name="connection.password">123456</property> -->
<!-- ?? ??? ?<property name="connection.url">jdbc:mysql://47.100.191.44:3308/lx?useUnicode=true&amp;characterEncoding=UTF-8</property> -->
<!-- ?? ??? ?<property name="connection.driver_class">com.mysql.jdbc.Driver</property> -->
?? ??? ?<!-- 1. 数据库相关8.0 -->
?? ??? ?<property name="connection.username">root</property>
?? ??? ?<property name="connection.password">123456</property>
?? ??? ?<property name="connection.url">jdbc:mysql://127.0.0.1:3306/mybatis_ssm?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT&amp;useSSL=true</property>
?? ??? ?<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
?? ??? ?<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
?? ??? ?<!-- <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> -->

?? ??? ?<!-- 配置本地事务(No CurrentSessionContext configured!) -->
?? ??? ?<property name="hibernate.current_session_context_class">thread</property>

?? ??? ?<!-- 2. 调试相关 -->
?? ??? ?<property name="show_sql">true</property>
?? ??? ?<property name="format_sql">true</property>
?? ??? ?<!-- 配置映射文件 -->
?? ??? ?<mapping resource="com/one/entity/User.hbm.xml" />

?? ??? ?<!-- 主键生成策略 -->
<!-- ?? ??? ?<mapping resource="com/zking/two/entity/Student.hbm.xml" /> -->
<!-- ?? ??? ?<mapping resource="com/zking/two/entity/Worker.hbm.xml" /> -->

?? ??? ?<!-- 一对多 -->
<!-- ?? ??? ?<mapping resource="com/zking/four/entity/Order.hbm.xml" /> -->
<!-- ?? ??? ?<mapping resource="com/zking/four/entity/OrderItem.hbm.xml" /> -->
?? ??? ?<!-- 一对多的自关联 -->
<!-- ?? ??? ?<mapping resource="com/zking/five/entity/TreeNode.hbm.xml" /> -->

?? ??? ?<!-- 多对多 -->
<!-- ?? ??? ?<mapping resource="com/zking/five/entity/Category.hbm.xml" /> -->
<!-- ?? ??? ?<mapping resource="com/zking/five/entity/Book.hbm.xml" /> -->
?? ?</session-factory>
</hibernate-configuration>

?对象要跟表进行映射,它必须要通过一个文件进行约束

配置映射文件的路径就是以下文件User.hbm.xml的路径

⑤、我们就在实体类内建一个xml文件User.hbm.xml

以下路径是写要写映射文件的实体类(如接下来要写的User类)

<?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>

?<!--? name:类的全限名

? ? ? ? table:类对应的表

? ? ? ? id标签:

? ? ? ? ? ? ? ? name:类属性 ? ? ?? type:类属性对应的Java类型 ? ? ?? column:数据库列表

? ? ? ? property标签:

? ? ? ? ? ? ? ? name:类属性 ? ? ?? type:类属性对应的Java类型 ? ? ?? column:数据库列表

????????????????increment:代表自增
? ? ? ? ????????assigned:代表自己设计-->

?? ?<class name="lv.com.one.entity.User" table="t_hibernate_user">
?? ??? ?<id name="id" type="java.lang.Integer" column="id">
?? ??? ??? ?<generator class="increment" />
?? ??? ?</id>
?? ??? ?<property name="userName" type="java.lang.String" column="user_name">
?? ??? ?</property>
?? ??? ?<property name="userPwd" type="java.lang.String" column="user_pwd">
?? ??? ?</property>
?? ??? ?<property name="realName" type="java.lang.String" column="real_name">
?? ??? ?</property>
?? ??? ?<property name="sex" type="java.lang.String" column="sex">
?? ??? ?</property>
?? ??? ?<property name="birthday" type="java.sql.Date" column="birthday">
?? ??? ?</property>
?? ??? ?<property insert="false" update="false" name="createDatetime"
?? ??? ??? ?type="java.sql.Timestamp" column="create_datetime">
?? ??? ?</property>
?? ??? ?<property name="remark" type="java.lang.String" column="remark">
?? ??? ?</property>
?? ?</class>
</hibernate-mapping>

????????4、将数据表描述成一个对象

实体类entity中User文件,此过程描写的属性名无需与数据库中的属性名相同

package lv.com.one.entity;

import java.sql.Timestamp;
import java.util.Date;

public class User {
?? ?private int id;
?? ?private String userName;
?? ?private String userPwd;
?? ?private String realName;
?? ?private String sex;
?? ?private Date birthday;
?? ?private Timestamp createDatetime;
?? ?private String remark;
?? ?public int getId() {
?? ??? ?return id;
?? ?}
?? ?public void setId(int id) {
?? ??? ?this.id = id;
?? ?}
?? ?public String getUserName() {
?? ??? ?return userName;
?? ?}
?? ?public void setUserName(String userName) {
?? ??? ?this.userName = userName;
?? ?}
?? ?public String getUserPwd() {
?? ??? ?return userPwd;
?? ?}
?? ?public void setUserPwd(String userPwd) {
?? ??? ?this.userPwd = userPwd;
?? ?}
?? ?public String getRealName() {
?? ??? ?return realName;
?? ?}
?? ?public void setRealName(String realName) {
?? ??? ?this.realName = realName;
?? ?}
?? ?public String getSex() {
?? ??? ?return sex;
?? ?}
?? ?public void setSex(String sex) {
?? ??? ?this.sex = sex;
?? ?}
?? ?public Date getBirthday() {
?? ??? ?return birthday;
?? ?}
?? ?public void setBirthday(Date birthday) {
?? ??? ?this.birthday = birthday;
?? ?}
?? ?public Timestamp getCreateDatetime() {
?? ??? ?return createDatetime;
?? ?}
?? ?public void setCreateDatetime(Timestamp createDatetime) {
?? ??? ?this.createDatetime = createDatetime;
?? ?}
?? ?public String getRemark() {
?? ??? ?return remark;
?? ?}
?? ?public void setRemark(String remark) {
?? ??? ?this.remark = remark;
?? ?}
?? ?@Override
?? ?public String toString() {
?? ??? ?return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + ", realName=" + realName
?? ??? ??? ??? ?+ ", sex=" + sex + ", birthday=" + birthday + ", createDatetime=" + createDatetime + ", remark="
?? ??? ??? ??? ?+ remark + "]";
?? ?}
?? ?
?? ?

}

? ? ? ? 5、写实体类的映射文件

以上第⑤点有

????????6、写测试代码

三、hibernate的使用

? ? ? ? 1、对框架核心配置文件(hibernate.cfg.xml)进行建模

? ? ? ? 2、获取sessionFactory工厂

? ? ? ? 3、获取session会话

? ? ? ? 4、开启事务(查询不需要事务)

? ? ? ? 5、session操作对象

? ? ? ? 6、提交事务(查询不需要事务)

? ? ? ? 7、关闭session

查询:

package lv.com.one.test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class QueryDemo {
?? ?public static void main(String[] args) {
?? ??? ?/*1、对框架核心配置文件进行建模 ? ? ??
?? ??? ?2、获取sessionFactory工厂 ? ? ??
?? ??? ?3、获取session会话 ? ? ??
?? ??? ?4、开启事务(查询不需要事务) ? ? ? ?
?? ??? ?5、session操作对象 ? ? ??
?? ??? ?6、提交事务(查询不需要事务) ? ? ? ?
?? ??? ?7、关闭session*/
?? ??? ?Configuration configure = new Configuration().configure("/hibernate.cfg.xml");
?? ??? ?SessionFactory sessionFactory = configure.buildSessionFactory();
?? ??? ?Session session = sessionFactory.openSession();
?? ??? ?List list = session.createQuery("from User").list();
?? ??? ?for(Object object:list) {
?? ??? ??? ?System.out.println(object);
?? ??? ?}
?? ??? ?session.close();
?? ?}

}
?

得到数据;

增加AddDemo:?

package lv.com.one.test;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import lv.com.one.entity.User;

public class AddDemo {
?? ?public static void main(String[] args) {
?? ??? ?/*1、对框架核心配置文件进行建模 ? ? ??
?? ??? ?2、获取sessionFactory工厂 ? ? ??
?? ??? ?3、获取session会话 ? ? ??
?? ??? ?4、开启事务(查询不需要事务) ? ? ? ?
?? ??? ?5、session操作对象 ? ? ??
?? ??? ?6、提交事务(查询不需要事务) ? ? ? ?
?? ??? ?7、关闭session*/
?? ??? ?Configuration configure = new Configuration().configure("/hibernate.cfg.xml");
?? ??? ?SessionFactory sessionFactory = configure.buildSessionFactory();
?? ??? ?Session session = sessionFactory.openSession();
?? ??? ?Transaction transaction = session.beginTransaction();
?? ??? ?User user=new User();
?? ??? ?user.setBirthday(new Date(System.currentTimeMillis()));
?? ??? ?user.setCreateDatetime(new Timestamp(System.currentTimeMillis()));
?? ??? ?user.setId(55);
?? ??? ?user.setRealName("小熊");
?? ??? ?user.setRemark("武侠");
?? ??? ?user.setSex("男");
?? ??? ?user.setUserName("yiyi");
?? ??? ?user.setUserPwd("123456");
?? ??? ?session.save(user);
?? ??? ?transaction.commit();
?? ??? ?session.close();
?? ?}

}
?

修改EditDemo:

?

package lv.com.one.test;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import lv.com.one.entity.User;

public class EditDemo {
?? ?public static void main(String[] args) {
?? ??? ?/*1、对框架核心配置文件进行建模 ? ? ??
?? ??? ?2、获取sessionFactory工厂 ? ? ??
?? ??? ?3、获取session会话 ? ? ??
?? ??? ?4、开启事务(查询不需要事务) ? ? ? ?
?? ??? ?5、session操作对象 ? ? ??
?? ??? ?6、提交事务(查询不需要事务) ? ? ? ?
?? ??? ?7、关闭session*/
?? ??? ?Configuration configure = new Configuration().configure("/hibernate.cfg.xml");
?? ??? ?SessionFactory sessionFactory = configure.buildSessionFactory();
?? ??? ?Session session = sessionFactory.openSession();
?? ??? ?Transaction transaction = session.beginTransaction();
//?? ??? ?拿到数据
?? ??? ?User user = session.get(User.class, 55);
//?? ??? ?修改数据
?? ??? ?user.setRealName("toto");
?? ??? ?System.out.println(user);
?? ??? ?
?? ??? ?transaction.commit();
?? ??? ?session.close();
?? ?}

}
?

?删除DelDemo:

package lv.com.one.test;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import lv.com.one.entity.User;

public class DelDemo {
?? ?public static void main(String[] args) {
?? ??? ?/*1、对框架核心配置文件进行建模 ? ? ??
?? ??? ?2、获取sessionFactory工厂 ? ? ??
?? ??? ?3、获取session会话 ? ? ??
?? ??? ?4、开启事务(查询不需要事务) ? ? ? ?
?? ??? ?5、session操作对象 ? ? ??
?? ??? ?6、提交事务(查询不需要事务) ? ? ? ?
?? ??? ?7、关闭session*/
?? ??? ?Configuration configure = new Configuration().configure("/hibernate.cfg.xml");
?? ??? ?SessionFactory sessionFactory = configure.buildSessionFactory();
?? ??? ?Session session = sessionFactory.openSession();
?? ??? ?Transaction transaction = session.beginTransaction();
//?? ??? ?拿到数据
?? ??? ?User user = session.get(User.class, 55);
//?? ??? ?修改数据
?? ??? ?session.delete(user);
?? ??? ?transaction.commit();
?? ??? ?session.close();
?? ?}

}
?

?四、注意:

我们在pom.xml中导jar包依赖时,可能有时会没下载完全,就可以按以下操作检查(可以解决90%jar包没下载完全的问题):

首先清除jar包依赖:

?然后进行测试:

不论是清除jar包还是测试jar包,只有出现包含以下样式都说明成功了的:

? ? ? ?

本期内容结束~

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-10-19 11:44:20  更:2021-10-19 11:46:29 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/27 6:34:55-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码