Hibernate与Oracle数据库连接

本文详细介绍了一个使用Hibernate框架连接Oracle数据库的实例,包括所需jar包、实体类定义、映射文件配置、核心配置文件编写及测试类实现,是学习Hibernate数据库操作的实用指南。

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

          比较详细的hibernate与Oracle连接的小例子吧~     直接上代码。

1、首先需要的jar包列出来~

附上jar包和项目   有需要的小伙伴自行下载哦~

jar:https://pan.baidu.com/s/1UFpNF0AwC6_XRPDNvWQXRg

项目代码:https://pan.baidu.com/s/1JNKrqFi1nInzciLRDmtCBQ

2、目录结构也看一下吧~

3、先编写一个用户的实体类,给定属性和get,set方法。

package cn.com.entity;

public class User {
	private int uids;
	private String username;
	private String password;
	private String adress;
	public int getUids() {
		return uids;
	}
	public void setUids(int uids) {
		this.uids = uids;
	}
	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;
	}
	public String getAdress() {
		return adress;
	}
	public void setAdress(String adress) {
		this.adress = adress;
	}
	

}

然后编写实体类的映射文件,一般命名规则是类名.hbm.xml。例如我这个叫User.hbm.xml。到这里感觉应该都能看懂吧~ 在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>
	<class name="cn.com.entity.User" table="h_user">
		<!-- 配置实体类id和表的字段对应 
			hibernate要求实体类有一个属性唯一值
			hibernate要求表有字段作为唯一值
		-->
		<!-- name属性:实体类里面id属性名称     column属性:生成的表字段名称 -->
		<id name="uids" column="uids">
			<!-- 设置数据库表id增长策略
				native:生成表主键值就是自动增长
			 -->
			<generator class="native"></generator>
		</id>
		<!-- 配置其他属性和表的字段对应 -->
		<property name="username" column="username"></property>
		<property name="password" column="password"></property>
		<property name="adress" column="adress"></property>
	</class>
</hibernate-mapping>

4、核心配置文件的编写,可以先写核心配置文件再写对应的实体类映射文件(数据库的配置不想写在这里的就写个jdbc.properties文件引进来哦~  这都不是关键^_^)

<?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">
                    oracle.jdbc.driver.OracleDriver
                </property>
            <!-- 提供url,我的数据库安在本机就是localhost,如果不在本机就写上相应的ip地址 -->
	 	<property name="hibernate.connection.url">
                    jdbc:oracle:thin:@localhost:1521:orcl
                </property>
               <!-- 登录数据库时用的用户名和密码 -->
	 	<property name="hibernate.connection.username">用户名</property>
	 	<property name="hibernate.connection.password">密码</property>
	 	
	 	<!-- 第二部分配置hibernate信息(可选的) -->
		 <!-- 输出底层的sql语句 -->
		 <property name="hibernate.show_sql">true</property>
		 <!-- 对底层SQL语句进行格式化 -->
		 <property name="hibernate.format_sql">true</property>
		 <!-- hibernate配置之后自动创建表
		 	  update:如果已经有表了就更新,如果没有表就创建,不加这个就不会自动创建表
		  -->
		 <property name="hibernate.hbm2ddl.auto">update</property>
		 <!-- 配置数据库方言,11g也是这么配,不影响使用 -->
		 <property name="hibernate.dialect">
             org.hibernate.dialect.Oracle10gDialect
         </property>
		  <!-- 第三部分把实体类映射文件放到核心配置文件中去  必须做的 -->
		 <mapping resource="cn/com/entity/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

5、到这里我们的hibernate就算是已经配置完成了,是不是很简单呢~,下一步就是编写测试类啦~

这个是我编写的一个工具类,用来获得sessionFactory,可以写也可以不写,看个人喜好。

package cn.com.utils;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {
	static Configuration cfg = null;
	static SessionFactory sessionFactory = null;
	//静态代码块实现 只用一个sessionFactory
	static {
		//加载核心配置文件
		cfg = new Configuration();
		cfg.configure();
		sessionFactory = cfg.buildSessionFactory();
	}
	//提供方法返回sessionfactory
	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public static void main(String[] args) {
		
	}

}

然后是编写测试的HibernateTest类

package cn.com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;

import cn.com.entity.User;
import cn.com.utils.HibernateUtils;

public class HibernateTest {
	@Test
	public void testAdd() {
		//加载配置文件  与上面工具类的作用相同
//		Configuration cfg = new Configuration();
//		cfg.configure();
//		SessionFactory sessionFactory = cfg.buildSessionFactory();
		
                //利用工具类获得sessionFactory对象
                SessionFactory sessionFactory = HibernateUtils.getSessionFactory();
                //得到Session对象
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		
		User user = new User();
		user.setUsername("举个栗子");
		user.setPassword("123456");
		user.setAdress("localhost");
		session.save(user);
		
		tx.commit();
		session.close();
		sessionFactory.close();
	}
}

好啦~   到这里就完美收工啦! 右击runAs测试类看看你的数据库里是不是多了张表吧~     有什么问题欢迎私信哦~

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值