一,环境的搭建

1)创建maven项目

2)导入依赖的jar包.pom.xml和创建实体类User

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.hibernate</groupId>
<artifactId>hibernate004</artifactId>
<version>1.0-SNAPSHOT</version> <name>hibernate004</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</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>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.3.Final</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.7.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.6.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.3.6.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
</dependencies> <build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

实体类

package com.hibernate;

public class User {
private Integer id;
private Integer age;
private String name;
private String password; public User() {
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}

3)创建resources的文件夹和创建hibernate.cfg.xml的配置文件以及hibernate.hbm.xml文件

hibernate.cfg.xml文件

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Hibernate配置文件的DTD信息 -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- hibernate- configuration是连接配置文件的根元素 -->
<hibernate-configuration>
<session-factory>
<!-- 指定连接数据库所用的驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 指定连接数据库的url,hibernate连接的数据库名 -->
<property name="connection.url">jdbc:mysql://localhost/hibernate001</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 指定连接数据库的密码 -->
<property name="connection.password">123456</property>
<!-- 指定连接池里最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 指定连接池里最小连接数 -->
<property name="hibernate.c3p0.min_size">1</property>
<!-- 指定连接池里连接的超时时长 -->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 指定连接池里最大缓存多少个Statement对象 -->
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<!-- 指定数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 根据需要自动创建数据表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<property name="show_sql">true</property>
<!-- 将SQL脚本进行格式化后再输出 -->
<property name="hibernate.format_sql">true</property>
<!--设置数据源,由于上述使用的属性有hibernate.c3p0.*,会自动设置为C3P0可不设。默认为hibernate数据源。-->
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<!--current_session作用域,thread表示当前线程用同一个session-->
<property name="current_session_context_class">thread</property>
<!-- 罗列所有的映射文件,resources为文件路径,最好和主配置文件同一目录,自己开始没同一目录报错了.原因未知,就是找不到 -->
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>

hibernate.hbm.xml文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--package为实体类包名-->
<hibernate-mapping package="com.hibernate">
<!--name为类名,如果没写包名,这里的类名要写成权限定性类名.
table为表名,不写默认为简单类名。tips:Windows系统mysql数据库默认不区分大小写。-->
<class name="User" table="user">
<!--id标签表示这是主键,name为实体类属性名,column为对应表的
gernarator参考https://blog.csdn.net/ye1992/article/details/19632001说明
这里native就好了。-->
<id name="id" column="id">
<generator class="native"/>
</id>
<!--name为属性名,colunmn就是列名-->
<property name="age" column="age"/>
<property name="name" column="name"/>
<property name="password" column="password"/>
</class>
</hibernate-mapping>

4) 创建一个hibernateUtile工具类和测试类

hibernateUtile类

package com.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUtil {
private static SessionFactory sf;
static{
Configuration configure = new Configuration().configure();
sf= configure.buildSessionFactory();
} //opensession
public static Session getOpenSession(){
return sf.openSession();
}
//currentsession
public static Session getCurrentSession(){
return sf.getCurrentSession();
}
}

测试类

package com.hibernate;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; public class Hibernate_test {
@Test
public void test01(){
Session session=HibernateUtil.getOpenSession();
Transaction transaction = session.beginTransaction(); User user=session.get(User.class,2);
System.out.println(user); transaction.commit();
session.close();
}
}

测试结果

说明hibernate的环境搭建成功了

二,配置文件说明

hibernate有两个配置文件 hibernate.cfg.xml和hibernate.hbm.xml

配置文件中的主要内容,已经加上了注释信息,可以网上查看

三,什么是hibernate?

Hibernate是一个操作数据库的持久层框架,实现了对JDBC的封装,.Hibernate是一个ORM(对象关系映射)框架,我们在写程序时 ,用的时面向对象的方法,但是在关系型数据库里,存的是一条条的数据,为了用纯面向对象的思想解决问题,所有需要将程序中的对象和数据库的记录建立起映射关系,ORM就是这样的技术,而Hibernate就是这样一个框架,以操作对象的方式操作数据库。

四,hibernate相关API

configuration:配置对象

在使用hibernate的时候,首先要创建configuration的实例,Configuration实例主要用于启动,加载,管理hibernate的配置文件信息,在启动hibernate的过程中,configuration首先确定hibernate的配置文件的位置,然后读取相关配置,最后创建一个唯一的SessionFactory实例,configuration值存在于最初的系统初始化阶段,它将sessionFactory创建完成之后,就完成它的使命了.hibernate通常会使用new Configuration().configure(),来创建一个配置类对象,默认的是在resources下的hibernate.cfg.xml配置文件,如果不想创建和该文件名一样的xml配置文件,可以在cofigure()中添加一个参数,参数的内容是配置文件的名称

sessionFactory 是session的工厂对象

