Hibernate之API

2019-09-29  本文已影响0人  紫荆秋雪_文

一、Hibernate的API介绍

1、Configuration对象

Hibernate框架的配置对象,主要用于解析Hibernate配置文件和持久化映射文件中的信息,最重要的功能是用来创建SessionFactory对象(配置文件都要存放在classpath根路径)

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!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>


        <!--配置数据库连接的信息-->

        <!--数据库连接方言-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!--数据库驱动类名-->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        
        <!--数据库连接账户-->
        <property name="hibernate.connection.url">jdbc:mysql:///hibernateDemo?useSSL=true&amp;serverTimezone=UTC</property>

        <!--数据库连接账户-->
        <property name="hibernate.connection.username">root</property>

        <!--数据库连接密码-->
        <property name="hibernate.connection.password">admin</property>

        <!--引用xml-->
        <mapping resource="com/revanwang/_01/crud/domain/User.hbm.xml"/>

    </session-factory>
</hibernate-configuration>
 //1、创建配置对象
Configuration cfg = new Configuration();
//2、读取配置文件
cfg.configure("/hibernate.cfg.xml");
#数据库方言
hibernate.dialect=org.hibernate.dialect.MySQLDialect
#数据库驱动类名
hibernate.connection.driver_class=com.mysql.jdbc.Driver
#数据库连接账户
hibernate.connection.url=jdbc:mysql:///hibernateDemo?useSSL=true&amp;serverTimezone=UTC
#数据库连接账户
hibernate.connection.username=root
#数据库连接密码
hibernate.connection.password=admin
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!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>
        <!--引用xml-->
        <mapping resource="com/revanwang/_01/crud/domain/User.hbm.xml"/>

    </session-factory>
</hibernate-configuration>    

2、SessionFactory对象

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

3、Session对象

Session分析一级缓存

在一个session的生命周期之内,连续拿到相同类型,相同ID的对象,只会发送一次SQL

#数据库方言
hibernate.dialect=org.hibernate.dialect.MySQLDialect
#数据库驱动类名
hibernate.connection.driver_class=com.mysql.jdbc.Driver
#数据库连接账户
hibernate.connection.url=jdbc:mysql:///hibernateDemo?useSSL=true&serverTimezone=UTC
#数据库连接账户
hibernate.connection.username=root
#数据库连接密码
hibernate.connection.password=admin
#配置getCurrentSession
hibernate.current_session_context_class=thread
#显示SQL语句
hibernate.show_sql=true
#格式化显示SQL语句
hibernate.format_sql=true
    @Override
    public User get(Long id) {
        //4、获取Session
        Session session = HibernateUtil.getHibernateSession();
        session.beginTransaction();
        //5、具体的查询操作
        User user = (User) session.get(User.class, 1L);
        session.getTransaction().commit();
        return user;
    }
Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.name as name2_0_0_,
        user0_.date as date3_0_0_ 
    from
        t_user user0_ 
    where
        user0_.id=?
    @Override
    public User get(Long id) {
        //4、获取Session
        Session session = HibernateUtil.getHibernateSession();
        session.beginTransaction();
        //5、具体的查询操作
        User user = (User) session.get(User.class, 1L);
        User user1 = (User) session.get(User.class, 3L);
        session.getTransaction().commit();
        return user;
    }
Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.name as name2_0_0_,
        user0_.date as date3_0_0_ 
    from
        t_user user0_ 
    where
        user0_.id=?
Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.name as name2_0_0_,
        user0_.date as date3_0_0_ 
    from
        t_user user0_ 
    where
        user0_.id=?
    @Override
    public User get(Long id) {
        //4、获取Session
        Session session = HibernateUtil.getHibernateSession();
        session.beginTransaction();
        //5、具体的查询操作
        User user = (User) session.get(User.class, 1L);
        User user1 = (User) session.get(User.class, 1L);
        session.getTransaction().commit();
        return user;
    }
Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.name as name2_0_0_,
        user0_.date as date3_0_0_ 
    from
        t_user user0_ 
    where
        user0_.id=?
Session缓存.png
    public User get(Long id) {
        //4、获取Session
        Session session = HibernateUtil.getHibernateSession();
        session.beginTransaction();
        //5、具体的查询操作
        User user = (User) session.get(User.class, 1L);

        //清除一级缓存中所有的缓存
        session.clear();

        User user1 = (User) session.get(User.class, 1L);
        session.getTransaction().commit();
        return user;
    }
Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.name as name2_0_0_,
        user0_.date as date3_0_0_ 
    from
        t_user user0_ 
    where
        user0_.id=?
Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.name as name2_0_0_,
        user0_.date as date3_0_0_ 
    from
        t_user user0_ 
    where
        user0_.id=?
    public User get(Long id) {
        //4、获取Session
        Session session = HibernateUtil.getHibernateSession();
        session.beginTransaction();
        //5、具体的查询操作
        User user = (User) session.get(User.class, 1L);

        //清除一级缓存中所有的缓存
        session.evict(user);

        User user1 = (User) session.get(User.class, 1L);
        session.getTransaction().commit();
        return user;
    }
Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.name as name2_0_0_,
        user0_.date as date3_0_0_ 
    from
        t_user user0_ 
    where
        user0_.id=?
Hibernate: 
    select
        user0_.id as id1_0_0_,
        user0_.name as name2_0_0_,
        user0_.date as date3_0_0_ 
    from
        t_user user0_ 
    where
        user0_.id=?

4、Transaction对象:代表对象事务的封装

5、Query查询

    @Override
    public void queryList(String hql) {
        Session session = HibernateUtil.getHibernateSession();
        Query query = session.createQuery(hql);
        query.setString(0, "%西夏%");
        query.setLong(1, 1L);
        query.setLong(2, 10L);
        List<User> list = query.list();

        for (User user : list) {
            System.out.println(user);
        }
    }

6、Criteria查询

    @Override
    public void criteriaList() {
        Session session = HibernateUtil.getHibernateSession();

        Criteria criteria = session.createCriteria(User.class);
        //设置约束条件
        criteria.add(Restrictions.like("name", "西夏", MatchMode.ANYWHERE));
        criteria.add(Restrictions.between("id", 1L, 10L));
        List<User> list = criteria.list();

        for (User user : list) {
            System.out.println(user);
        }
    }
上一篇下一篇

猜你喜欢

热点阅读