使用hibernate框架连接oracle数据库进行简单的增删改

初始化配置和session

  关于配置文件这里就不在赘述了,假设配置文件配好后我们需要加载配置和sessionFactory,并获取session,因为每次进行增删改查时都需要session,所以封装成了一个工具类

package tool;

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

public class HibernateUtil {
    private static Configuration configuration;
    private static SessionFactory sessionFactory;
    //初始化配置和sessionFactory
    static {
        try {
            configuration = new Configuration().configure();
            sessionFactory =configuration.buildSessionFactory();
        } catch (HibernateException e) {
            throw new ExceptionInInitializerError();
        }
    }
    private HibernateUtil(){}
    //获取session对象
    public static Session currentSession(){
        return sessionFactory.getCurrentSession();
    }
}

添加

dao层

public void addEmp(Emp emp){
        //添加 保存所传过来的对象
        HibernateUtil.currentSession().save(emp);
    }

service层

public void add(Emp emp){
        Transaction tx=null;
        try {
            //打开事务
            tx=HibernateUtil.currentSession().beginTransaction();
            empDao.addEmp(emp);
            //提交
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx!=null)
                //回滚
                tx.rollback();
        }
    }

测试类

public static void main(String[] args){
        EmpSerivce empSerivce = new EmpSerivce();
        Emp emp = new Emp();
        emp.setEmpno(7903);
        emp.setEname("张三");
        emp.setDeptNo(20);
        emp.setHiretDate(Date.valueOf("2015-06-24"));
        emp.setJob("CLERK");
        emp.setSal(9000);
        empSerivce.add(emp);
    }

结果 

修改

 dao层

public Emp update(Serializable id){
        //加载需要修改的对象
        return (Emp)HibernateUtil.currentSession().get(Emp.class,id);
    }

service层

public void updateEmp(){
        Transaction tx = null;
        try {
            tx= HibernateUtil.currentSession().beginTransaction();
            Emp empUpdate = empDao.update(7903);
            empUpdate.setSal(9999);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();  //回滚事务
        }
    }

测试类

 public static void main(String[] args) {
       EmpService empService = new EmpService();
       empService.updateEmp();
    }

结果:可以看到修改成功了 从9000 变成了 9999

删除

dao层

public void delete(Emp emp){
        //删除指定的对象
        HibernateUtil.currentSession().delete(emp);
}

service层

public void delEmp(Integer id){
        Transaction tx =null;
        try {
            tx=HibernateUtil.currentSession().beginTransaction();
            empDao.delete(empDao.update(id));//使用update获得指定对象 通过delete删除
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
}

测试类

  public static void main(String[] args) {
        EmpService empService = new EmpService();
      //删除
        empService.delEmp(7903);
    }

结果:

 

posted @ 2018-11-19 10:31  不--易  阅读(719)  评论(0编辑  收藏  举报