SSH-Hibernate(1)

一.Hibernate概述

1.hibernate是一个轻量级的开源框架;

2.hibernate应用于JavaEE三层结构中的dao层

3.在dao层里面不再需要jdbc复杂的代码操作数据库,不需要写sql语句就可以实现对数据库的crud操作。

二.Hibernate中的orm思想

orm(Object Relational Mapping):对象关系映射。

概述:通俗说就是把对象与数据库中的表对应,把对象中的属性与表中的字段对应,不需要操作数据库表,可以

直接操作对象。

三.Hibernate入门

1.导入jar包


2.创建实体

package com.lgh.entity;

/**
 * Created by LGH on 2018/3/8.
 */
public class User {
    //hibernate要求实体类有一个唯一属性
    private String id;
    private String userName;
    private String password;

    public String getId() {
        return id;
    }

    public void setId(String 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;
    }
}

3.配置实体类与数据库表映射

(1)在src下创建User.hbm.xml配置文件

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lgh.entity">
    <!--配置表和类映射关系-->
    <class name="User" table="user">
        <id name="id" column="uid">
            <!--设置数据库表id生成策略
                native:生成表id值就是主键自动增长
            -->
            <generator class="native"></generator>
        </id>
        <property name="userName" column="userName"/>
        <property name="password" column="password"/>
        
    </class>
</hibernate-mapping>

4.创建hibernate核心配置文件(hibernate.cfg.xml)

文件名固定,必须在src文件夹下

<!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>
        <!--1.配置数据库信息-->
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:mldn</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">12345</property>

        <!--2.配置hibernate信息-->
        <!--输出sql语句-->
        <property name="hibernate.show_sql">true</property>
        <!--sql语句格式化-->
        <property name="hibernate.format_sql">true</property>
        <!--hiebernate帮创建表
            update:如果已经有表,更新,如果没有表自动创建
        -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!--配置方言,识别不同的数据库特有语句-->
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <!--在当前用户空间中寻找表信息-->
        <property name="default_schema"/>
        <!--3.配置映射配置文件-->
        <mapping resource="com/lgh/entity/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

5.添加数据测试

package com.lgh.test;

import com.lgh.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

/**
 * Created by LGH on 2018/3/8.
 */
public class HibernateTest {
    @Test
    public void add(){
        //加载hibernate核心配置文件
        Configuration cfg= new Configuration();
        cfg.configure();
        //创建sessionFactory对象(在此过程中把数据库的表创建)
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        //创建session(类似于Connection)
        Session session = sessionFactory.openSession();
        //开启事务
        Transaction transaction = session.beginTransaction();
        //编写crud操作
        User user = new User();
        user.setUserName("李");
        user.setPassword("12345");
        session.save(user);
        //提交事务
        transaction.commit();
        //关闭资源
        session.close();
        sessionFactory.close();
    }
}

四.Hibernate核心API

Configuration:用于启动,加载hibernate配置文件

Configuration cfg= new Configuration().configure();

此种方式默认在src下加载hibernate.cfg.xml配置文件,如需在指定文件夹下加载配置文件,如下:

Configuration cfg= new Configuration().configure("path");

SessionFactory:负责建立,初始化Session对象,在hibernate中它起到一个缓冲区的作用,可以将自动生成的SQL,

映射数据以及重复利用的数据放在这个缓冲区,同时保存了对数据库配置的所有映射关系,维护了当前二级缓存。

SessionFactory通过Configuration对象获取:

SessionFactory sessionFactory = cfg.buildSessionFactory();

SessionFactory具有以下特点:

线程安全,它的同一个实例可以供多个线程共享;

重量级,不能随意创建和销毁。

一般情况下SessionFactory在项目中只创建一个对象,只有当应用中存在多个数据源时,才会为每个数据源提供一个SessionFactory,在开发时可抽取一个工具类

public class HibernateUtils {
    private static final Configuration cfg;
    private static final SessionFactory sessionFactory;
    static {
        cfg=new Configuration().configure();
        sessionFactory=cfg.buildSessionFactory();
    }
}

Session:为持久化对象提供持久化操作。

两种方法获取Session

方式一:

Session session=sessionFactory.openSession();

方式二:

Session sessionsessionFactory.getCurrentSession();

采用openSession()方法获取的Session实例时,SessionFactory直接创建一个Session对象,使用完毕后close手动关闭,

采用getCurrentSession()方法获取Session实例,实例会被绑定到当前线程中,它在提交或者回滚操作时自动关闭。

session是线程非安全的,只能自己使用,不能共用。


Transaction:事务对象

commit:提交事务

rollback:事务回滚

session操作完数据后,必须提交事务才能将数据写到数据库中,如果发生异常采用rollback事务回滚。











.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值