1、内容:  hibernate 也是一个经典的[数据访问中间件] 开源框架。
 
 2、hibernate核心组件
 
    SessionFactory[整个数据的操作]重量级组件
 
    Session[对数据库的一次业务操作] 轻量级
 
3、ORM(对象关系映射):   是一种数据访问层 解决方案、用它开源很好的移植到不同数据库平台。
 
     通过 对象模型   操作  数据库关系模型
 
 
4、hibernate配置
   配置SessionFactory
 
开发大致步骤:
 

 
(参考文档路径)\hibernate-distribution-3.6.8.Final-dist\hibernate-distribution-3.6.8.Final\documentation\manual\zh-CN\pdf
 
(配置文件路径)
hibernate3.jar\org\hibernate - ZIP 压缩文件, 解包大小为 9,174,527 字节
 
 
    1、配置 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>
<!--Database connection settings -->
<!--连接数据库-->
<property name="connection.driver_class"
>oracle.jdbc.driver.OracleDriver</property>  
<!--连接URL-->   
<property name="connection.url"
>jdbc:oracle:thin:@localhost:1521:orcl</property>  
<!--帐号-->
<property name="connection.username"              
>scott</property>
<!--密码-->
<property name="connection.password"
>tiger</property>
<!--SQL dialect 方言(hiernate可以将对对象模型的操作转为对oracle实际的SQL)-->    
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!--Enable Hibernate's automatic session context management -->
<!--将通过当前sessionFactory得到的session会被绑定到当前的线程-提高性能-->
<property name="current_session_context_class">thread</property>     
<!--Echo all executed SQL to stdout -->
<!--将hibernate转成的SQL语句-显示到控制台->
<property name="show_sql">true</property>
<mapping resource="com/it/bean/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
 
com/it/bean/UserInfo.hbm.xml
(文件路径)hibernate3.jar\org\hibernate - ZIP 压缩文件, 解包大小为 9,174,527 字节
<!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.it.bean">
    <class name="UserInfo" table="userInfo">
        <id name="user_id" column="user_id">
            <!-- 主键生成策略 -->
            <generator class="assigned"></generator>
        </id>
        <property name="user_pwd" column="user_pwd"></property>
        <property name="user_sex" column="user_sex"></property>
 
 
    2、创建SessionFactory
 
public class Text1{
     public static void main(String[] args){
         //创建SessionFactory
          SessionFavtory sessionFactory = null;
          try{
               sessionFactory = new Configuration.configure().buildSessionFactory();
          }catch(Exception e){
               e.printStackTrace();
          } 
     }
}
 
    3、创建Session
 
public class Text1{
     public static void main(String[] args){
         //创建SessionFactory
          SessionFavtory sessionFactory = null;
         //创建Session
          Session session  = null;
          try{
               sessionFactory = new Configuration.configure().buildSessionFactory();
               session = sessionFactory.getCurrentSession();
          }catch(Exception e){
               e.printStackTrace();
          } 
     }
}
    4、创建Transaction(事务处理对象)
 
public class Text1{
     public static void main(String[] args){
         //创建SessionFactory
          SessionFavtory sessionFactory = null;
         //创建Session
          Session session  = null;
          //事务
          Transaction tx = null;
          try{
               sessionFactory = new Configuration.configure().buildSessionFactory();
               session = sessionFactory.getCurrentSession();
          }catch(Exception e){
               e.printStackTrace();
          } 
     }
}
 
    5、开启事务
 
public class Text1{
     public static void main(String[] args){
         //创建SessionFactory
          SessionFavtory sessionFactory = null;
         //创建Session
          Session session  = null;
          //事务
          Transaction tx = null;
          try{
               sessionFactory = new Configuration.configure().buildSessionFactory();
               session = sessionFactory.getCurrentSession();
               //开启事务
               tx = sesison.beginTransaction();
          }catch(Exception e){
               e.printStackTrace();
          } 
     }
}
 
 
    6、提交事务
 
public class Text1{
     public static void main(String[] args){
         //创建SessionFactory
          SessionFavtory sessionFactory = null;
         //创建Session
          Session session  = null;
          //事务
          Transaction tx = null;
          try{
               sessionFactory = new Configuration.configure().buildSessionFactory();
               session = sessionFactory.getCurrentSession();
               //开启事务
               tx = sesison.beginTransaction();
               //提交
               tx.commit();
          }catch(Exception e){
               e.printStackTrace();
          } 
     }
}
 
    7、执行操作
 
