美文网首页
hibernate.cfg.xml 常用配置

hibernate.cfg.xml 常用配置

作者: 年少懵懂丶流年梦 | 来源:发表于2017-02-24 16:15 被阅读456次

要求

  1. 位置:放到src下
  2. 名称:必须是hibernate.cfg.xml

配置三部分

  1. 配置数据库信息(必须的)
  2. 配置hibernate的相关信息(不是必须的)
  3. 配置引入的映射文件(如果有映射文件必须配置)
<?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 name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3306/student
          <!--  一种简写形式
              jdbc:mysql:///student  -->
        </property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">111111</property>
        <!-- 配置数据库的方言 比如在mysql里面有关键字 limit ,只能使用在mysql里面 
            让hibernate识别到不同数据库自己特有的语句 -->
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>

        <!-- 配置hibernate的相关信息 -->
        <!-- 是否显示底层的sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 是否格式化sql语句 -->
        <property name="hibernate.format_sql">true</property>
        <!-- hibernate会帮自己创建数据库表,默认不会创建,需要配置。 
            值 update: 如果数据库里面不存在表,创建;如果已经存在,更新 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <!-- 配置事务自动提交 -->
        <!-- <property name="hibernate.connection.autocommit">true</property> -->

        <!-- 引入映射配置文件 -->
        <mapping resource="com/meng/entity/Users.hbm.xml" />
    </session-factory>
</hibernate-configuration>
  • hbm2ddl.auto:代表的是数据库的生成策略。
    create,表示删除原有的表结构,包括里面原有的数据,创建新的表结构,并插入新的数据。
    update,表示在原有表结构的基础上进行更新,保留原有的数据,对表结构进行更新,并插入新的数据。
    create-drop,表示先创建,后删除。
    validate,表示对原有的表结构进行验证,如果现有的表结构与原有的不一致则不对表结构进行更新。

  • <property name="hibernate.default_schema">xxx</property>,默认的数据库前缀,会在生成的表明前面加上固定的数据库前缀。

相关文章

网友评论

      本文标题:hibernate.cfg.xml 常用配置

      本文链接:https://www.haomeiwen.com/subject/atkhwttx.html