一、Hibernate简介
Hibernate一种ORM框架,全称为 Object_Relative DateBase-Mapping,在Java对象与关系数据库之间建立某种映射,以实现直接存取Java对象。
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用。
从中,我们可以得出这样的结论:Hibernate是一个轻量级的JDBC封装,也就是说,我们可以使用Hibernate来完成原来我们使用JDBC完成的操作,也就是与数据库的交互操作。它是在dao层去使用的。
二、为什么要使用Hibernate?
原因如下:
1. 对JDBC访问数据库的代码做了封装,大大简化了数据访问层繁琐的重复性代码。
2. Hibernate是一个基于JDBC的主流持久化框架,是一个优秀的ORM实现。他很大程度的简化DAO层的编码工作
3. hibernate使用Java反射机制,而不是字节码增强程序来实现透明性。
4. hibernate的性能非常好,因为它是个轻量级框架。映射的灵活性很出色。它支持各种关系数据库,从一对一到多对多的各种复杂关系
总结:Hibernate是企业级开发中的主流框架,映射的灵活性很出色。它支持很多关系型数据库。
Hibernate是一种ORM的框架。那什么是ORM呢?ORM是一种思想
O代表的是Objcet
R代表的是Relative
M代表的是Mapping
ORM->对象关系映射....ORM关注是对象与数据库中的列的关系
三、在SpringBoot中使用Hibernate
1.引入jar开发包
配置hibernate的pom文件以及mysql的pom文件
<!--hibernate 的配置文件-->
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.0.Final</version>
</dependency>
<!-- mysql数据库的驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<!--插件 maven编译的时候不会编译java目录下的xml文件 所以需要配置这个查询 不然会找不到文件的路径-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
2.配置相关的XML文件
Hibernate的相关配置文件分为两种:
xxx.hbm.xml:它主要是用于描述类与数据库中的表的映射关系。
hibernate.cfg.xml:它是Hibernate框架的核心配置文件
配置 xxx.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>
<!--
name:即实体类的全名
table:映射到数据库里面的那个表的名称
catalog:数据库的名称
-->
<class name="cn.et.dao.StudentEntity" table="student" schema="test">
<!-- class下必须要有一个id的子元素 -->
<!-- id是用于描述主键的 必须要指定 不然会报错的-->
<id name="sid" column="sid"></id>
<!-- 使用property来描述属性与字段的对应关系如果length忽略不写,且你的表是自动创建这种方案,那么length的默认长度是255-->
<property name="sname" column="sname"/>
<property name="gid" column="gid"/>
<property name="sage" column="sage"/>
<property name="ssex" column="ssex"/>
</class>
</hibernate-mapping>
配置 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>
<!-- 配置关于数据库连接的四个项:driverClass url username password -->
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- 可以将向数据库发送的SQL语句显示出来 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL语句 -->
<!--<property name="hibernate.format_sql">true</property>-->
<!-- 数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置hibernate的映射文件所在的位置 -->
<!--<mapping resource="cn/et/dao/StudentEntity.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>
3.测试
public class HibernateTest {
//获取加载配置管理类 不给参数就默认加载hibernate.cfg.xml文件,
private static final Configuration config = new Configuration().configure().addClass(StudentEntity.class);
//创建Session工厂对象
private static final SessionFactory sessionFactory = config.buildSessionFactory();
//得到Session对象
private static final Session session = sessionFactory.openSession();
public static void main(String[] args) {
// getQBCStudent(); //查询
// insertStudent(); //添加
// deleteStudent(); //删除
// updateStudent(); //修改
getHQLStudent();
}
/**
* 添加
*/
public static void insertStudent(){
//使用Hibernate操作数据库,都要开启事务,得到事务对象
session.beginTransaction();
StudentEntity studentEntity =new StudentEntity();
studentEntity.setSid("aa");
studentEntity.setGid("bb");
studentEntity.setSage("30");
studentEntity.setSname("kkk");
studentEntity.setSsex("女");
//调用添加的方法
session.save(studentEntity);
// 事务提交
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
/**
* 删除
*/
public static void deleteStudent(){
// 开启事务
session.beginTransaction();
StudentEntity studentEntity =new StudentEntity();
studentEntity.setSid("aa");
//调用删除的方法
session.delete(studentEntity);
// 事务提交
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
/**
* 修改
*/
public static void updateStudent(){
// 开启事务
session.beginTransaction();
StudentEntity studentEntity =new StudentEntity();
studentEntity.setSid("2");
studentEntity.setSage("200");
//调用修改的方法
session.update(studentEntity);
// 事务提交
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
/**
* QBC查询
*/
public static void getQBCStudent(){
//创建关于StudentEntity对象的criteria对象
Criteria criteria = session.createCriteria(StudentEntity.class);
//添加条件
criteria.add(Restrictions.eq("ssex", "男"));
criteria.add(Restrictions.like("sname","%小%"));
//查询全部数据
List<StudentEntity> list = criteria.list();
for (StudentEntity student : list){
System.out.println(student.getSid()+"=="+student.getSname()+"=="+student.getGid()+"=="+student.getSage()+"=="+student.getSsex());
}
}
/**
* HQL查询
*/
public static void getHQLStudent(){
Query query =session.createQuery("from StudentEntity where sname=?");
query.setParameter(0,"张三");
List<StudentEntity> list = query.list();
for (StudentEntity student : list){
System.out.println(student.getSid()+"=="+student.getSname()+"=="+student.getGid()+"=="+student.getSage()+"=="+student.getSsex());
}
}
}
四.hibernate执行流程