hibernate入门(一)——基本环境搭建

所需要的jar包文件:
这里写图片描述
首先将这些jar包导入项目,我的项目目录如下:
这里写图片描述
在src目录下创建 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>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>

        <!--指定数据库方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <!--在控制台显示执行的数据库操作语句 -->
        <property name="hibernate.show_sql">true</property>
        <!--在控制台显示执行的数据库操作语句(格式) -->
        <property name="hibernate.format_sql">true</property>
        <!--hibernate根据实体自动生成数据库表 -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- 配置 CurrentSession -->
        <property name="hibernate.current_session_context_class">thread</property>

        <!-- 引入xml映射文件 -->
        <mapping resource="orm/entity/User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

在包orm.entity下创建类 HibernateUtil.java

package orm.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static SessionFactory factory;

    // 静态初始化块,加载配置信息,获取SessionFactory
    static {
        // 读取hibernate.cfg.xml文件
        Configuration cfg = new Configuration().configure();
        // 建立SessionFactory
        factory = cfg.buildSessionFactory();
    }

    public static SessionFactory getSessionFactory() {
        return factory;
    }
}

在包orm.entity下创建类 User.java

package orm.entity;

public class User {
    private int id;
    private String username;
    private String password;
    private int age;

    public User() {}

    public User(String username,int age) {
        this.username = username;
        this.age = age;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof User)) {
            return false;
        }
        final User user = (User) obj;
        if (this.id == user.id && this.username.equals(user.username) && this.password.equals(user.password)
                && this.age == user.age) {
            return true;
        }
        return false;
    }

    public int hashCode() {
        int result = 0;
        result = this.username.hashCode() + this.password.hashCode();
        result = 29 * age + result;
        return result;
    }

    @Override
    public String toString() {
        String str = this.getId() + ", " + this.getUsername() + ", " + this.getPassword() + ", " + this.getAge();
        return str;
    }

}

在包orm.entity下创建xml映射文件 User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="orm.entity">
    <class name="User" table="user" select-before-update="true" >
        <id name="id" column="id">
            <generator class="identity" />
        </id>

        <property name="username" column="username"/>
        <property name="password" column="password"/>
        <property name="age" column="age"/>

    </class>
</hibernate-mapping>

这样,基本的环境就搭建好了。
hibernate入门(二) —— session操作实体对象
hibernate入门(三) —— HQL查询 (Query)
hibernate入门(四) —— QBC查询 (Criteria)
hibernate入门(五) —— 关联映射

猜你喜欢

转载自blog.csdn.net/qq_35224639/article/details/80610648