HibernateUtil工具类、CRUD操作

 @authorhttp://www.yiidian.com
一、抽取HibernateUtil工具类

public class HibernateUtil{

private static Configurationcfg = null;
private static SessionFactoryfactory = null;

//只需要执行1次
static{
cfg = newConfiguration();
cfg.configure();
factory =cfg.buildSessionFactory();
}

public static SessiongetSession(){
returnfactory.openSession();
}
}

二、代码使用工具类获取Session

public class Demo{

@Test
public voidtest1(){
Customer customer = newCustomer();
customer.setName("老王2");
customer.setAge(40);
customer.setGender("男");
customer.setLevel("VIP客户");

//从工具类获取Session对象
Session session =HibernateUtil.getSession();
Transaction tx =session.beginTransaction();
session.save(customer);
tx.commit();
session.close();
}
}

三、HibernateCRUD操作

1、save(obj)
2、elete(obj) 
3、get(Class,id)
4、update(obj)
5、saveOrUpdate(obj) 保存或者修改(如果没有数据,保存数据。如果有,修改数据)
6、createQuery()    HQL语句的查询的方式

public class Demo{

/ / save(Objectobj) : 保存对象
@Test
public voidtest1(){
Customer customer = newCustomer();
customer.setName("老王3");
customer.setAge(40);
customer.setGender("男");
customer.setLevel("VIP客户");
Session session =HibernateUtil.getSession();
//开启事务
Transaction tx =session.beginTransaction();
//执行操作
session.save(customer);
//提交事务
tx.commit();
//关闭
session.close();
}

/ / update(Objectobj): 更新对象
@Test
public voidtest2(){
Customer customer = newCustomer();
//给Customer的id赋值,才可以更新
customer.setId(6);
customer.setName("老王44444");
customer.setAge(45);
customer.setGender("男");
customer.setLevel("VIP客户");
Session session =HibernateUtil.getSession();
//开启事务
Transaction tx =session.beginTransaction();
//执行操作
session.update(customer);
//提交事务
tx.commit();
//关闭
session.close();
}

/ / saveOrUpdate(Objectobj): 添加或修改对象
@Test
public voidtest3(){
Customer customer = newCustomer();
//给Customer的id赋值,才可以更新
customer.setId(6);
customer.setName("老王666");
customer.setAge(45);
customer.setGender("男");
customer.setLevel("VIP客户");
Session session =HibernateUtil.getSession();
//开启事务
Transaction tx =session.beginTransaction();
//执行添加操作
session.saveOrUpdate(customer);
//提交事务
tx.commit();
//关闭
session.close();
}

/ / delete(Objectobj): 删除对象
@Test
public voidtest4(){
Session session =HibernateUtil.getSession();
//开启事务
Transaction tx =session.beginTransaction();
//执行操作
Customer customer = newCustomer();
customer.setId(7);
session.delete(customer);
//提交事务
tx.commit();
//关闭
session.close();
}

// get(Classclz,Serialize id): 获取对象
// load(Classclz,Serialize id): 获取对象
@Test
public voidtest5(){
Session session =HibernateUtil.getSession();
//开启事务
Transaction tx =session.beginTransaction();
//执行操作
//Customer cust =session.get(Customer.class, 6);
Customer cust =session.load(Customer.class, 6);
System.out.println(cust);
//提交事务
tx.commit();
//关闭
session.close();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值