Hibernate框架(4) —— Hibernate主配置(配置详解)
2018-03-08 20:48
399 查看
引言:Hibernate主配置文件 为hibernate.cfg.xml
<?xml version=”1.0” encoding=”UTF-8”?><!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/HibernateConfiguration DTD 3.0//EN”
“http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd”>
<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”>0516</property>
<property name=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property>
<property name=”hibernate.show_sql”>true</property> 显示SQL
<property name=”hibernate.format_sql”>true</property> 格式化SQL
<property name=”hibernate.hbm2ddl.auto”>update</property>
<mapping resource=”cn/it/domain/Customer.hbm.xml”>
</session-factory>
</hibernate-configuration>
一、必选属性配置(5个)
1. 数据库驱动<property name=”hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>
2. 数据库URL
<property name=”hibernate.connection.url”>jdbc.mysql:///hibernate_32 </property>
3. 数据库连接用户名
<property name=”hibernate.connection.username”>root </property>
4. 数据库连接密码
<property name=”hibernate.connection.password”>123 </property>
5. 数据库方言:
<propertyname=”hibernate.dialect”>org.hibernate.MySQLDialect</property>
方言的作用:不同的数据库中,SQL语法略有区别,指定方言可以让hibernate框架在生成SQL语句时,针对数据库的方言生成。
SQL99标准:
DDL | 定义语言 | 库表的增删改查 |
DCL | 控制语言 | 事务&权限 |
DML | 操纵语言 | 增删改查 |
org.hibernate.dialect.MySQLDialect
二、可选属性配置(3个)
1. 将hibernate自动生成的sql语句打印到控制台<property name=”hibernate.show_sql”>true</property>
2. 将hibernate自动生成的sql语句格式化(语法缩近)
<property name=”hibernate.format_sql”>true</property>
注意:以上两个配置一般同时配置。
3. auto schema export 自动导出表结构(自动建表)
<property name=”hibernate.hbm2ddl.auto”>update</property>(推荐使用update)
作用:开发中不需要设计表结构,只要设计出实体对象,表通过实体对象反向生成。
正向设计:先设计实体,后设计表。(面向对象的角度)
反向设计:先设计表,后设计实体。
A | hibernate.hbm2ddl.auto | create | 自动建表 |
B | hibernate.hbm2ddl.auto | create-drop | 自动建表 |
C | hibernate.hbm2ddl.auto | update | 自动建表 |
D | hibernate.hbm2ddl.auto | validate | 不自动生成表 |
三、元数据引入配置
<mapping resource=”cn/it/domain/Customer.hbm.xml”>相关文章推荐
- Java三大框架学习之Hibernate的配置详解
- hibernate框架配置文件详解
- 8、Hibernate框架(ORM详解、主文件配置、映射文件配置)
- 【Hibernate框架学习】:Hibernate进阶之详解Hibernate配置文件和对象关系映射配置文件
- Java三大框架学习之Hibernate的配置详解
- SSH:Hibernate框架(七种关联关系映射及配置详解)
- JAVAWEB开发之Hibernate详解(一)——Hibernate的框架概述、开发流程、CURD操作和核心配置与API以及Hibernate日志的使用
- SSH:Hibernate框架(配置文件详解)
- JAVAWEB开发之Hibernate详解(一)——Hibernate的框架概述、开发流程、CURD操作和核心配置与API以及Hibernate日志的使用
- hibernate框架学习笔记2:配置文件详解
- JAVA框架之Hibernate【配置文件详解】
- SSH:Hibernate框架(七种关联关系映射及配置详解)
- Hibernate框架(一)文件配置、核心API、对象状态详解
- Hibernate框架两种配置文件详解
- Java开发框架--spring-myBatis.xml 配置详解
- Hibernate各种主键生成策略与配置详解
- hibernate.cfg.xml中配置详解
- hibernate映射文件XXX.hbm.xml配置映射元素详解--对象标识符(OID)
- Hibernate 配置详解(11)
- hibernate--5.Hibernate配置文件详解