增已经在之前完成过了
删
@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();
}
“`