public class Text1{
     public static void main(String[] args){
         //创建SessionFactory
          SessionFavtory sessionFactory = null;
         //创建Session
          Session session  = null;
          //事务
          Transaction tx = null;
          try{
               sessionFactory = new Configuration.configure().buildSessionFactory();
               session = sessionFactory.getCurrentSession();
               //开启事务
               tx = sesison.beginTransaction();
               UserInfo u = new UserInfo("9989","sd","23");
               //执行U对象 操作 保存
               Session.save(u);   -------添加
               //session.delete(u);     ------删除
               //UserInfo u1 = (UserInfo)session.get(UserInfo.class,"1001");     ------查询
               //System.out.println(u1.getUser_pwd());
               //u1.setUser_sex("weinihaom?-sidashu");      ----修改
 
               // Query query = session.createQuery("from UserInfo");                                         //query.list();    -------查询2
 
                //提交
               tx.commit();
          }catch(Exception e){
               e.printStackTrace();
          } 
     }
}
 
    8、异常处理块,事务回滚
 
public class Text1{
     public static void main(String[] args){
         //创建SessionFactory
          SessionFavtory sessionFactory = null;
         //创建Session
          Session session  = null;
          //事务
          Transaction tx = null;
          try{
               sessionFactory = new Configuration.configure().buildSessionFactory();
               session = sessionFactory.getCurrentSession();
               //开启事务
               tx = sesison.beginTransaction();
               UserInfo u = new UserInfo("9989","sd","23");
               //执行U对象 操作 保存
               Session.save(u);
                //提交
               tx.commit();
          }catch(Exception e){
               e.printStackTrace();
               //事务回滚
               tx.rollback();
          } 
     }
}
 
