Hibernate学习笔记1

  Hibernate是持久化解决方案中ORM框架中的一种,它的意义在于解决面向对象语言和关系型数据库之间的存储、查询的转换关系。其中ORM
  使用Hibernate的步骤:
(1)加入hibernate的jar包   
(2)创建持久化类(pojo)
    Hibernate对pojo的要求 : 
     属性要有对应的get和set方法 
     要有无参数的默认构造方法
     不要使用final进行修饰 
(3)创建映射文件(和pojo类放在一个包中),文件名以pojo类的类名开始加.hbm.xml
     例如:pojo类名为User的对应的映射文件名为:User.hbm.xml
    建映射文件第一步:导入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 package="com.xue.entity">
        <class table= "t_user" name="com.xue..entity.User ">
            <id name="id" column="id">
                 <generator class="native"/>
             </id>
            <property name="username"/>
            <property name="password" column="password"/>
        </class>
   </hibernate-mapping> 
(4)创建配置文件(hibernate.cfg.xml)
   第一步:导入dtd
      <?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.dialect">org.hibernate.dialect.MySQLDialect</property>
            <property       name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
          <property name="hibernate.connection.url">jdbc:mysql:///mydb</property>
       <property name="hibernate.connection.username">root</property>
                <property name="hibernate.connection.password"></property>
                <property name="hibernate.current_session_context_class">thread</property
                <property name="hibernate.show_sql">true</property>
                <property name="hibernate.connection.pool_size">1</property>                 
                <mapping resource="com/xue/entity/User.hbm.xml"/>
        </session-factory>
</hibernate-configuration> 

未完待续----

猜你喜欢

转载自xueyanping2012.iteye.com/blog/1847343