编写第一个Hibernate应用程序

编写第一个Hibernate应用程序

Hibernate插件安装

hibernate插件可以快速配置与生成配置和映射文件,还有代码提示等功能。

一、 在Eclipse Marketplace中搜索jboss

Eclipse Marketplace

安装插件

二、 按照提示,安装插件,并重启Eclipse
三、 插件安装完成

创建数据库

一、 创建一个名为hibernate5的数据库
二、 创建一个t_empdetails表,用于存储员工信息并进行下面的测试与练习

create table t_empdetails(
    id int primary key auto_increment,
    name varchar(255) not null,
    gender varchar(6),
    email varchar(55)
)

程序编写

一、 创建一个maven项目

创建一个maven项目

创建一个maven项目

二、 新建一个Employee类

package cn.comman.hibernate.pojo;

public class Employee {
    private int id;
    private String name;
    private String gender;
    private String email;

    public Employee() {}

    public Employee(String name, String gender, String email) {
        super();
        this.name = name;
        this.gender = gender;
        this.email = email;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


}

三、 创建Employee的ORM映射表

可以通过刚刚安装的hibernate插件生成此文件

Employee映射表

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 25, 2017 5:13:59 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <!-- class标签里面的name对应Employee类,而table对应数据库中的员工信息表,将数据库的关系型数据映射到java对象 -->
    <class name="cn.comman.hibernate.pojo.Employee" table="t_empdetails">
        <id name="id" type="int">
            <column name="ID" />
            <!-- 生成策略 -->
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="gender" type="java.lang.String">
            <column name="GENDER" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
    </class>
</hibernate-mapping>

四、 创建hibernate配置文件

可以用hibernate插件在resources文件夹下创建一个hibernate的配置文件

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>
        <!-- 配置数据库连接驱动和信息 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate5</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>

        <!-- 数据库会话方式或方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- 在控制台打印SQL语句 -->
        <property name="show_sql">true</property>
        <!-- 格式化SQL语句 -->
        <property name="format_sql">true</property>

        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- 导入映射表的位置信息 -->
        <mapping resource="mapping/Employee.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

五、 创建一个测试单元

package cn.comman.hibernate.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import cn.comman.hibernate.pojo.Employee;

public class HibernateTest {

    @Test
    public void Test() throws Exception {
        Configuration config = new Configuration().configure();
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
        SessionFactory sessionFactory = config.buildSessionFactory(registry);

        Session session = sessionFactory.openSession();
        Transaction trans = session.beginTransaction();

        Employee emp = new Employee("Tom", "m", "888@163.com");

        session.save(emp);

        trans.commit();

        session.close();

        sessionFactory.close();
    }

}

六、 执行代码

后台打印信息

后台打印信息

查询数据库插入数据
数据库查询结果

代码运行成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值