Hibernate CRUD

DAOUtils.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Created by qianxingzhe on 16/5/2.
 */
public class DAOUtils {
    private static Session session;
    private static SessionFactory sf;

    static {
        Configuration configuration = new Configuration();
        configuration.configure();
        sf = configuration.buildSessionFactory();
    }

    public static Session getSession() {
        session = sf.openSession();
        return session;
    }

    public static void closeSession() {
        if (session != null) {
            session.close();
        }
    }
}

IEmployeeDao.java

import com.yly.a_hello.Employee;

import java.io.Serializable;
import java.util.List;

/**
 * Created by qianxingzhe on 16/5/2.
 */
public interface IEmployeeDao {
    void save(Employee employee);

    void update(Employee employee);

    Employee findById(Serializable id);

    List<Employee> getAll();

    List<Employee> getAll(String employeeName);

    List<Employee> getAll(int index, int count);

    void delete(Serializable id);
}

EmployeeDaoImpl.java

import com.yly.a_hello.Employee;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.io.Serializable;
import java.util.List;

/**
 * Created by qianxingzhe on 16/5/2.
 */
public class EmployeeDaoImpl implements IEmployeeDao {
    @Override
    public void save(Employee employee) {
        Session session = DAOUtils.getSession();
        Transaction transaction = session.beginTransaction();
        session.save(employee);
        transaction.commit();
        DAOUtils.closeSession();
    }

    @Override
    public void update(Employee employee) {
        Session session = DAOUtils.getSession();
        Transaction transaction = session.beginTransaction();
        session.update(employee);
        transaction.commit();
        DAOUtils.closeSession();
    }

    @Override
    public Employee findById(Serializable id) {
        Session session = DAOUtils.getSession();
        Transaction transaction = session.beginTransaction();
        Employee employee = (Employee) session.get(Employee.class, id);
        transaction.commit();
        DAOUtils.closeSession();
        return employee;
    }

    @Override
    public List<Employee> getAll() {
        Session session = DAOUtils.getSession();
        Transaction transaction = session.beginTransaction();
        Query query = session.createQuery("from Employee ");
        List<Employee> employees = query.list();
        transaction.commit();
        DAOUtils.closeSession();
        return employees;
    }

    @Override
    public List<Employee> getAll(String employeeName) {
        Session session = DAOUtils.getSession();
        Transaction transaction = session.beginTransaction();
        Query query = session.createQuery("from Employee where empName=?");
        // 注意,参数索引从0开始
        query.setParameter(0, employeeName);
        List<Employee> employees = query.list();
        transaction.commit();
        DAOUtils.closeSession();
        return employees;
    }

    @Override
    public List<Employee> getAll(int index, int count) {
        Session session = DAOUtils.getSession();
        Transaction transaction = session.beginTransaction();
        Query query = session.createQuery("from Employee");
        query.setFirstResult(index);//查询的起始行,从0开始
        query.setMaxResults(count);//查询返回的行数
        List<Employee> employees = query.list();
        transaction.commit();
        DAOUtils.closeSession();
        return employees;
    }

    @Override
    public void delete(Serializable id) {
        Session session = DAOUtils.getSession();
        Transaction transaction = session.beginTransaction();
        //先根据Id查询对象,再判断删除
        Object obj = session.get(Employee.class, id);
        if (null != obj) {
            session.delete(obj);
        }
        transaction.commit();
        DAOUtils.closeSession();
    }
}

Employee.java

import java.util.Date;

/**
 * 一.对象
 * Created by qianxingzhe on 16/2/13.
 */
public class Employee {
    private int empId;
    private String empName;
    private Date workDate;

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Date getWorkDate() {
        return workDate;
    }

    public void setWorkDate(Date workDate) {
        this.workDate = workDate;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empId=" + empId +
                ", empName='" + empName + '\'' +
                ", workDate=" + workDate +
                '}';
    }
}
posted @ 2016-05-02 09:08  yly123  阅读(163)  评论(0编辑  收藏  举报