1.导包,这里可以自己去官网下载,也可以去根据网址去我网盘下载
链接:https://pan.baidu.com/s/1FvwxtUtAoMOR7KFJp6oaOg
密码:2yi3
把下载下来的包放进lib里面
2.创建Hibernate配置文件:hibernate.cfg.xml
还没安装hibernate插件的,去这个网址看,安装了会方便很多
https://blog.youkuaiyun.com/weixin_41069280/article/details/80465206
新建一个web工程,在src下面创建配置文件hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置连接数据库的基本信息 -->
<property name="connection.username">root</property>
<property name="connection.password">12345</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate1</property>
<!-- 配置hibernate的基本信息 -->
<!-- hibernate使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 执行操作时是否在控制台打印SQL -->
<property name="show_sql">true</property>
<!-- 是否对SQL进行格式化 -->
<property name="format_sql">true</property>
<!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
3.创建持久化类
package com.lzk.hibernate1.entity;
/**
* @author Lzk
*
*/
public class User {
private Integer id;
private String username;
private String password;
public User() {
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
4.创建对象-关系映射文件
一直next
然后去hibernate.cfg.xml里面指定关联的User.hbm.xml
5.通过Hibernate API编写访问数据库代码
这里我通过编写一个Junit测试类来展示
package com.lzk.hibernate1.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.Test;
/**
* @author Lzk
*
*/
public class HibernateTest {
@Test
public void test() {
//1.创建一个SessionFactory对象
SessionFactory sessionFactory = null;
//1)创建Configuration对象:对应hibernate的基本配置信息和对象关系映射信息
Configuration configuration = new Configuration().configure();
//2)创建一个ServiceRegistry对象
//hibernate的任何配置和服务都需要在该对象中注册后才能有效
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.build();
//3)
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//2.创建一个Session对象
Session session = sessionFactory.openSession();
//3.开启事务
Transaction transaction = session.beginTransaction();
//4.执行保存操作
User user = new User("lzk","123");
session.save(user);//保存操作
//5.提交事务
transaction.commit();
//6.关闭Session
session.close();
//7.关闭SessionFactory对象
sessionFactory.close();
}
}
运行Junit测试类,如图所示
数据库也生成表了,hibernate开发步骤就先介绍到这里