Hibernate基础配置 hibernate.cfg.xml

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z591826160/article/details/82590581

hibernate.cfg.xml

<?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>
        <!-- property 元素用于配置Hibernate中的属性键:值-->

        <!-- 数据库内容 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <!-- 如果出现SQL的time zone问题 请添加serverTimezone=UTC -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property>

        <!-- show_sql: 操作数据库时,会向控制台打印SQL语句 -->
        <property name="show_sql">true</property>

        <!-- format_sql: 美化打印的SQL语句-->
        <property name="format_sql">true</property>

        <!-- hbm2ddl.auto: 生成表结构的策略配置
             update(最常用的取值): 如果当前数据库中不存在表结构,那么自动创建表结构.
                     如果存在表结构,并且表结构与实体一致,那么不做修改
                     如果存在表结构,并且表结构与实体不一致,那么会修改表结构.会保留原有列.
             create(很少):无论是否存在表结构.每次启动Hibernate都会重新创建表结构.(数据会丢失)
             create-drop(极少): 无论是否存在表结构.每次启动Hibernate都会重新创建表结构.每次Hibernate运行结束时,删除表结构.
             validate(很少):不会自动创建表结构.也不会自动维护表结构.Hibernate只校验表结构. 如果表结构不一致将会抛出异常.
          -->
        <property name="hbm2ddl.auto">update</property>

        <!-- 最近MySQL建表使用engine 而旧版使用type 按需修改为 MySQLDialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- hibernate.connection.autocommit: 事务自动提交  -->
        <property name="hibernate.connection.autocommit">true</property>

        <!-- 将Session与线程绑定=> 只有配置了该配置,才能使用getCurrentSession -->
        <property name="hibernate.current_session_context_class">thread</property>

        <!-- 以下为Hibernate的三种映射方式,可选其中一种或多种使用,推荐Annotation形式 -->

        <!-- 在使用Annotation的情况下进行对象映射 -->
        <mapping class="com.test.hibernate"/>
        <!-- 链接.hbm.xml文件进行绑定 方便项目管理 -->
        <mapping resource="com/test/hibernate.hbm.xml"/>
        <!-- .hbm.xml文件中为类似以下的映射配置内容 -->
        <class name="Event" table="EVENTS">
        <id name="id" column="EVENT_ID">
            <generator class="increment"/>
        </id>
        <property name="date" type="timestamp" column="EVENT_DATE"/>
        <property name="title"/>
        </class>
    </session-factory>
</hibernate-configuration>

猜你喜欢

转载自blog.csdn.net/z591826160/article/details/82590581
今日推荐