package Dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import Bean.Student;
public class StudentDao implements dao{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
// 注意 session要强制关闭 利用try{}catch{}finally
public List<Student> showAll(){
Session session=sessionFactory.openSession();
String hql="from Student";
Query query= session.createQuery(hql);
List<Student> list= query.list();
session.close();
return list;
}
public void add(Student stu){
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(stu);
session.getTransaction().commit();
session.close();
}
// 删除单条记录
/*public void deleteStudent(Integer id){
Session session=sessionFactory.openSession();
String hql="delete Student s where s.id=?";
session.beginTransaction();
Query query=session.createQuery(hql);
query.setInteger(0,id);
int Dnum= query.executeUpdate();
System.out.println(Dnum);
session.getTransaction().commit();
session.close();
}
public void update(Integer id){
Session session=sessionFactory.openSession();
String hql="update Student s where s.id=?";
session.beginTransaction();
Query query= session.createQuery(hql);
query.setInteger(0, id);
int Unum= query.executeUpdate();
System.out.println(Unum);
session.getTransaction().commit();
session.close();
}*/
@Override
public boolean deleteStudent(Student s) {
// TODO Auto-generated method stub
Session session=sessionFactory.openSession();
session.beginTransaction();
session.delete(s);
session.getTransaction().commit();
return true;
}
@Override
public Student getStudentById(int id) {
// TODO Auto-generated method stub
Session session=sessionFactory.openSession();
return session.get(Student.class,id);
}
@Override
public boolean updateStudent(Student sdt) {
// TODO Auto-generated method stub
Session session=sessionFactory.openSession();
session.beginTransaction();
session.update(sdt);
session.getTransaction().commit();
return true;
}
}
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import Bean.Student;
public class StudentDao implements dao{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
// 注意 session要强制关闭 利用try{}catch{}finally
public List<Student> showAll(){
Session session=sessionFactory.openSession();
String hql="from Student";
Query query= session.createQuery(hql);
List<Student> list= query.list();
session.close();
return list;
}
public void add(Student stu){
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(stu);
session.getTransaction().commit();
session.close();
}
// 删除单条记录
/*public void deleteStudent(Integer id){
Session session=sessionFactory.openSession();
String hql="delete Student s where s.id=?";
session.beginTransaction();
Query query=session.createQuery(hql);
query.setInteger(0,id);
int Dnum= query.executeUpdate();
System.out.println(Dnum);
session.getTransaction().commit();
session.close();
}
public void update(Integer id){
Session session=sessionFactory.openSession();
String hql="update Student s where s.id=?";
session.beginTransaction();
Query query= session.createQuery(hql);
query.setInteger(0, id);
int Unum= query.executeUpdate();
System.out.println(Unum);
session.getTransaction().commit();
session.close();
}*/
@Override
public boolean deleteStudent(Student s) {
// TODO Auto-generated method stub
Session session=sessionFactory.openSession();
session.beginTransaction();
session.delete(s);
session.getTransaction().commit();
return true;
}
@Override
public Student getStudentById(int id) {
// TODO Auto-generated method stub
Session session=sessionFactory.openSession();
return session.get(Student.class,id);
}
@Override
public boolean updateStudent(Student sdt) {
// TODO Auto-generated method stub
Session session=sessionFactory.openSession();
session.beginTransaction();
session.update(sdt);
session.getTransaction().commit();
return true;
}
}