标签:hibernate
IEmployeeDao.java
package com.rk.hibernate.b_crud;
import java.io.Serializable;
import java.util.List;
import com.rk.hibernate.a_hello.Employee;
public interface IEmployeeDao
{
Employee findById(Serializable id);
void save(Employee emp);
void update(Employee emp);
void delete(Serializable id);
List<Employee> findAll();
List<Employee> findByName(String empName);
List<Employee> page(int index,int count);
}EmployeeDaoImpl.java
package com.rk.hibernate.b_crud;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.rk.hibernate.a_hello.Employee;
import com.rk.hibernate.utils.HibernateUtils;
public class EmployeeDaoImpl implements IEmployeeDao
{
@Override
public Employee findById(Serializable id)
{
Session session = null;
Transaction tx = null;
try
{
// 获取Session
session = HibernateUtils.getSession();
// 开启事务
tx = session.beginTransaction();
// 主键查询
Employee obj = (Employee) session.get(Employee.class, id);
return obj;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
finally
{
tx.commit();
session.close();
}
}
@Override
public void save(Employee emp)
{
Session session = null;
Transaction tx = null;
try
{
session = HibernateUtils.getSession();
tx = session.beginTransaction();
// 执行保存操作
session.save(emp);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
finally
{
tx.commit();
session.close();
}
}
@Override
public void update(Employee emp)
{
Session session = null;
Transaction tx = null;
try
{
session = HibernateUtils.getSession();
tx = session.beginTransaction();
session.update(emp);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
finally
{
tx.commit();
session.close();
}
}
@Override
public void delete(Serializable id)
{
Session session = null;
Transaction tx = null;
try
{
session = HibernateUtils.getSession();
tx = session.beginTransaction();
// 先根据id查询对象,再判断删除
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();
}
}
@Override
public List<Employee> findAll()
{
Session session = null;
Transaction tx = null;
try
{
session = HibernateUtils.getSession();
tx = session.beginTransaction();
// HQL查询
Query q = session.createQuery("from Employee");
List<Employee> list = q.list();
return list;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
finally
{
tx.commit();
session.close();
}
}
@Override
public List<Employee> findByName(String empName)
{
Session session = null;
Transaction tx = null;
try
{
session = HibernateUtils.getSession();
tx = session.beginTransaction();
Query q = session.createQuery("from Employee where empName=?");
// 注意:参数索引从0开始
q.setParameter(0, empName);
// 执行查询
List<Employee> list = q.list();
return list;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
finally
{
tx.commit();
session.close();
}
}
@Override
public List<Employee> page(int index, int count)
{
Session session = null;
Transaction tx = null;
try
{
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();
}
}
}HibernateUtils.java
package com.rk.hibernate.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils
{
private static SessionFactory sessionFactory;
static
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
public static Session getSession()
{
return sessionFactory.openSession();
}
}标签:hibernate
原文地址:http://lsieun.blog.51cto.com/9210464/1812353