Hibernate核心配置文件小结

在与spring整合以前 ,Hibernate核心配置文件的代码实现:

<span style="font-size:24px;"><?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_day01</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123</property>
        <!-- 数据库的方言:根据底层的数据库生成不同的SQL -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>


        <!-- 配置显示SQL -->
        <property name="hibernate.show_sql">true</property>
        <!-- 配置格式化SQL -->
        <property name="hibernate.format_sql">true</property>
        <!-- 配置hbm2ddl -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- 加载映射文件 -->
        <mapping resource="com/itheima/hibernate/domain/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
</span>
配置hbm2ddl 有null,create ,create-drop,update,validate   create每次创建都会将原先的表删除掉(测试中使用),update如果原来数据库没有表,就创建一个表,而且更新表的结构,如果原先数据库中有这些表,就只是会更新 .而validate不会创建表,只是会检测映射文件中表的配置和映射文件中的配置是否正确;

映射文件具体实体类映射关系配置文件 , 下面是映射文件的模版

<span style="font-size:24px;">引入约束:
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<?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>
    <!-- ORM:Object Relational Mapping,将实体类O和数据库的表R 建立映射关系 -->
    <class name="com.itheima.hibernate.domain.Customer" table="cst_customer">
        <!-- 类中的属性与表中的主键对应 -->
        <id name="cust_id" column="cust_id">
               主键生成策略
            <generator class="native"/>
        </id>
        <!-- 类中的属性与表中的字段对应 -->
        <property name="cust_name" column="cust_name"/>
        <property name="cust_source" column="cust_source"/>
        <property name="cust_industry" column="cust_industry"/>
        <property name="cust_level" column="cust_level"/>
        <property name="cust_phone" column="cust_phone"/>
        <property name="cust_mobile" column="cust_mobile"/>
    </class>
</hibernate-mapping>
</span>
上面的主键生成策略 ; native根据本地数据的不同自动选择,(如果实体类中id是interest和 long的适用).assigned和uuid是自动生成id的(如果id值设置为String的时候同样适用)


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值