一,环境的搭建
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的详解
原文地址:https://www.cnblogs.com/liu1459310172/p/9781422.html