Configuration、Session


1.configuration

  Configuration 类负责管理Hibernate配置信息。它包括如下内容:

    Hibernate运行的底层信息:数据库的URL、用户名、密码、JDBC驱动类,数据库Dialect,数据库连接池等。

    Hibernate映射文件(*.hbm.xml)。

    Hibernate的配置方法:

      基于属性文件(hibernate.properties),调用代码: Configuration cfg = new Configuration();

      基于Xml文件(hibernate.cfg.xml),调用代码: Configuration cfg = new Configuration().configure();

  Hibernate的配置文件 hibernate.cfg.xml 配置数据库的相关信息及对象

<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">zhuoshi</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
</session-factory>
</hibernate-configuration>

  Hibernate配置文件属性


2.Session

  Session 代表与数据库服务器之间的一次操作,它的概念介于Connection和Transaction之间。

  Session 也称为持久化管理器,因为它是与持久化有关的操作接口。

  Session 通过SessionFactory打开,在所有的工作完成后,需要关闭。

  Hibernate 中的 Session 与 Web 层的 HttpSession 没有任何关系。

  Hibernate中获取 Session 调用代码: Session session = sessionFactory.openSession();

 


原文地址:https://www.cnblogs.com/xiaonanman/p/6851851.html