Hibernate
Posted 掬一束月光
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate相关的知识,希望对你有一定的参考价值。
先放入门demo
hibernate的工作原理
配置文件
对于 hibernate 的核心配置文件它有两种方式:
1. hibernate.cfg.xml
2. hibernate.properties
我们主要讲解的是 hibernate.cfg.xml 配置
1. 可以加载数据库相关信息
<property name="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url" >jdbc:mysql:///hibernateTest</property>
<property name="hibernate.connection.username" >root</property>
<property name="hibernate.connection.password" >root</property>
2.hibernate相关配置
<!-- 可以将向数据库发送的sql显示出来 -->
<property name="hibernate.show_sql" >true</property>
<!-- 格式化sql -->
<property name="hibernate.format_sql" >true</property>
<!-- hibernate的方言 -->
<property name="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property>
3.加载映射配置文件
<mapping resource="cn/itheima/domain/Customer.hbm.xml" />
对于hibernate.cfg.xml的其它配置信息可以参考下边文件里的hibernate.properties
hibernate.properties的配置也在下边文件里
映射文件配置
文件名称为.hbm.xml
其主要作用是建立表与类的映射关系.
1.统一声明包名,这样在<class>中就不需要写类的全名.
<hibernate-mapping package="cn.itheima.domain"/>
2.关于<class/>标签配置
name属性:类的全类名
talbe属性:表的名称.
catalog属性:数据库名称 可以省略.如果省略,参考核心配置文件中url路径中的库名称.
3.关于<id/>标签
首先它必须存在.<id/>是用于建立类中的属性与表中的主键的映射.
name 类中的属性名称
column 表中的主键名称 column可以省略,这时列明名就与类中的属性名一直.
length 字段长度
type 字段类型.
<generator/>主要描述主键生成策略.
4.关于<property/>标签
描述类中属性与表中非主键的映射关系.
关于hibernate映射文件中类型问题
对于type属性它的取值,可以有三种,默认是hibernate的数据类型.
1.java中的数据类型.
2.hibernate中的数据类型
3.SQL的数据类型
API
configuration.
以上是关于Hibernate的主要内容,如果未能解决你的问题,请参考以下文章
HibernateHibernate配置与sessiontransaction
hibernateHibernate SQL 方言(hibernate.dialect)
hibernateHibernate中get()和load()的区别
HibernateHibernate中使用延迟加载应该注意的事项
hibernateHibernate中save, saveOrUpdate, persist, merge, update 区别