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. 【知识积累】爬虫之网页乱码解决方法(gb2312 -> utf-8)

    前言 今天在测试爬虫项目时,发现了一个很严肃的问题,当爬取的网页编码格式为gb2312时,按照一般的办法转化为utf-8编码时总是乱码,PS:爬取的所有网页无论何种编码格式,都转化为utf-8格式进行 ...

  2. 【Java基础】类和接口

    Num1:使类和成员的可访问性最小化 要区别设计良好的模块与设计不好的模块,最重要的因素在于,这个模块对于外部的其他模块而言,是否隐藏其内部数据和其他实现细节.设计良好的模块会隐藏所有的实现细节,把它 ...

  3. Mac下如何配置环境变量

    以前都是在Windows平台上开发,在配置一些框架的时候,为了能够在命令行中调用,一般都会配置bin目录到环境变量中,这是为了让命令行在执行的时候,能够查找到对应的执行文件. 现在工作使用Mac,配置 ...

  4. 加速Web开发的9款知名HTML5框架

    与手工编码比起来,HTML5框架在准确性和正确率方面给予了保证.大多数HTML5框架都会有一个组合或者包含一些额外的组件,比如jQuery Scripts.CSS3样式表则以改善多媒体特征的功能性和响 ...

  5. 关于Html与css的一些解释

    一.简单介绍 1.html,是hyper text markup language的缩写,中文为“超文本标记语言”. 2.html不是编程语言而是一种标记语言. 二.标签与元素 1.html标签:如& ...

  6. Redis代码阅读之Hacking Strings

    Hacking Strings The implementation of Redis strings is contained in sds.c ( sds stands for Simple Dy ...

  7. Emit学习(1) - HelloWorld

    之前看过Dapper(使用到了Emit), CYQ.Data(另一种思路,没有使用Emit)类的框架之后, 也想自己做一个小框架玩一下, 不过此时能力太过欠缺, 做不了Cyq.Data或者PDF.Ne ...

  8. 递归查询树形结构的SQL

    最近项目中要递归树形,案例如下: 测试数据: ),PID ),Name )) ',NULL ,'山东省' ','烟台市' ','招远市' ','青岛市' ',NULL ,'四会市' ','清远市' ' ...

  9. Linq和Lamda表达式的简单处理方式

    一 什么是LINQ? LINQ即Language Integrated Query(语言集成查询),LINQ是集成到C#和Visual Basic.NET这些语言中用于提供查询数据能力的一个新特性. ...

  10. .NET invoke NetSuite Restlet

    Please indicate the source if you need to repost. Restlet allows programmers to use the http request ...