什么是Hibernate?
- 应用在JavaWeb三层结构中的DAO层
- 主要实现的是对数据库的增删改查,避免了写重复且复杂的JDBC代码
- 它是一个开源并且轻量级的框架
什么是orm思想
- orm即Object Relational Mapping(对象关系映射),简单来说就是让实体类与数据库中的表和实体类中属性与数据库中表的字段进行一一映射
- 不用直接操作数据库,操作表对应的实体类即可
搭建hibernate环境
- 所需jar包
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/38efd167def73d78db8431129468320f.png)
- 配置映射文件和核心文件
1)映射文件和核心文件的配置(前提已经建立好相应的实体类,即JavaBean和数据库中已经建立相应的库)
package tqb.entity;
public class User {
private int uid;
private String username;
private String pwd;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [uid=" + uid + ", username=" + username + ", pwd=" + pwd + "]";
}
}
<?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="tqb.entity.User" table="t_user">
<id name="uid" column="uid">
<generator class="native"></generator>
</id>
<property name="username" column="username"></property>
<property name="pwd" column="pwd"></property>
</class>
</hibernate-mapping>
<?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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123321tqb</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="tqb/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
代码实现
package tqb.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import tqb.entity.User;
public class Demo {
@Test
public void addTest(){
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
User user = new User();
user.setUsername("zhangsan");
user.setPwd("123456");
session.save(user);
transaction.commit();
session.close();
factory.close();
}
}
Hibernate的核心的API
- Configuration
1)加载核心配置文件,即在src目录下找hibernate.cfg.xml的配置文件,创建对象,把核心配置文件放到cfg对象中 - SessionFactory
1)根据文件内容进行数据库表的创建
2)这个过程非常的耗费资源,建议创建工具类,使用静态代码块实现,项目中只加载一次 - Session
1)类似于jdbc中数据库的连接
2)调用session对象的方法进行一些具体的操作
3)session对象为单线程对象,只能自己进行使用
工具类
package utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
static Configuration cfg = null;
static SessionFactory sessionFactory = null;
static {
cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
}
public static Session getSessionObject() {
return sessionFactory.getCurrentSession();
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}