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 +
                '}';
    }
}

转载于:https://www.cnblogs.com/FlySheep/p/5451883.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值