?? studentdaoimp.java
字號:
package com.stuman.dao.imp;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import com.stuman.dao.StudentDAO;
import com.stuman.dao.hibernate.HibernateUtil;
import com.stuman.domain.Student;
public class StudentDAOImp implements StudentDAO {
private static Log log = LogFactory.getLog(StudentDAOImp.class);
public List getStudent() {
// TODO Auto-generated method stub
try {
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
List results = s.createQuery("from Student stu").list();
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
if (results != null && results.size() > 0) {
return results;
}
} catch (HibernateException e) {
log.fatal(e);
}
return null;
}
public boolean deleteStudentByID(String id) {
// TODO Auto-generated method stub
try {
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
Student stu = (Student) s.load(Student.class, id);
HibernateUtil.commitTransaction();
// System.out.println(stu.getId());
HibernateUtil.beginTransaction();
s.delete(stu);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
return true;
} catch (HibernateException e) {
log.fatal(e);
}
return false;
}
public Student getStudentByID(String id) {
// TODO Auto-generated method stub
try {
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
Student stu = (Student) s.load(Student.class, id);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
return stu;
} catch (HibernateException e) {
log.fatal(e);
}
return null;
}
public boolean updateStudent(Student student) {
// TODO Auto-generated method stub
try {
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
s.update(student);
System.out.println("update student id =" + student.getId());
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
return true;
} catch (HibernateException e) {
log.fatal(e);
}
return false;
}
public boolean saveStudent(Student stu) {
// TODO Auto-generated method stub
try {
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
s.saveOrUpdate(stu);
System.out.println("save student id =" + stu.getId());
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
return true;
} catch (HibernateException e) {
log.fatal(e);
}
return false;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -