zoukankan      html  css  js  c++  java
  • 使用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);
        }

    结果:

  • 相关阅读:
    Java开发web的几种开发模式
    Tomcat7.0安装配置
    Apache与Tomcat 区别联系
    编写 Window 服务程序
    系列文章--SharePoint 开发教程
    Google Chrome浏览器调试功能介绍
    Chrome的JS调试工具
    Google Chrome 调试JS简单教程[更新]
    ASP.NET常用标准配置web.config
    WINDOWS下kill进程的命令
  • 原文地址:https://www.cnblogs.com/hfx123/p/9981431.html
Copyright © 2011-2022 走看看