hibernate配置文件详解

hibernate配置文件

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

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多平台发布

相关推荐
用户611881615196224 分钟前
Java基础面试题
java
DavidSoCool1 小时前
Elasticsearch Java API Client [8.17] 使用
java·大数据·elasticsearch
无世世1 小时前
【Java从入门到起飞】面向对象编程(高级)
java·开发语言
Vic101011 小时前
Mac如何查看 IDEA 的日志文件
java·macos·intellij-idea
陈逸轩*^_^*1 小时前
idea打不开,idea打不开,Error occurred during initialization of VM
java·ide·intellij-idea
=PNZ=BeijingL1 小时前
使用Mockito实现单元测试
java
yuren_xia1 小时前
在 IntelliJ IDEA(2024) 中创建 JAR 包步骤
java·intellij-idea·jar
User_芊芊君子2 小时前
【JAVA】之路启航——初识Java篇
java·开发语言