02Hibernate常见配置

01xml代码提示配置

  • 在eclipse的widows下面选择Preferences,添加配置:
    在这里插入图片描述
  • 三个需要注意的地方已经在下面标出来了:dtd文件、uri、地址
    mapping:http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
    configuration:http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
    在这里插入图片描述

02Hibernate映射配置

名称:类名.hbm.xml。配置模板如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="" table="">
		<id name="" column=">
			<generator class="native"></generator>
		</id>
		<property name="" column=""/>
		<property name="" column=""/>
		<property name="" column=""/>
	</class>
</hibernate-mapping>
  • class标签的配置

用来建立类与表的映射关系
属性:
name : 类的全路径
table : 表名(类名与表名一致,table可以省略)
catalog :数据库名(可以不写)

  • id标签的配置

用来建立类中的属性与表中的主键的对应的关系
属性:
name:类中的属性名
column:表中的字段名(类中属性名与字段名一致,column可以省略)
length:长度(自动建表)
type:数据类型

  • property标签的配置

用来建立类中的普通属性与表中的字段的对应的关系
属性:
name:类中的属性名
column:表中的字段名(类中属性名与字段名一致,column可以省略)
length:长度(自动建表)
type:数据类型
not-null: 非空
unique:唯一

03Hibernate核心配置

名称: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.url">jdbc:mysql:///hibernate_demo</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<property name=" "></property>
		<property name=" "></property>
		
		<mapping resource=" "/>
	</session-factory>
</hibernate-configuration>
  • 必须的配置
    文件所在:bernate-release-5.0.7.Final\project\etc\hibernate.properties
## MySQL

#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password

驱动类
url路径
用户名
密码
方言

  • 可选配置

显示SQL:hibernate.show_sql
格式化SQL:hibernate.format_sql
自动建表:hibernate.hbm2ddl.auto
none:不使用hibernate自动建表
create:有表删除重建表,没有表新建表。 (测试)
create-drop:有表删除重建表,执行操作,删除表。没有表新建表,执行操作,删除表。(测试)
update:有表更新表,无表创建更新。(可以更新表结构)
validate:不会自己创建表,只更新表。(校验映射和表结构)

  • 映射文件引入

mapping标签
resource=“xxx/xxx/类名.hbm.xml”

备注:
HIbernate的两种核心配置方式:
1.xml文件的方式(抬头就是)
2.properties方式(了解)
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql:///test
hibernate.connection.username = gavin
hibernate.show_sql = true
properties方式不能引入映射文件(需要手动加载映射文件

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值