hibernate 配置详解(笔记四)

这篇博客详细介绍了Hibernate的映射配置,包括class标签的使用,如name、table、catalog属性,以及id和property标签的属性设置,如name、column、length、type等。此外,还讲解了Hibernate的核心配置,如数据库连接参数、方言设置,以及show_sql、format_sql、hbm2ddl.auto等可选配置的含义和用途。最后,提到了hibernate.properties和hibernate.cfg.xml两种配置文件的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

映射配置:

例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
     PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping>
	<!-- 建立类与表的映射 -->
	<class name="com.test.Customer" table="cst_customer">
		<!-- 建立类中的属性与表中的主键对应 -->
		<id name="cust_id" column="cust_id" type="java.lang.Long"  catalog="" >
			<generator class="sequence" >
			 <param name="sequence_name">cust_id_sequence</param>  
			</generator>
		</id>

		<!-- 建立类中的普通的属性和表的字段的对应 -->
		<property name="cust_name" column="cust_name" type="java.lang.String" length="32"  />
		<property name="cust_source" column="cust_source" type="java.lang.String" length="32" />
		<property name="cust_industry" column="cust_industry" type="java.lang.String" length="32" />
		<property name="cust_level" column="cust_level" type="java.lang.String" length="32" />
		<property name="cust_phone" column="cust_phone" type="java.lang.String" length="64" />
		<property name="cust_mobile" column="cust_mobile" type="java.lang.String" length="16" />
		
	</class>

</hibernate-mapping>

【class标签】

    ■class标签用来建立表的映射关系

    ■属性:

      ▲name       :类的路径。

      ▲table       :表名(类名与表名 可省了 table)

      ▲catalog : 数据库名(可以不填)

  ■【id标签】

  ■标签用建立类中的属性与表中的主键的对应关系

  ■属性:

         ▲name          :类中的属性名。

         ▲column       :表中的字段。(类中的属性名 和 表中的字段名 一样 可省略 column)

         ▲length         :长度。

         ▲type            :数据类型。

  ■【id标签】

  ■标签用来建立类中普通属性与表字段对应关系:

 ■属性:

         ▲name          :类中的属性名。

         ▲column       :表中的字段。 

         ▲length         :长度。

         ▲type            :数据类型。

         ▲not-null      :不为空。

         ▲unique       :设置唯一。

type 和length 是hibernate 自动创建表使用


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">oracle.jdbc.driver.OracleDriver</property>
		<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1522:orcl</property>
		<property name="hibernate.connection.username">hncdsite</property>
		<property name="hibernate.connection.password">power123</property>
		<!-- 配置Hibernate的方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
		<!-- 可选配置 -->
		<!-- 打印sql -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化sql -->
		<property name="hibernate.format_sql">true</property>
		<!-- 自动创建表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<mapping resource="com/test/customer.hbm.xml" />
	</session-factory>
</hibernate-configuration>

  ●必须配置

        ■连接数据库的基本参数

        ▲驱动类

        ▲url路径

        ▲用户名

        ▲密码

    ■  方言   

    ●  可选配置

           ■打印sql   : hibernate.show_sql

           ■格式化sql   : hibernate.format_sql

           ■自动创建表  : hibernate.hbm2ddl.auto

                     ▲none   :不使用。

                     ▲create :如果数据库有表删除表再新建表,如果没有表新建表(测试)。

                     ▲create-drop:(测试)如果数据库有表删除表,执行代码,在删除这个表。如果没有表新建,使用完成再删除表

                    (java需要sesionFactory.close();)

                      ▲update:如果数据库有表使用原有表,如果没有表创建新表,和表结构的更新操作。

                      ▲validate:校验,如果没有表不会创建表,只会使用数据库的表,但会校验映射。

     ●  映射文件引入:

<mapping resource="com/test/customer.hbm.xml" />

hibernate 配置文件有两种方式:

 一 . 属性文件的方式  :hibernate.properties

配置用等于号连接   hibernate.connection.driver_class=com.mysql.jdbc.Driver; 

属性文件不能引入映射文件(自己编写映射文件)

 二 . XML文件 方式   :hibernate.cfg.xml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值