Hibernate学习(3)hibernate实现增删改查

增已经在之前完成过了


 @Test
    public void test2(){
        System.out.println("test....");
        TUserEntity tUserEntity = new TUserEntity();    


        //单例模式创建会话工厂对象
        SessionFactory sessionFactory = SessionFactoryUtil.getSession();
        //会话对象
        Session session = null;
        //开启事务
        Transaction transaction = null;
        //删
        session = sessionFactory.openSession();;
        transaction = session.beginTransaction();
        tUserEntity.setUid(1);
        tUserEntity.setUsername("王琛昱");
        tUserEntity.setPassword("123456");
        tUserEntity.setAge(18);
        session.delete(tUserEntity);

        //提交事务
        transaction.commit();
        //关闭事务
        session.close();
        sessionFactory.close();
    }

这里写图片描述


@Test
    public void test3(){
        System.out.println("test....");
        TUserEntity tUserEntity = new TUserEntity();


        //单例模式创建会话工厂对象
        SessionFactory sessionFactory = SessionFactoryUtil.getSession();
        //会话对象
        Session session = null;
        //开启事务
        Transaction transaction = null;

        //改(方法一)
        session = sessionFactory.openSession();;
        transaction = session.beginTransaction();
        tUserEntity.setUid(4);
        tUserEntity.setUsername("小王");
        tUserEntity.setAge(4);
        tUserEntity.setPassword("555555555");
        session.update(tUserEntity);
        //提交事务
        transaction.commit();
        //关闭事务
        session.close();
        sessionFactory.close();


    }

这里写图片描述


“`
@Test
public void test4(){
System.out.println(“test….”);
TUserEntity tUserEntity = new TUserEntity();

    //单例模式创建会话工厂对象
    SessionFactory sessionFactory = SessionFactoryUtil.getSession();
    //会话对象
    Session session = null;
    //开启事务
    Transaction transaction = null;

    //查(方法一)
    session = sessionFactory.openSession();;

// transaction = session.beginTransaction();
tUserEntity = (TUserEntity) session.get(TUserEntity.class,2);
System.out.println(tUserEntity.getUsername());
System.out.println(tUserEntity.getAge());
System.out.println(tUserEntity.getUid());
System.out.println(tUserEntity.getPassword());
//提交事务
// transaction.commit();
//关闭事务
session.close();
sessionFactory.close();
}

“`这里写图片描述

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页