如何在Hibernate中使用DAO方法从数据库中检索,更新,删除数据。
我的DAO看起来像这样:
package com.sample.common.impl;
import java.util.List;
import com.sample.common.Employee;
public interface EmployeeDao {
public List<Employee> getAllEmployee();
public void updateEmployee(Employee emp);
public void deleteEmployee(Employee emp);
}
我的实现类看起来像这样:
package com.sample.common.impl;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import com.sample.common.Employee;
public class EmployeeDaoImpl implements EmployeeDao {
private SessionFactory sessionFactory;
public List<Employee> getAllEmployee() {
return null;
}
public void updateEmployee(Employee emp) {
}
public void deleteEmployee(Employee emp) {
}
}
如何创建选择,更新和删除查询。能否请您提出任何可能的解决方案
答案 0 :(得分:1)
您必须更新以下代码
public void deleteEmployee(Employee emp) {
Session session = sessionFactory.getCurrentSession();
session.delete(emp);
logger.debug(emp.getClass());
}
public void updateEmployee(Employee emp) {
Session session = sessionFactory.getCurrentSession();
session.update(emp);
logger.debug(emp.getClass());
}
public List<Employee> getAllEmployee(){
String query ="SELECT e FROM EMPLOYEE e";
List<Employee> empList = session.createQuery(query);
return empList;
}
希望这些东西有效。