日期:2014-05-16  浏览次数:20462 次

【学习笔记】Hibernate配置进行数据库操作

让我们先来看一下Hibernate配置文件内容:

???

<?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">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost/mydata</property>
		<property name="connection.username">root</property>
		<property name="connection.password">chengfei</property>

		<!-- JDBC connection pool (use the built-in) -->
		<!--   <property name="connection.pool_size">1</property>-->

		<!-- SQL dialect 方言 将SQL语句翻译成对应的数据库中-->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

		<!-- Enable Hibernate's automatic session context management -->
		<!--<property name="current_session_context_class">thread</property>-->

		<!-- Disable the second-level cache  -->
		<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

		<!-- Echo all executed SQL to stdout //回应所有的执行SQL语句-->
		<property name="show_sql">true</property>

		<!-- Drop and re-create the database schema on startup //是否自动生成建表语句 -->
		<property name="hbm2ddl.auto">validate</property>

		<mapping class="com.cf.model.Student"/>
	</session-factory>

</hibernate-configuration>

??

我们来分析一下这个配置文件(主要是session-factory节点的内容):

???

?<!-- Database connection settings -->
????????<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
??? 从这个属性的name就能够知道,该节点的值代表的是要连接的数据库驱动包。如MySQL的jdbc.mysql.Driver等;

?

???????<property name="connection.url">jdbc:mysql://localhost/mydata</property>
??? 这是连接数据库的URL:如MySQL的jdbc:mysql://localhost/库名


??????? <property name="connection.username">root</property>
??? 这是连接到数据库的用户名


??????? <property name="connection.password">chengfei</property>
??? 连接到数据库的密码

??????? <!-- JDBC connection pool (use the built-in) -->
??????? <property name="connection.pool_size">1</property>

???? 这是Hibernate连接池的配置,即在连接池中存放的最大可用连接数。不过Hibernate自身都知道连接池的功能效率较差,所以我们在做产品的项目时,应该使用第三方的容器进行连接池的配置!

?????在测试阶段还是可以小用一下。

??????? <!-- SQL dialect -->
???????<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
???? dialect 方言的意思,该配置是告诉Hibernate将使用哪种数据库的语言进行数据库操作(也可以说是将要支持的SQL语句转换成各类数据库所识别的SQL语句)

??????? <!-- Enable Hibernate's automatic session context management -->
??????? <property name="current_session_context_class">thread</property>

??? 从说明中来看,表示的是自动管理当前的会话上下文,默认是thread。该参数定义了应该采用哪个?org.hibernate.context.CurrentSessionContext实现。

??? 具体的作用还未体验到……跳过

??????? <!-- Disable the second-level cache? -->
??????? <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

??? 配置二级缓存,同样未深入研究,待述……

??????? <!-- Echo all executed SQL to stdout -->
??????? <property name="show_sql">true</property>

??? 这个配置很关键,是否响应所有被执行的SQL语句。默认为true,即将执行成功的SQL语句输出到控制台上。

??????? <!-- Drop and re-create the database schema on startup -