Hibernate配置文件详解

1.orm元数据(对象与表的映射配置文件)

<?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">
<!-- 配置表与实体对象的关系 -->
<!-- package属性:填写一个包名,在内部凡是需要填写完整包名的地方只需要填写类名就行了 -->
<hibernate-mapping package="com.wenhao.domain">
    <!--
        class元素:配置实体类与表的对应关系
            name:实体类的完整类名
            table:数据库表名    
     -->
    <class name="Customer" table="cst_customer">
        <!--
            id元素:配置实体类与表中的主键关系
            name:填写主键对应的属性名
            column(可选):数据库表中的主键列名       -默认值:列名默认使用属性名
            type(可选):填写列(属性)对应的类型       -hibernate会自动检测实体的属性类型
            not-null(可选):配置列(属性)是否不能为空       -默认值为false
            length(可选):配置数据库中列的长度     -默认值:使用数据库类型的最大长度
         -->

        <id name="cust_id" column="cust_id">

                <!--
                主键生成策略: generator -就是每条记录记录时,主键的生成规则
                1. identity : 主键自增.由数据库来维护主键值,录入时不需要指定主键值
                2. sequence : Oracle中的主键生成策略
                3. increment(了解) : 主键自增,由hibernate来维护,每次查询时会先查询数据库中的最大值,然后在最大值的基础上+1
                4. hilo(了解) : 高低位算法,主键自增,由hibernate维护,开发时不使用
                5. native : hilo+sequence+identity自动三选一
                6. uuid : 产生随机字符串作为主键,主键类型必须为string类型
                7. assigned : 自然主键生成策略
             -->

            <generator class="native"></generator>
        </id>
        <!--
            除id之外的所有普通属性的配置
         -->
        <property name="cust_name" column="cust_name"></property>
        <property name="cust_user_id" column="cust_user_id"></property>
        <property name="cust_create_id" column="cust_create_id"></property>
        <property name="cust_source" column="cust_source"></property>
        <property name="cust_industry" column="cust_industry"></property>
        <property name="cust_level" column="cust_level"></property>
        <property name="cust_linkman" column="cust_linkman"></property>
        <property name="cust_phone" column="cust_phone"></property>
        <property name="cust_mobile" column="cust_mobile"></property>
    </class>

</hibernate-mapping>

对应的Customer表属性

private Long cust_id;          //客户编号(主键)
    private String cust_name;        //客户名称(公司名称)
    private String cust_user_id;     //负责人id
    private String cust_create_id;   //创建人id
    private String cust_source;      //客户信息来源
    private String cust_industry;    //客户所属行业
    private String cust_level;       //客户级别
    private String cust_linkman;     //联系人
    private String cust_phone;       //固定电话

    private String cust_mobile;      //移动电话

2.Hibernate主配置(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>
    <!--
        #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
     -->
         <!-- 数据库驱动 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 数据库URL -->
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate_01</property>
        <!-- 数据库连接用户名 -->
        <property name="hibernate.connection.username">root</property>
        <!-- 数据库连接密码 -->
        <property name="hibernate.connection.password">123456</property>
        <!-- 数据库方言
            不同数据库中的SQL语法略有差别,指定方言可以让hibernate数据库生成SQL语句时,针对数据库的方言生成
         -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
         <!--
         hibernate.show_sql true
         hibernate.format_sql true
          -->
          <!-- 将hibernate生成的SQL语句打印到控制台 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 将hibernate生成的SQL语句格式化使人能够更方便的查看 -->

        <property name="hibernate.format_sql">true</property>

        <!--
            指定hibernate中操作数据库时的隔离级别
            ## specify a JDBC isolation level
            #hibernate.connection.isolation 1|2|4|8
            0001   1   读未提交
            0010   2   读已提交
            0100   4   可重复读(mysql默认)
            1000   8   串行化
         -->
        <property name="hibernate.connection.isolation">4</property>
        
        <!--
            配置session与当前线程绑定
         -->
        <property name="hibernate.current_session_context_class" >thread</property>
       

        <!--
        ## auto schema export    自动导出表结构,自动建表
        #hibernate.hbm2ddl.auto create-drop  自动建表,每次框架运行结束会自动删除所有表(开发环境中测试使用)
        #hibernate.hbm2ddl.auto create    自动建表,框架每次运行都会重新创建新的表.以前的表会被覆盖,表数据会丢失(开发环境中测试使用)
        #hibernate.hbm2ddl.auto update(推荐使用) 自动生成表,如果已经存在不会创建,如果表有变动会自动更新(不会删除任何数据)
        #hibernate.hbm2ddl.auto validate  校验,不自动生成表,每次启动会自动校验数据库中表是否存在
         -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!--
            引入orm元数据
            路径书写;填写src下的路径
         -->
        <mapping resource="com/wenhao/domain/Customer.hbm.xml" />
        
    </session-factory>
</hibernate-configuration>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值