Hibernate从其配置文件中读取和数据库连接有关的信息。Hibernate配置文件有两种形式,XML格式或者java属性文件(properties)格式。
(一)java属性文件的格式创建hibernate的配置文件,默认文件名为hibernate.properties,为键值对的形式,放在src目录下:例如
??(一)java属性文件的格式创建hibernate的配置文件,默认文件名为hibernate.properties,为键值对的形式,放在src目录下:例如 ???????????????hibernate.dialect=org.hibernate.dialect.MySQLDialect ???????????????hibernate.connection.driver_class=com.mysql.jdbc.Driver ???????????????hibernate.connection.url=jdbv:mysql://localhost:3306/hibernate ???????????????hibernate.connection.username=root ???????????????hibernate.connection.password=123456 ???????????????hibernate.show_sql=true ???????????????hibernate.dialect:指定数据库使用的sql方言。可以根据数据库的不同生成不同的方言,底层是通过调用一个一个类实现的。 ???????????????hibernate.connection.driver_class:指定数据库的驱动程序 ???????????????hibernate.connection.url:指定连接数据库的url ???????????????hibernate.connection.username:指定连接数据库的用户名 ???????????????hibernate.connection.password:指定连接数据库的密码 ???????????????hibernate.show_sql:如果为true,可以在控制台打印sql语句 ????????????????hbm2ddl.auto:生成表结构的策略配置,配置这个可以通过映射文件和实体类自动生成表结构 ???????????????有四个值: ???????????????????update(最常用的取值):如果当前数据库不存在对应的数据表,那么自动创建数据表 ???????????????????如果存在对应的数据表,并且表结构和实体类属性一致,那么不做任何修改 ???????????????????如果存在对应的数据表,但是表结构和实体类属性不一致,那么会新创建与实体类属性对应的列,其他列不变 ????????????????????create(很少使用):无论是否存在对应的数据表,每次启动Hibernate都会重新创建对应的数据表,以前的数据会丢失 ???????????????????create-drop(极少使用):无论是否存在对应的数据表,每次启动Hibernate都会重新创建对应的数据表,每次运行结束删除数据表 ???????????????????validate(很少使用):只校验表结构是否和我们的实体类属性相同,不同就抛异常
(二)使用xml格式的配置文件,默认文件名为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 name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> ???????????????????????<property name="hibernate.connection.username">root</property> ???????????????????????<property name="hibernate.connection.password">123456</property> ???????????????????????<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> ???????????????????????<property name="show_sql">true</property> ???????????????????????<property name="format_sql">true</property> ???????????????????????<property name="hbm2ddl.auto">update</property> ???????????????????????<property name="hibernate.connection.autocommit">true</property> ????????????????????????//引入映射文件 ???????????????????????<mapping resource="com/cad/domain/User.hbm.xml"/> ???????????????????</session-factory> ???????????????</hibernate-configuration>
两种方式的区别
???????????如果Hibernate的配置文件为java属性文件,那么必须通过代码来声明需要加载的映射文件 ???????????通过Configuration的addClass(实体类名.class)来加载。 ???????????配置文件为xml文件时,可以通过<mapping>元素来指定需要加载的映射文件。 ???????????当通过Configuration的默认构造方法创建实例时,会默认查找hibernate.properties文件,如果找到就将配置信息加载到内存中。 ???????????默认情况下,hibernate不会加载hibernate.cfg.xml文件,必须通过Configuration的configure()方法来显式加载hibernate.cfg.xml文件
(二)hibernate配置管理
原文地址:https://www.cnblogs.com/yuexiaoyun/p/9451865.html