加入收藏 | 设为首页 | 会员中心 | 我要投稿 威海站长网 (https://www.0631zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 移动互联 > 应用 > 正文

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

发布时间:2022-12-17 17:31:21 所属栏目:应用 来源:转载
导读: 1.新建一个java工程。加入hibernate的jar包
Hibernate不一定应用在web工程中,一个简单的java 工程也可以应用Hibernate

2. 在src目录下新建hibernate配置文件应用程序编写,文件名称为hib

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"?>

<hibernate-configuration>
    <session-factory>
        
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialectproperty>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriverproperty>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orclproperty>
        <property name="hibernate.connection.username">scottproperty>
        <property name="hibernate.connection.password">adminproperty>
        
        <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"?>

<hibernate-mapping package="com.demo.pojo">
    <class name="TbDemo" table="tb_demo">
        
        <id name="id" column="id" type="int">
            
            <generator class="sequence">
                 <param name="sequence">tb_user_seqparam> 
            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();
    }
}

(编辑:威海站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章