Hibernate开发详细步骤

这篇博客详细介绍了Hibernate开发的步骤,包括导入Hibernate库、创建hibernate.cfg.xml配置文件、设置持久化类、建立对象-关系映射文件,并通过API编写访问数据库的Junit测试代码,展示了如何利用Hibernate生成数据库表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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开发步骤就先介绍到这里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值