Hibernate基本框架创建
来源:互联网 发布:在哪儿查询车流量数据 编辑:程序博客网 时间:2024/06/06 18:18
Hibernate基本框架创建步骤
1、在java项目中创建lib文件夹,放入所有需要的jar包,在buildPath一下。
所有的jar包如下:(10个基本jar包和一个数据库的jar包)
2、在src根文件夹下创建hibernate.cfg.xml文件,进行配置(oracle数据库配置)
<?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> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@172.16.130.35:1521:orcl</property> <property name="connection.username">uuid</property> <property name="connection.password">uuid</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/iflytek/domain/Student.hbm.xml"/> </session-factory></hibernate-configuration>
3、创建基本的实体类如Student.java包含setter和getter的方法
package com.iflytek.domain;public class Student {private String id;private String name;private int age;public String getName() {return name;}public String getId() {return id;}public void setId(String id) {this.id = id;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
4、创建Student.hbm.xml文件
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.iflytek.domain"> <class name="Student" table="tt_student"> <id name="id" column="id"> <generator class="uuid"/> </id> <property name="name" column="name"/> <property name="age" column="age"/> </class></hibernate-mapping>
5、封装util对象
package com.iflytek.util;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;public class HibernateUtil {private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();//ThreadLocal对象 private static SessionFactory sessionFactory = null;//SessionFactory对象 //静态块static {final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure() // configures settings from hibernate.cfg.xml.build();try {sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();}catch (Exception e) {// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory// so destroy it manually.StandardServiceRegistryBuilder.destroy( registry );} }/** *获取Session * @return Session * @throws HibernateException */ public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get();if (session == null || !session.isOpen()) {if (sessionFactory == null) {rebuildSessionFactory();}session = (sessionFactory != null) ? sessionFactory.openSession(): null;threadLocal.set(session);} return session; }/** * 重建会话工厂 */public static void rebuildSessionFactory() {try {// 加载Hibernate配置文件Configuration cfg = new Configuration().configure();sessionFactory = cfg.buildSessionFactory();} catch (Exception e) {System.err.println("创建会话工厂失败");e.printStackTrace();}}/** * 获取SessionFactory对象 * @return SessionFactory对象 */public static SessionFactory getSessionFactory() {return sessionFactory;}/** *关闭Session * @throws HibernateException */ public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close();//关闭Session } if ( sessionFactory != null ) {sessionFactory.close();} }}6、测试类进行测试
package com.iflytek.test;import static org.junit.Assert.*;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.junit.After;import org.junit.Before;import com.iflytek.domain.Student;import com.iflytek.util.HibernateUtil;public class Test {//private SessionFactory sessionFactory;@Beforepublic void setUp() throws Exception {/*final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure() // configures settings from hibernate.cfg.xml.build();try {sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();}catch (Exception e) {// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory// so destroy it manually.StandardServiceRegistryBuilder.destroy( registry );}*/}@Afterpublic void tearDown() throws Exception {/*if ( sessionFactory != null ) {sessionFactory.close();}*/}@org.junit.Testpublic void test() {Session session = HibernateUtil.getSession();Transaction tx = session.beginTransaction();Student stu=new Student();stu.setName("张三");stu.setAge(11);session.save(stu);tx.commit();session.close();}}
完毕!!!!!
阅读全文
0 0
- Hibernate基本框架创建
- Hibernate 基本创建
- hibernate基本框架及配置
- Hibernate框架(1)框架基本搭建
- Hibernate框架中Session的基本操作
- hibernate框架基本搭建及使用
- springMVC+spring+hibernate基本框架的搭建
- Eclipese搭建Hibernate框架,基本的CRUD
- 第一个hibernate创建的基本过程
- hibernate框架自动创建数据库表格
- Qt中创建OpenGL基本框架
- struts+spring+hibernate基本项目框架的搭建(1)
- struts+spring+hibernate基本项目框架的搭建(2)
- java框架简介1--spring+hibernate简介 2 基本结合
- android orm映射框架(类似hibernate)基本使用
- Spring+hibernate+struts2+easyui基本权限管理框架
- android orm映射框架(类似hibernate)基本使用
- SpringMVC+Hibernate+Junit4+json基本框架近乎0配置
- 一句话理解数据库索引
- JS基础学习第四天:条件控制语句、循环语句、函数模块、事件等通用代码块2
- install opencv in docker
- C++ 学习笔记之派生
- 斗地主手牌 代码
- Hibernate基本框架创建
- AfxBeginThread
- mysql索引总结----mysql 索引类型以及创建
- require/exports和import/export的区别
- LBP特征的几种模式、代码实现链接
- Android Studio下的NDK开发(四):调用动态链接库中的函数
- 集合中除去指定元素
- 最大子阵列
- OPUS资料汇总