likes
comments
collection
share

hibernate配置文件详解

作者站长头像
站长
· 阅读数 10

hibernate配置文件

hibernate的配置文件在hibernate.cfg.xml文件中编写

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- 配置连接数据库信息 -->
        <property name="connection.url">jdbc:mysql://localhost:3306/studyhibernate</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>

      	<!-- JDBC connection pool 数量 -->
        <property name="connection.pool_size">1</property>
        <!-- 配置hibernate的基本信息 -->
        <!-- hibernate方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <!-- 执行操作时是否在控制台打印 -->
        <property name="show_sql">true</property>
        <!-- 是否对SQL进行格式化 -->
        <property name="format_sql">true</property>
        <!-- 指定自动生成数据表的策略 -->
        <!--
            取值可以有create、update、create-drop、validate
            - create  根据.hbm.xml文件来生成数据表,但是每次运行都会删除上一次的表,重新生成表
            - create-drop 根据.hbm.xml文件生成表,sessionFactory关闭,表会自动删除
            - update  根据.hbm.xml文件生成表,但若.hbm.xml文件和数据库中对应的数据表的表结构不同,hibernate会跟新数据表结构,但不会删除已有的行和列
            - validate  和数据库的表进行比较,若.hbm.xml文件中的列在数据表中不存在,则抛出异常

        -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- DB schema will be updated if needed -->
        <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
      	<!-- session管理方式 -->
        <property name="current_session_context_class">thread</property>

        <!-- 启用二级缓存 -->
        <property name="cache.use_second_level_cache">false</property>
        <!-- 二级缓存指定第三方插件 -->
        <!--<property name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>-->
        <!-- 开启查询缓存 -->
        <!--<property name="cache.use_query_cache">true</property>-->

        <!--<property name="current_session_context_class">thread</property>-->
        <!-- 指定关联的映射文件 -->
        <mapping resource="User.hbm.xml"/>
        <mapping resource="Customer.hbm.xml"/>
        <mapping resource="Order.hbm.xml"/>
        <mapping resource="Department.hbm.xml"/>
        <mapping resource="Manager.hbm.xml"/>
        <!-- 配置哪个类使用二级缓存 -->
        <!--<class-cache class="com.zhanghe.study.model.User" usage="read-write"/>
        <collection-cache collection="com.zhanghe.study.model.many2one.Customer.orderList" usage="read-write"/>
        <class-cache class="com.zhanghe.study.model.many2one.Order" usage="read-write"/>-->
    </session-factory>
</hibernate-configuration>

zhhll.icu/2020/框架/hib…

本文由mdnice多平台发布

转载自:https://juejin.cn/post/7351321295068479497
评论
请登录