`

Hibernate配置文件

阅读更多

Hibernate包含两种配置文件,如下:

 

1. 类与表的映射文件(*.hbm.xml),部分代码如下

 

    ......

    <hibernate-mapping>

 

    /*

        name: 类的完整路径

        table: 数据库中对应的表名

     */
    <class name="org.dreamweav.lzc.hibernate.Person" table="person" catalog="hibernate">

        

        /*

             id: 主键,type:类型,column:对应表中的字段

       */
        <id name="id" type="string">
            <column name="id" length="1" />
            <generator class="assigned" />
        </id>

       

        /*

           property: java类的属性值,type:熟悉的类型

           column: 对应的表中的字段,后面跟上一些表中字段的约束

        */
        <property name="name" type="string">
            <column name="name" length="20" not-null="true" />
        </property>


        <property name="age" type="integer">
            <column name="age" />
        </property>


        <property name="sex" type="string">
            <column name="sex" length="2" />
        </property>


    </class>


    </hibernate-mapping>

 

2. 整体的配置文件(hibernate.cfg.xml),部分代码如下

 

    <hibernate-configuration>

 

             <session-factory>

                     

                     // 数据库登陆用户名
                     <property name="connection.username">root</property>
                    

                     // 数据库登陆密码

                     <property name="connection.password">123456</property>

 

                     // 数据库连接地址
                     <property name="connection.url">jdbc:mysql://localhost:3306/数据库名</property>
                    

                     // 数据库的方言,告诉数据库使用的是哪种数据库

                     <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

 

                     // 根据属性生成表结构(create-drop(程序结束删除表,执行时创建),create,update(更新),validate(校验,只会报错))

                    <property name="hbm2ddl.auto">create</property>
  
                     <property name="myeclipse.connection.profile">mysql-hibernate</property>
  
                     // 数据库驱动名称,此处用的是mysql
                     <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  

                     // 打印sql语句
                     <property name="show_sql">true</property>
  

                     // 注册关联文件
                     <mapping resource="org/dreamweav/lzc/hibernate/Person.hbm.xml" />

 

           </session-factory>

 

</hibernate-configuration>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics