1.创建session的封装
package cn.itcast.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory sf;
static{
//加载主配置文件,并创建Session的工厂
sf=new Configuration().configure().buildSessionFactory();
}
//创建Session对象
public static Session getSession(){
return sf.openSession();
}
}
2.增删查改的封装
package cn.itcast.b_crud;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import cn.itcast.a_hello.Employee;
import cn.itcast.utils.HibernateUtils;
public class EmployeeDaoImpl implements IEmployeeDao{
@Override
//保存数据
public void save(Employee emp) {
Session session=null;
Transaction tx=null;
try{
//创建Session对象
session=(Session) HibernateUtils.getSession();
//开启事物
tx=session.beginTransaction();
//保存数据
session.save(emp);
}catch(Exception e){
throw new RuntimeException(e);
}finally{
//提交事物
tx.commit();
//关闭session
session.close();
}
}
@Override
//更新数据
public void update(Employee emp) {
Session session=null;
Transaction tx=null;
try{
session=(Session) HibernateUtils.getSession();
tx=session.beginTransaction();
session.update(emp);
}catch(Exception e){
throw new RuntimeException(e);
}finally{
tx.commit();
session.close();
}
}
//根据员工id查询
@Override
public Employee findById(Serializable id) {
Session session=null;
Transaction tx=null;
try{
session=(Session) HibernateUtils.getSession();
tx=session.beginTransaction();
return (Employee) session.get(Employee.class,id);
}catch(Exception e){
throw new RuntimeException(e);
}finally{
tx.commit();
session.close();
}
}
@Override
//查询全部
public List<Employee> getAll() {
Session session=null;
Transaction tx=null;
try{
session=(Session) HibernateUtils.getSession();
tx=session.beginTransaction();
//HQL查询
Query q=session.createQuery("from Employee");
return q.list();
}catch(Exception e){
throw new RuntimeException(e);
}finally{
tx.commit();
session.close();
}
}
@Override
//根据姓名查询
public List<Employee> getAll(String employeeName) {
Session session=null;
Transaction tx=null;
try{
session=(Session) HibernateUtils.getSession();
tx=session.beginTransaction();
Query q=session.createQuery("from Employee where empName=?");
//注意,参数从0开始
q.setParameter(0, employeeName);
//执行查询
return q.list();
}catch(Exception e){
throw new RuntimeException(e);
}finally{
tx.commit();
session.close();
}
}
@Override
//分页查询
public List<Employee> getAll(int index, int count) {
Session session=null;
Transaction tx=null;
try{
session=(Session) HibernateUtils.getSession();
tx=session.beginTransaction();
Query q=session.createQuery("from Employee");
//设置分页参数
q.setFirstResult(index);//查询的起始行
q.setMaxResults(count);//查询返回的行数
List<Employee> list=q.list();
return list;
}catch(Exception e){
throw new RuntimeException(e);
}finally{
tx.commit();
session.close();
}
}
@Override
//根据id删除用户
public void delete(Serializable id) {
Session session=null;
Transaction tx=null;
try{
session=(Session) HibernateUtils.getSession();
tx=session.beginTransaction();
Object obj=session.get(Employee.class, id);
if(obj!=null){
session.delete(obj);
}
}catch(Exception e){
throw new RuntimeException(e);
}finally{
tx.commit();
session.close();
}
}
}