sessionFactory接口负责将Hibernate的初始化和建立session对象,它在hibernate中起到一个缓冲区的作用,hibernate可以将自动生成的sql语句,映射数据以及某些可重复利用的数据放在这个缓冲区中,同时它还保存了对数据库配置的所有映射关系,维护了当前的二级缓存

session对象

五,transaction事务

六,

1)什么是持久化类

2)持久化的编写规则

3)hibernate的主键生成策略

七,持久化状态的三种描述

区分对象的三种状态

hibernate三种转态之间的相互转换

持久化对象能够自动更新数据库

hibernate的一级缓存

什么是hibernate的一级缓存

测试一级缓存

一级缓存的快照

hibernate的事务控制

1)什么是事务

2)事务的四个特性

3)事务的并发问题

4)事务的隔离级别

5)事务管理

HQL

1)query

SQLQuery

hibernate的详解的更多相关文章

  1. hibernate Expression详解

    关键字: hibernate expression hibernate Expression详解Expression.gt:对应SQL条件中的"field > value " ...

  2. Java程序员从笨鸟到菜鸟之(五十一)细谈Hibernate(二)开发第一个hibernate基本详解

    在上篇博客中,我们介绍了<hibernate基本概念和体系结构>,也对hibernate框架有了一个初步的了解,本文我将向大家简单介绍Hibernate的核心API调用库,并讲解一下它的基 ...

  3. Hibernate配置文件详解

    Hibernate配置方式 Hibernate给人的感受是灵活的,要达到同一个目的,我们可以使用几种不同的办法.就拿Hibernate配置来说,常用的有如下三种方式,任选其一. 在 hibernate ...

  4. (转)spring hibernate properties详解

    转载地址:http://blog.sina.com.cn/s/blog_692d0a650100xyqx.html Hibernate配置属性 hibernate.dialect:一个Hibernat ...

  5. Maven搭建SpringMVC+Hibernate项目详解 【转】

    前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...

  6. Hibernate入门详解

    学习Hibernate ,我们首先要知道为什么要学习它?它有什么好处?也就是我们为什么要学习框架技术? 还要知道    什么是Hibernate?    为什么要使用Hibernate?    Hib ...

  7. Maven搭建SpringMVC+Hibernate项目详解

    前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...

  8. Hibernate 配置详解(3)

    7) hibernate.max_fetch_depth: 该属性用于设置left out join单对象查询关系(one-to-one/many-to-one)中最大的关联深度.默认值为0,即默认情 ...

  9. Hibernate 配置详解(2)

    6) hibernate.session_factory_name: 配置一个JNDI名称,通过Configuration对象创建的SessionFactory会绑定到JNDI下该名称中.一般名字格式 ...

  10. Hibernate 配置详解(5)

    9) hibernate.batch_fetch_style: 该配置是hibernate4.2.0新添加的,使用这个设置可以配置hibernate在做batch-fetch的时候,生成SQL的策略. ...

随机推荐

  1. Matlab绘制阶梯形图

    声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 对于Matlab的使用情况常常是这样子的,很多零碎的函数名字很难记忆,经常用过后过一段时间就又忘记了,又得去网 ...

  2. F2812 DSP程序运行在片内RAM和FLASH的区别

    F2812 DSP程序运行在片内RAM和片内FLASH的区别 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明:F2812是带有内部Flash的DSP,与 ...

  3. Beginning Linux Programming 学习-chapter2-Shell programming-Pipes and Redirection

    "为了从事创造性工作,人类需要孤独,可是在孤独中,广义的人类仍存在于内心."--(德国)奥铿                                             ...

  4. [CF369E]Valera and Queries_离线_树状数组

    Valera and Queries 题目链接:codeforces.com/problemset/problem/369/E 数据范围:略. 题解: 这种题,就单独考虑一次询问即可. 我们发现,包括 ...

  5. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  6. 轻松搭建CAS 5.x系列(9)-登录后显示通知信息

    概述说明 用户在账号名密码认证通过后,CAS可以跳转到登陆完成页面前,显示相关的通知页面. 搭建步骤 `1. 首先,您需要有个CAS Server端 如果您没有,可以按照我之前写的文章<轻松搭建 ...

  7. jvm的内存区域介绍

    什么是jvm? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的 ...

  8. C++虚函数【Java有虚函数吗?】

    1,简单介绍 定义在基类中的函数,子类必须对其进行覆写![必须对其进行覆写?!]——Java中的接口.Abstract方法中的抽象类也有这样的要求. C++中定义: virtual void deal ...

  9. Linux 安装Mysql(图文教程)

    原文:Linux 安装Mysql(图文教程) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net ...

  10. CentOS 系统下使用 yum 安装 Redis

    本文主要介绍在 CentOS 7 系统下使用 yum 安装 Redis 的过程. 更改 yum 源 将 Centos 的 yum 源更换为国内的阿里云源. 首先备份你的原镜像文件,保证出错后可以恢复: ...