hibernate基本配置

1. 新建JAVA项目,添加JAR包(type filter text ---java build path ---Libraries --add JAR)

2. 新建库方便,省去每次引入,可以在1那个位置建,也可以type filter text--java---build path- -user libraries---new不要将下面那个方框打钩。把hibernate的lib下的所有jar包加进来,hibernate的zip包,jdbc驱动。

3. 点击项目,property 添加 library

4. hibernate配置文件, 在解压缩的包中找etc,hibernater.cfg.xml配置文件复制到src目录下,删除名字,只留标签   <session-factory></session-factory>

5. 根据etc目录下的,properties 文件配置 hibernate.cfg.xml。(便于移植)

   ## MySQL

         #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

配置文件为    

<hibernate-configuration>

         <session-factory >

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

                   <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

                   <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/hibnater_first</property>

                   <property name="hibernate.connection.username">root</property>

                   <property name="hibernate.connection.password">ygp1026</property>

         </session-factory>

</hibernate-configuration>

6. hibernate应该先建对象再建立表,这样才更OO。

7、定义实体类

8、定义User类的映射文件User.hbm.xml,eg目录下面一直找到,user.bbm.xml复制到实体所在目录,只留hibernat的mapping根,)此文件是描述实体的,一般和实体放在同一个文件目录下。(原数据描述实体类和属性之间关系的

9、将User.hbml.xml文件加入到hibernate.cfg.xml文件中:<mapping resource="com/ygp/hibernate/User.hbm.xml"/>

10. Hibernate介绍

   Ormapping配置

         <hibernate-mapping >

    <class name="com.ygp.hibernate.User"> 完整路径找到实体

       <id name="id">

           <generator class="uuid"/>         uuid是全局唯一标示

       </id>

       <property name="name"/>               普通属性用property

       <property name="password"/>

       <property name="createTime"/>

       <property name="expireTime"/>

    </class>

   

   

</hibernate-mapping>

11.编写hbm2ddl工具类,将实体类生成数据库表

12. conlose中的详细信息主要是跟log4中信息有关,可以注释掉

         log4j.rootLogger=warn, stdout不能注释,开头的不注释(注释后不便于观察工作变化,所以可以把显示SQL的属性加上如下13)

13. <property name="hibernate.show_sql">true</property>可以显示SQL,他全部控制所以不适合巨型数据

原文地址:https://www.cnblogs.com/a1280055207/p/2805487.html