📄 employeedaoimpl.java
字号:
package com.sean.pet.hibernate.dao.impl;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.sean.pet.hibernate.HibernateSessionFactory;
import com.sean.pet.hibernate.dao.EmployeeDao;
import com.sean.pet.hibernate.po.Employee;
public class EmployeeDaoImpl implements EmployeeDao {
Session session = null;
public void delete(String id) {
// TODO Auto-generated method stub
Transaction tr = null;
try {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
Employee emp = (Employee) session.load(Employee.class, id);
session.delete(emp);
tr.commit();
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
} finally {
}
HibernateSessionFactory.closeSession();
}
@SuppressWarnings("unchecked")
public List<Employee> findAll() {
// TODO Auto-generated method stub
Transaction tr = null;
List<Employee> list = new ArrayList<Employee>();
try {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
String hql = "from Employee";
Query q = session.createQuery(hql);
list = q.list();
tr.commit();
return list;
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
} finally {
}
HibernateSessionFactory.closeSession();
return null;
}
public Employee findByID(String id) {
// TODO Auto-generated method stub
Employee emp = null;
Transaction tr = null;
try {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
String hql = "from Employee where id=?";
Query q = session.createQuery(hql);
q.setString(0, id);
emp = (Employee)q.uniqueResult();
tr.commit();
return emp;
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
} finally {
}
HibernateSessionFactory.closeSession();
return null;
}
public void save(Employee emp) {
// TODO Auto-generated method stub
Transaction tr = null;
try {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
session.save(emp);
tr.commit();
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
} finally {
}
HibernateSessionFactory.closeSession();
}
public void update(Employee emp) {
// TODO Auto-generated method stub
Transaction tr = null;
try {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
System.out.println("toUpdateDao+++"+emp.getName());
session.merge(emp);
tr.commit();
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
} finally {
HibernateSessionFactory.closeSession();
}
// HibernateSessionFactory.closeSession();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -