天天看点

Hibernate5.x Eclipse搭建

今天写一个简单的hibernate框架搭建流程

首先准备好我们的jar包,这里附上一个jar包链接

hibernate下载 下载完jar后新建一个简单的java工程,结构如下
Hibernate5.x Eclipse搭建

项目结构

接下来编写我们的hibernate的配置文件

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 该声明可以到org/hibernate/hibernate-configuration-3.0.dtd查看-->
<!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:///hibernate</property>
        <!-- 链接数据库的用户名 -->
        <property name="hibernate.connection.user">root</property>
        <!-- 密码 -->
        <property name="hibernate.connection.password">root</property>
        <!-- 是否显示SQL语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 链接的编码 -->
        <property name="connection.characterEncoding">utf8</property>
        <!-- hibernate使用的方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- 映射文件 -->
        <mapping resource="com/education/bean/User.hbm.xml" />
    </session-factory>
</hibernate-configuration>
           

下面列举一些常用的属性以及一些常用的方言设置

属性名 作用
hibernate.dialect 为所选数据库使用其SQL
hibernate.connection.driver_class JDBC驱动
hibernate.connection.url 链接地址
hibernate.connection.pool_size 连接池的数量
hibernate.connection.autocommint 自动提交模式

方言设置(可以在

org/hibernate/dialect

中找到)

数据库 属性
SQL Server 2000 org.hibernate.dialect.SQLServerDialect
SQL Server 2008 org.hibernate.dialect.SQLServer2008Dialect
MySQL org.hibernate.dialect.MySQLDialect
Oracle org.hibernate.dialect.OracleDialect

创建一个简单的User类,定义了三个属性以及他们的

get/set

方法

package com.education.bean;

public class User {
    
    private int id;
    
    private String name;
    
    private String password;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

    public int getId() {
        return id;
    }

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

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", password=" + password
                + "]";
    }

    
}

           

我们要做的是将数据库的表与实体类相连接,所以需要创建一个映射文件,映射文件的命名方式为

className.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 可以在org/hibernate/hibernate-mapping-3.0.dtd中找到 -->
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<!-- 指明为哪个包下的,如果此处的package没有指明,则下面的class中需要全名 -->   
<hibernate-mapping package="com.eduask.bean">
    <!-- 规定该类对应哪张表 -->
    <class name="User" table="user">
        <!-- 主键使用id标签,name为属性名与column列名相对应 -->
        <id name="id" type="int" column="id">
            <!-- 主键增长设置 -->
            <generator class="native"></generator>
        </id>
    <!-- 其余属性映射 type是java类型与数据库类型的对应设置 -->
    <property name="name" type="string" column="username"/>
    <property name="password" type="string" column="passwd"></property>     
    </class>

</hibernate-mapping>
           

附上自增长类型表与对应类型表

生产器 说明
increment 从数据库取出主键的最大值,然后递增1
identity 使用数据库的自增长策略
sequence 只能在支持序列的数据中使用,如oracle
native 由hibernate根据数据库自动选择

identity/hilo/sequence

中的任意一种
assigned 在外部生成,在save前必须指定一个
java类型 映射类型 sql类型
int/Integer integer
long/Long long bigint
short/Short short samllint
byte/Byte byte tinyint
float/Float float
double/Double double
String string varchar
boolean/Boolean true/false char(1)('T'/'F')
Date date
Date/Time time
Date/Timestamp timestamp
Clob clob
Blob blob

最后编写一个测试类来测试一下我们的hibernate框架是否搭建成功

package com.education.controller;

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

import com.education.bean.User;

public class IndexController {

    public static void main(String[] args) {
        
        //configuration  主配置类   负责读取hibernate运行的底层信息
        //sessionFactory 会话工厂   充当数据源,负责创建session对象
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        //session         会话     应用程序与数据库的一次交互,包含了一般的CURD方法
        Session session = sessionFactory.openSession();
        //实例化出一个对象
        User user = new User();
        user.setName("隔壁老王");
        user.setPassword("123123");
        //使用事务管理
        Transaction transcation = session.getTransaction();
        //开始事务
        transcation.begin();
        //此时数据不会真的保存到数据库中
        session.save(user);
        //提交事务后,数据才会真正的保存到数据库中
        transcation.commit();
    }
}

           

控制台已经将sql语句答应出来了

Hibernate5.x Eclipse搭建

sql语句

最后看一下数据库中是否添加成功

Hibernate5.x Eclipse搭建

数据库.jpg

到此为止,hibernate的框架搭建完毕。