最重要的代码
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;
}