hibernate(一)

最重要的代码

1.private Session session = null;

2.Configuration config = new Configuration().configure();


3. SessionFactory factory = config.buildSessionFactory();


4. this.session = factory.openSession(); 

5.Transaction tran = this.session.beginTransaction();

6.tran.commit();

public PersonOperator() {
  Configuration config = new Configuration().configure();
  SessionFactory factory = config.buildSessionFactory();
  this.session = factory.openSession();
 }
 public void insert(Person p) {
  Transaction tran = this.session.beginTransaction();
  this.session.save(p);
  tran.commit();
 }
 public void update(Person p) {
  Transaction tran = this.session.beginTransaction();
  this.session.update(p);
  tran.commit();
 }
 public Person queryById(String id) {
  Person p = null;
  String hql = "From Person as p where p.id=?";
  Query q = this.session.createQuery(hql);
  q.setString(0, id);
  List l = q.list();
  Iterator iter = l.iterator();
  while(iter.hasNext()) {
   p = (Person)iter.next();
  }
  return p;
 }
 public void delete(String id) {
  String hql = "delete from Person where id=?";
  Query q = this.session.createQuery(hql);
  q.setString(0,id);
  q.executeUpdate();
  this.session.beginTransaction().commit();
 }
 
 public List quaryAll() {
  List l= null;
  String hql = "From Person as p";
  Query q = this.session.createQuery(hql);
  l = q.list();
  return l;
 }
 
 public List queryByLike(String cond) {
  List l = null;
  String hql = "From Person as p where p.name like ?";
  Query q = this.session.createQuery(hql);
  q.setString(0,"%"+cond+"%");
  l = q.list();
  return l;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值