hibernate核心类和接口

来源:互联网 发布:阿里云手机官网 编辑:程序博客网 时间:2024/04/30 08:14

Part 1 Configuration类

    这里写图片描述 
Part 2 SessionFactory接口

     这里写图片描述 
    通过SessionFactory获取Session有两个方法:openSession()和getCurrentSession 

    1、openSession()是获取一个新的session 

    2、getCurrentSession()获取和当前线程绑定的session,换言之,在同一个线程中,我们获取的session是同一session,利于事务的控制。

    3、如果使用getCurrentSession需要在hibernate.cfg.xml中配置

<property name="current_session_context_class">thread</property>

Part 3 Session接口

        这里写图片描述

Part 4 Transaction接口 
    这里写图片描述 
Part 5 Query接口 
    这里写图片描述 
   
在实际应用中,Query接口十分常用,有更为强大的查询能力,而get、load只能根据主键查询,所以Query十分必要,Query可防止sql注入

Part 6 Criteria接口

     Criteria接口也可用于面向对象方式的查询,实际使用的并不多。

//id指的是映射对象的属性名称而不是表的字段名Query query=session.createQuery("from Employee where id=100");List<Employee> list=query.list();for(Employee e:list){ System.out.println(e.getName()+""+e.getHiredate()); }
Criteria cri=session.createCriteria(Employee.class).add(Restrictions.like("name","Fritz%")).setMaxResult(2);List<Employee> list=cri.list();



0 0