hibernate 核心配置

  • 必须的配置
    • 连接数据库的基本的参数
      驱动类
      url路径
      用户名
      密码
      方言
  • 可选的配置
    • 显示SQL :hibernate.show_sql
    • 格式化SQL :hibernate.format_sql
    • 自动建表 :hibernate.hbm2ddl.auto
      none :不使用hibernate的自动建表
      create :如果数据库中已经有表,删除原有表,重新创建,如果没有表,新建表。(测试)
      create-drop :如果数据库中已经有表,删除原有表,执行操作,删除这个表。如果没有表,新建一个,使用完了删除该表。(测试)
      update :如果数据库中有表,使用原有表,如果没有表,创建新表(更新表结构)
      validate :如果没有表,不会创建表。只会使用数据库中原有的表。(校验映射和表结构)。
  • 映射文件的引入
    • 引入映射文件的位置

例如我们之前给出的核心配置文件 是hibernate官方给出的配置,可以根据自己情况更改

<?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:///hibernate</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<!-- 配置Hibernate的方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<!-- 可选配置================ -->
		<!-- 打印SQL -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化SQL -->
		<property name="hibernate.format_sql">true</property>
		<!-- 自动创建表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!-- 配置C3P0连接池 -->
		<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<!--在连接池中可用的数据库连接的最少数目 -->
		<property name="c3p0.min_size">5</property>
		<!--在连接池中所有数据库连接的最大数目  -->
		<property name="c3p0.max_size">20</property>
		<!--设定数据库连接的过期时间,以秒为单位,
		如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
		<property name="c3p0.timeout">120</property>
		 <!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
		<property name="c3p0.idle_test_period">3000</property>
		
		<mapping resource=""/>
	</session-factory>
</hibernate-configuration>

猜你喜欢

转载自blog.csdn.net/qq_40435621/article/details/87624785