Hibernate项目环境搭建

1、首先在eclipse里面新建一个Java工程。

      

2、在数据库中新建一个数据库(无需创建表,有Hibernate生成)。

  

3、在项目中导入Hibernate所依赖的jar包,该jar包可以在此下载:Hibernate所需jar包

  

4、创建一个实体类。

  

public class UserEntity {
    /*
     * Hibernate要求在实体中必须要有一个
     * 属性值唯一的属性。
     * 在该实体中uid作为id,是唯一的
     * */
    private int uid;
    private String name;
    private String pwd;
    private int age;
    /**
     * @return the uid
     */
    public int getUid() {
        return uid;
    }
    /**
     * @param uid the uid to set
     */
    public void setUid(int uid) {
        this.uid = uid;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the pwd
     */
    public String getPwd() {
        return pwd;
    }
    /**
     * @param pwd the pwd to set
     */
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }
    

}

5、编写实体映射文件

  使用配置文件实现映射关系。

  (1)创建xml格式的配置文件。

    - 映射配置文件名称和位置没有固定要求。

    - 建议:在实体类所在包里面创建,实体类名称.hbm.xml。

  (2)配置是是xml格式,在配置文件中首先引入xml约束。

    -在hibernate里面引入的约束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>
        <!-- 
            class 里面写实体和表的映射关系
            name 里面是实体类的全路径
            table 是数据表的名字
         -->
        <class name ="com.jack.entity.UserEntity" table="t_user">
            <!-- 
                hibernate要求实体有一个属性值唯一的属性
                也要求数据表中有一个值唯一的字段
             -->
             <!-- 
                name 实体里面的id属性的名称(必须要一致)
                column 生成的数据表中字段的名称(可以随便起名,也可以不写,
                    不写默认生成的和name的值一致)
              -->
            <id name="uid" column="uid">
              <!-- 
                  generator 用来配置数据库id的增长策略
                              native表示 主键自增长
               -->
               <generator class="native"></generator> 
            </id>
            
            <!-- 
                property 用来配置其他属性
                name 实体类里面属性的名称(必须一致)
                 column 生成的数据表中字段的名称(可以随便起名,也可以不写,
                    不写默认生成的和name的值一致)
             -->
            <property name="name" column="username"></property>
            <property name="pwd" column="password"></property>
            <property name="age" column="age"></property>
        </class>  
    </hibernate-mapping>  

6、创建Hibernate的核心配置文件。

  (1)核心配置文件格式xml,但是核心配置文件名称和位置固定的。

    - 位置:必须src下面。

    - 名称:必须hibernate.cfg.xml。

  (2)引入dtd约束。

  (3)hibernate操作过程中,只会加载核心配置文件,其他配置文件不会加载。

    

<?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标签内
             -->
        <session-factory>
            <!-- 
                 第一部分:写数据库的配置 该部分必须要配置
             -->
             <!-- 数据库驱动配置 -->
             <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
             <!-- 数据库连接URL -->
             <property name="hibernate.connection.url">jdbc:mysql:///hibernatetest</property>
             <!-- 数据库连接用户名 -->
             <property name="hibernate.connection.username">root</property>
             <!-- 数据库连接密码 -->
             <property name="hibernate.connection.password">root</property>
             
             
             <!-- 
                 第二部分:hibernate配置   该部分可以省略
              -->
              <!-- 在控制台打印sql语句 -->
              <property name="hibernate.show_sql"> true</property>
              <!-- 打印sql时格式化该语句输出 -->
              <property name="hibernate.format_sql">true</property>
              <!-- 配置自动生成表 -->
              <property name="hibernate.hbm2ddl.auto">update</property>
              <!-- 配置数据库方言 -->
              <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
              
              <!-- 
                  第三部分:引入映射配置文件    该部分必须要配置
                  resource 映射文件的相对路径。
               -->
              <mapping  resource="com/jack/entity/User.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>

7、编写测试代码

  

public class HibernateTest {
    
    @Test
    public void Test1(){
        //1、创建configuration对象
            Configuration cfg = new Configuration();
        //2、加载核心配置文件hibernate.cfg.cxml
            cfg.configure();
        //3、创建SessionFactory对象
            SessionFactory sessionFactory = cfg.buildSessionFactory();
        //4、创建session对象
            Session  session = sessionFactory.openSession();
        //5、开启事物
            Transaction tx = session.beginTransaction();
        //6、编写测试代码
            UserEntity userEntity = new UserEntity();
            userEntity.setName("张三");
            userEntity.setPwd("12345");
            userEntity.setAge(25);
            session.save(userEntity);
        //7、提交事物
            tx.commit();
        //8、释放资源
            session.close();
            sessionFactory.close();
    }

}

8、项目优化

  创建sessionFactory过程中,这个过程特别耗资源的.

   (1)在hibernate操作中,建议一个项目一般创建一个sessionFactory对象.

 (2)解决方法:写工具类,写静态代码块实现.

public class HibernateUtils {
    
    private static Configuration configuration= null;
    private static SessionFactory sessionFactory = null;
    
    /**
     * 静态代码块,在类加载时只会执行一次,这样就可以保证只有一个SessionFactory对象
     */
    static{
        configuration = new Configuration();
        configuration.configure();
        sessionFactory = configuration.buildSessionFactory();
    }

    /**
     * 提供一个返回SessionFactory对象的方法
     */
    
    public static SessionFactory getFactory(){
        return sessionFactory;
    }
}

9、测试结果

  测试执行成功,并且在控制台打印出来了格式化后的sql语句。

             

   数据库按我们在映射文件中配置的生成了表。并插入了一条记录。

                

  数据库字段的类型进行了自动匹配,主键是自增长类型。

          

项目源代码:Hibernate01.zip

原文地址:https://www.cnblogs.com/jack1995/p/6934815.html