欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

搭建Hibernate框架

程序员文章站 2022-04-22 07:59:24
...

新建 hibernate.cfg.xml

<?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>
		<!--数据库URL -->
		<property name="connection.url"><!--数据库IP地址记得更改  -->
			jdbc:oracle:thin:@192.168.43.185:1521:orcl
		</property>
		<!--数据库用户 -->
		<property name="connection.username">scott</property>
		<!--数据库用户密码 -->
		<property name="connection.password">tigger</property>
		<!--数据库JDBC驱动 -->
		<property name="connection.driver_class">
			oracle.jdbc.driver.OracleDriver
		</property>
		<!--每个数据库都有其对应的Dialect以匹配其平台特性 --><!--  数据库方言-->
		<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
		<!--指定当前session范围和上下文 -->
		<property name="current_session_context_class">thread</property>
		<!--是否将运行期生成的SQL输出到日志以供调试 -->
		<property name="show_sql">true</property>
		<!--是否格式化SQL -->
		<property name="format_sql">true</property>
		<mapping resource="com/it/pojo/Dept.hbm.xml"/>
	</session-factory>


</hibernate-configuration>


新建 公共类 hiberbateUtil

public class hiberbateUtil {
   private static Configuration configuration=null;
   private static SessionFactory factory=null;
   
   
   static {
	    //创建配置文件
	   configuration=  new Configuration().configure();
	 //创建会话工厂
	   factory=configuration.buildSessionFactory();
	 
   }
   
   private hiberbateUtil() {
	
}
   public static Session  getsession() {
	   
	 //创建会话
	  
	return factory.openSession();
   }
}

dao层事例

package com.it.dao;





import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.it.pojo.Dept;
import com.it.util.hiberbateUtil;

public class DeptDao {
	Session session=hiberbateUtil.getsession();
	Transaction tran=null;
	
	/*
	 * 查询单个
	 */
	public Dept selOne(int deotno) {
		Dept dept =	(Dept) session.get(Dept.class, deotno);
		return dept;
		
	}

	/*
	 * 查询所有
	 */
	public List<Dept> selAll(){
		Query  query=session.createQuery("from Dept");
		List<Dept> list=query.list();
		return list;
	}
	
	
	/*
	 * 增加
	 */
	
	public void add(Dept dept) {
		try {
			//开启事务
			 tran=		session.beginTransaction();
			session.save(dept);
			tran.commit();
		} catch (Exception e) {
			tran.rollback();
		}finally {
			if(session!=null) {
				if(session.isOpen()) {
					session.close();
				}
			}
			
		}
	}
	
	/*
	 * 修改
	 */
	
	public void upd(Dept dept) {
		try {
			//开启事务
			 tran=		session.beginTransaction();
			session.update(dept);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}finally {
			session.close();
		}
	}
	
	/*
	 * 删除
	 */
	
	public void del(Dept dept) {
		try {
			//开启事务
			 tran=		session.beginTransaction();
			session.delete(dept);
			tran.commit();
		} catch (Exception e) {
			tran.rollback();
		}finally {
			session.close();
		}
	}
	
	
	
}