封装---工具包
 
 
package com.it.dao.util;
/**
*单例模式
*/
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class SessionFactoryUtils {
    private static SessionFactory  sessionFactory;
    static{
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
}
 
 
基础包(BaseDAO)
 
package com.it.dao;
 
import java.util.List;
 
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
 
import com.it.dao.util.SessionFactoryUtils;
 
public class BaseDAO<T>{
 
/**
*查询2
*/
    public List<T>  find(String hql,String...params){
        Session session = null;
        Transaction tx = null;
        List<T> list = null;
        try {
            session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            Query query =session.createQuery(hql);
            for (int i = 0; i < params.length; i++) {
                query.setString(i, params[i]);
            }
            list=query.list();
            tx.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            tx.rollback();
        }
        return list;
    }
    public List<T>  find(String hql){
        Session session = null;
        Transaction tx = null;
        List<T> list = null;
        try {
            session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            Query query =session.createQuery(hql);
            list=query.list();
            tx.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            tx.rollback();
        }
        return list;
    }
    public void add(Object o){
        Session session = null;
        Transaction tx = null;
        try {
            session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            session.save(o);
            tx.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            tx.rollback();
        }
    }
/**
*查询
*/
    public T get(Class<T> clz,String OID){
        Session session = null;
        Transaction tx = null;
        T t=null;
        try {
            session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            t=(T) session.get(clz, OID);
            tx.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            tx.rollback();
        }
        return t;
    }
/**
*删除
*/
    public void delete(Class<T> clz,String OID){
        Session session = null;
        Transaction tx = null;
        Object o =get(clz,OID);
        try {
            session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            session.delete(o);
            tx.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            tx.rollback();
        }
    }
/**
*修改
*/
    public void update(Object o){
        Session session = null;
        Transaction tx = null;
        try {
            session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            session.update(o);
            tx.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            tx.rollback();
        }
    }
    public void delete(Object o){
        Session session = null;
        Transaction tx = null;
        try {
            session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            session.delete(o);
            tx.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            tx.rollback();
        }
    }
}
 
 
userDAO
 
 
package com.it.dao;
 
import java.util.List;
 
import com.it.bean.UserInfo;
 
public class UserDAO extends BaseDAO<UserInfo>{
/**
*查询
*/
    public List<UserInfo>  findUsers(UserInfo user){
        String hql = "from UserInfo user where user.user_id like ? and user.user_pwd like ?  and user.user_sex like ? ";
        String []params={"%"+user.getUser_id()+"%","%"+user.getUser_pwd()+"%","%"+user.getUser_sex()+"%"};
        return super.find(hql,params);
    }
/**
*查询2
*/
    public List<UserInfo>  findAllUsers(){
        String hql = "from UserInfo";
        return super.find(hql);
    }
 
 
    public void add(UserInfo user){
        super.add(user);
    }
    public void delete(String id){
        super.delete(UserInfo.class, id);
    }
    public void delete(UserInfo user){
        super.delete(user);
    }
    public UserInfo get(String user_id){
        return super.get(UserInfo.class,user_id);
    }
}
 
 
package com.it.test;
 
import java.util.List;
 
import com.it.bean.UserInfo;
import com.it.dao.BaseDAO;
import com.it.dao.UserDAO;
 
public class Test {
    public static void main(String[] args) {
        UserDAO dao = new UserDAO();
        //UserInfo u = new UserInfo("1","1","1");
        //UserInfo u = new UserInfo("1","1","");
        List<UserInfo> users = dao.findUsers(u);
        for (UserInfo user : users) {
            System.out.println(user.getUser_pwd());
        }
    }
}
 
 
 
 
 
 
 
 

hibernate 入门([数据访问中间件] 开源框架)的更多相关文章

  1. 三大框架之hibernate入门

    hibernate入门   1.orm      hibernate是一个经典的开源的orm[数据访问中间件]框架           ORM( Object Relation Mapping)对象关 ...

  2. .net core 基于Dapper 的分库分表开源框架(core-data)

    一.前言 感觉很久没写文章了,最近也比较忙,写的相对比较少,抽空分享基于Dapper 的分库分表开源框架core-data的强大功能,更好的提高开发过程中的效率: 在数据库的数据日积月累的积累下,业务 ...

  3. Farseer.net轻量级ORM开源框架 V1.x 入门篇:视图的数据操作

    导航 目   录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:视图实体类映射 下一篇:Farseer.net轻量级ORM开源 ...

  4. Farseer.net轻量级ORM开源框架 V1.x 入门篇:表的数据操作

    导航 目   录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:表实体类映射 下一篇:Farseer.net轻量级ORM开源框 ...

  5. Farseer.net轻量级开源框架 入门篇:添加数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 分类逻辑层 下一篇:Farseer.net轻量级开源框架 入门篇: 修改数据详解 ...

  6. Farseer.net轻量级开源框架 入门篇:修改数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 添加数据详解 下一篇:Farseer.net轻量级开源框架 入门篇: 删除数据详解 ...

  7. Farseer.net轻量级开源框架 入门篇:删除数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 修改数据详解 下一篇:Farseer.net轻量级开源框架 入门篇: 查询数据详解 ...

  8. Farseer.net轻量级开源框架 入门篇:查询数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 删除数据详解 下一篇:Farseer.net轻量级开源框架 中级篇: Where条 ...

  9. PDF.NET SOD 开源框架红包派送活动 && 新手快速入门指引

    一.框架的由来  快速入门 有关框架的更多信息,请看框架官方主页! 本套框架的思想是借鉴Java平台的Hibernate 和 iBatis 而来,兼有ORM和SQL-MAP的特性,同时还参考了后来.N ...

随机推荐

  1. ssh git免密码提交代码

    使用ssh协议通过密钥验证的方式提交代码,不用再每次提交时输入账户密码. 1.打开bash 输入一下命令, ssh-keygen -t rsa -C youremail@example.com(把邮件 ...

  2. javascript学习总结(三):如何较好的使用js。

    1 假如浏览器不支持JavaScript怎么办? a.为什么浏览器会不支持?大部分浏览器都有禁用脚本的功能,例如chrome.b.在js被禁用的情况下要保证网页仍能实现它的核心功能(关键的用户需求) ...

  3. Android自动化测试之Monkeyrunner学习笔记(一)

    Android自动化测试之Monkeyrunner学习笔记(一) 因项目需要,开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括Monkey.Monkeyr ...

  4. LocationManager使用细节

    在使用系统的LocationManager请求地理位置的时候,请特别注意一个很小的细节,调用 requestLocationUpdates 以后,请记得[自己]设置一个timeout值,否则在某些情况 ...

  5. JAVA 设计模式 代理模式

    用途 代理模式 (Proxy) 为其他对象提供一种代理以控制对这个对象的访问. 代理模式是一种结构型模式. 结构

  6. 用SQL语句查找包含有某个关键字的存储过程、触发器、函数等(仅适用MS SQL SERVER)

    第一种方法:利用系统表进行查询   --将text替换成你要查找的内容  select name  from sysobjects o, syscomments s  where o.id = s.i ...

  7. 使用James搭建一个自己的邮箱服务器

    ---第一天开发--- 下载Apache James 3.0邮箱服务器,解压到响应的目录 可以看到目录结构: H:\code\JavaCode\James\apache-james-3.0-beta4 ...

  8. 利用Navigation Timing测量页面加载时间

    最近在看一本名为<web性能实践日志>的书籍,其中第十三章"网络计时"中介绍了一种比较新的计算页面各部分加载时间方法,这也是W3C Web性能工作小组正在做的事情,接下 ...

  9. jQuery使用经验建议

    在开发过很多 jQuery 插件以后,我慢慢的摸索出了一套开发jQuery插件比较标准的结构和模式.这样我就可以复制并粘贴大部分的代码结构,只要专注最主要的逻辑代码就行了. 使用相同的设计模式和架构也 ...

  10. Tomcat Server Timeouts属性的设置

    在启动Tomcat Server时,经常会出现启动时间过长的错误,如下图所示(为了方便截图,Start Timeout被设置为5秒钟,一般为45秒钟). 双击Tomcat v7.0 Server at ...