hibernate.cfg.xml详解(1.1)

1.配置怎么连接数据库了

2.Student与数据表的映射关系

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory><!-- session工厂-->

        <!-- Database connection settings --><!--意思是说你自己不要再去连数据库了,hibernate你配置好了自动帮你连了-->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>用户名
        <property name="connection.password">bjsxt</property>密码

        <!-- JDBC connection pool (use the built-in) -->
        <!--hibernate自带了一个连接池你可以用,不过在真正项目开发过程当中我们很少用hibernate自带的连接池,我们用的最多的是application server本身用JNDI注册在里面的连接池,所以这项给它注释掉-->
        <!--<property name="connection.pool_size">1</property>   -->

        <!-- SQL dialect -->
        <!--dialect翻译过来是“方言”的意思,hibernate统一了我们在数据库这方面的访问,hibernate的HSQL是官方语言,不过它最终还是要翻译成为每一种不同数据库里面的特殊语言,数据库与数据库之间的语言还是有点区别的,在这个层面上hibernate已经帮我们统一了,但是往下翻译的时候,翻译到具体内容的时候,你必须告诉hibernate要翻译成什么语言(是什么样的数据库就翻译成什么样的dialect(方言)),在hibernate生成具体的语句的时候是生成针对MySQL的语句,还是生成针对Oracle的语句-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property><!--指明在生成语句的时候是针对何种数据库的语句,是生成针对MySQL的语句,还是生成针对Oracle的语句等-->

        <!-- Enable Hibernate's automatic session context management -->
        <!--getCurrentSession()从当前上下文里面找Session(上下文有2种,一种是jta,一种是thread),当前线程里面有就拿来用,没有就生产一个;如果你要使用getCurrentSession(),那么就必须设置“current_session_context_class”,否则会报错-->
       <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 -->
        <property name="show_sql">true</property><!--在我们执行的过程当中要不要把它(hibernate)生成的sql语句给我们打印出来-->

        <!-- Drop and re-create the database schema on startup -->
       <!-- <property name="hbm2ddl.auto">update</property>--><!--hbm就是hibernate mapping,ddl就是(data definition language 数据定义语言,说的直白一点就是建表语句,这个话的意思是说你要不要hibernate自动帮你生成建表语句;2.如果后面的值是create,表示如果数据库中没有我们实例对应的表的话,那么数据库会自动帮我们创建一个与实例同名的表,但是如果有我们的实例对应的表的话,数据库这个表会被删除,让后重写创建表;3.如果后面的值是update,表示如果数据库中有表,但是你的配置文件里面这张表的结构修改了,那么数据库就会自动帮我们修该表的结构;4.当使用create-drop的时候,只要你关闭sessionFactory,将drop掉数据库的scheme-->

       
		
		<mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/><!--告述hibernate.cfg.xml在哪里找Student.hbm.xml配置文件-->
		<mapping class="com.bjsxt.hibernate.Teacher"/><!--annotation-->
    </session-factory>

</hibernate-configuration>

 

猜你喜欢

转载自weigang-gao.iteye.com/blog/2156777