deptdaoimpl.java

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

JAVA
74
字号
package org.lxh.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.lxh.dao.DeptDAO;
import org.lxh.factory.DefaultSessionFactory;
import org.lxh.vo.Dept;

public class DeptDAOImpl implements DeptDAO {
	private Session session = null;

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

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

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

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

	public Dept queryByDeptno(String deptno) throws Exception {
		Dept d = null;
		String hql = "FROM Dept AS d WHERE d.deptno=?";
		Query q = this.session.createQuery(hql);
		q.setString(0, deptno);
		List all = q.list();
		if (all.size() > 0) {
			d = (Dept) all.get(0);
		}
		d.getEmps().hashCode();
		this.session.close();
		return d;
	}

	public List queryByLike(String cond) throws Exception {
		List all = null;
		String hql = "FROM Dept AS d WHERE d.dname LIKE ? OR d.loc LIKE ? OR d.business LIKE ?";
		Query q = this.session.createQuery(hql);
		q.setString(0, "%" + cond + "%");
		q.setString(1, "%" + cond + "%");
		q.setString(2, "%" + cond + "%");
		all = q.list();
		this.session.close();
		return all;
	}

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

}

⌨️ 快捷键说明

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