empdaoimpl.java

来自「java 框架核心技术编程」· Java 代码 · 共 71 行

JAVA
71
字号
package org.lxh.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.lxh.dao.EmpDAO;
import org.lxh.factory.DefaultSessionFactory;
import org.lxh.vo.Emp;

public class EmpDAOImpl implements EmpDAO {
	private Session session = null;

	public EmpDAOImpl() {
		this.session = DefaultSessionFactory.getSession();
	}

	public void delete(String empno) throws Exception {
		String hql = "DELETE FROM Emp WHERE empno=?";
		Query q = this.session.createQuery(hql);
		q.setString(0, empno);
		q.executeUpdate();
		this.session.beginTransaction().commit();
		this.session.close();
	}

	public void insert(Emp emp) throws Exception {
		this.session.save(emp);
		this.session.beginTransaction().commit();
		this.session.close();
	}

	public List queryAll() throws Exception {
		List all = null;
		String hql = "FROM Emp AS e";
		all = this.session.createQuery(hql).list();
		this.session.close();
		return all;
	}

	public Emp queryByEmpno(String empno) throws Exception {
		Emp e = null;
		String hql = "FROM Emp AS e WHERE e.empno=?";
		Query q = this.session.createQuery(hql);
		q.setString(0, empno);
		List all = q.list();
		if (all.size() > 0) {
			e = (Emp) all.get(0);
		}
		this.session.close();
		return e;
	}

	public List queryByLike(String cond) throws Exception {
		List all = null;
		String hql = "FROM Emp AS e WHERE e.ename LIKE ?";
		Query q = this.session.createQuery(hql);
		q.setString(0, "%" + cond + "%");
		all = q.list() ;
		this.session.close() ;
		return all;
	}

	public void update(Emp emp) throws Exception {
		this.session.update(emp) ;
		this.session.beginTransaction().commit() ;
		this.session.close() ;
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?