第一个 Hibernate应用程序(手动编写)

1.新建一个java工程。加入hibernate的jar包

Hibernate不一定应用在web工程中,一个简单的java 工程也可以应用Hibernate
这里写图片描述

2. 在src目录下新建hibernate配置文件,文件名称为hibernate.cfg.xml

hibernate.cfg.xml
这里使用的数据库为oracle数据库

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- 数据库方言 hibernate.是可以省略 -->
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="hibernate.connection.username">scott</property>
        <property name="hibernate.connection.password">admin</property>

        <!-- 关联映射文件 -->
        <mapping resource="com/demo/pojo/TbUser.hbm.xml" />
    </session-factory>
</hibernate-configuration>


3.在数据库中建立表:

create table tb_demo
(
       id int primary key,
       username varchar2(100),
       password varchar2(100),
       sex varchar2(2),
       address varchar2(100)
)

4.在src目录建立pojo 包,新建TbDemo.java实体类。

package com.demo.pojo;

public class TbDemo {
    private int id;
    private String username;
    private String password;
    private String sex;
    private String address;
    public TbDemo() {
        super();
    }
    public TbDemo(String username, String password, String sex, String address) {
        super();
        this.username = username;
        this.password = password;
        this.sex = sex;
        this.address = address;
    }
    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 String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "TbDemo [address=" + address + ", password=" + password
                + ", sex=" + sex + ", username=" + username + "]";
    }


}

5 .在与TbDemo类相同的包下建立TbDemo.hbm.xml文件。文件内容如下:

TbDemo.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.demo.pojo">
    <class name="TbDemo" table="tb_demo">
        <!-- name属性值是TbUser类中的属性,column="id"是tb_user表的字段名,如果字段名和屬性名是相同的,那麼可以省略不寫 -->
        <id name="id" column="id" type="int">
            <!-- 主键生成策略,如果指定为sequence,表示使用序列号产生ID的值,但是我们没有指定序列名称的时候,默认使用的是hibernate提供的序列 -->
            <generator class="sequence">
                 <param name="sequence">tb_user_seq</param> 
            </generator>
        </id>
        <property name="username" column="username"></property>
        <property name="password"></property>
        <property name="sex"></property>
        <property name="address"></property>
    </class>
</hibernate-mapping>

注意:需要在hibernate.cfg.xml文件中关联此文件。


6.编写工具类,用来获取SessionFactory

package com.demo.util;

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

public class HibernateSessionFactory {
    // 私有的成员变量
    private static SessionFactory sessionFactory;

    // 私有的构造方法
    private HibernateSessionFactory() {

    }

    // 通过静态代码块进行初始化操作
    static {
        // 默认会去读取src/hibernate.cfg.xml文件
        Configuration cfg = new Configuration();
        cfg = cfg.configure();
        // 获取sessionFactory
        sessionFactory = cfg.buildSessionFactory();
    }

    /**
     * 对外提供获取SessionFactory对象的方法
     * @return
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

7. 测试类

package com.demo.test;
/**
 * @author 作者 E-mail:
 * @version 创建时间:2016-11-9 上午11:13:28
 * 类说明
 */

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import com.demo.pojo.TbDemo;
import com.demo.util.HibernateSessionFactory;

public class Test {
    public static void main(String[] args) {
        // 1、获取SessionFactory
        SessionFactory sessionFactory = HibernateSessionFactory
                .getSessionFactory();

        // 2、根据session工厂获取Session对象
        Session session = sessionFactory.openSession();

        // 开启事务
        Transaction tran = session.beginTransaction();
        // 3、执行session中的方法
        try {
            session.save(new TbDemo("admin", "123456", "男", "地址不详"));
            // 提交事务
            tran.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            //回滚事务
            tran.rollback();
        }
        // 4、需要记得关闭session
        session.close();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值