⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 employeedao.java

📁 只是一个宽带的收费管理系统 很值得大家学习
💻 JAVA
字号:
package cn.handson.dao;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


import cn.handson.base.*;
import cn.handson.utils.*;

public class EmployeeDao {
	
	/*
	 * 此方法用来验证登陆用户的合法性。。。
	 * 
	 */
	
	
	public String login(Employee user) {
		// TODO Auto-generated method stub
		String hefa;
		Session session=HibernateUtil.currentSession();
		Transaction tx=session.beginTransaction();//定义一个事务 
		String sql="select b from Employee as b where b.employeeUsername='"+user.getEmployeeUsername()+"' and b.employeePassword='"+user.getEmployeePassword()+"'";
		Query query=session.createQuery(sql);
		List list=query.list();
		tx.commit();//提交事务
		HibernateUtil.closeSession();
		int i=list.size();
		if(i==0){
			hefa="";
		}else{
			hefa="ok";
			System.out.println(hefa);
		}
		return hefa;
	}
	
	
	/*
	 * 此方法用来验证登陆用户的合法性。。。
	 * 
	 */
	
	
	public List getEmployeeIteam() {
		// TODO Auto-generated method stub
		Session session=HibernateUtil.currentSession();
		Transaction tx=session.beginTransaction();//定义一个事务 
		String sql="select b from Employee as b where b.depart.departId=3";
		Query query=session.createQuery(sql);
		List list=query.list();
		tx.commit();//提交事务
		HibernateUtil.closeSession();
		return list;
	}
	
	
	/*
	 * 此方法用来得到所有的员工表对象。。。
	 * 
	 * 
	 */
	
	public List loadAllEmployee() {
		// TODO Auto-generated method stub
		Session session=HibernateUtil.currentSession();
		Transaction tx=session.beginTransaction();//定义一个事务 
		String sql="select b from Employee as b";
		Query query=session.createQuery(sql);
		List list=query.list();
		tx.commit();//提交事务
		//HibernateUtil.closeSession();
		return list;
		
	}
	/*
	 * 此方法用来删除员工表对象.....
	 * 
	 */
	public boolean deleteEmployee(Employee ey){
		Session session=HibernateUtil.currentSession();
		Transaction tx=session.beginTransaction();//定义一个事务 
		session.delete(ey);
		tx.commit();//提交事务
		//HibernateUtil.closeSession();
		return true;
	}
	
	/*
	 * 此方法用来通过ID来得到一个员工表的对象。。。
	 * 
	 * 
	 */
	
	public Employee getEmployeeById(Integer id) {
		// TODO Auto-generated method stub
		Session session=HibernateUtil.currentSession();
        Transaction tx=session.beginTransaction();//定义一个事务 
        Employee us=(Employee)session.get(Employee.class, id);
		tx.commit();//提交事务
		//HibernateUtil.closeSession();
		return us;
	}
    
	
	/*
	 * 此方法用来通过ID来得到一个员工表的对象。。。
	 * 
	 * 
	 */
	
	public Employee getEmployeeByName(String name) {
		// TODO Auto-generated method stub
		Session session=HibernateUtil.currentSession();
        Transaction tx=session.beginTransaction();//定义一个事务 
        String sql="select b from Employee as b where b.employeeUsername='"+name+"'";
        Query query=session.createQuery(sql);
		List list=query.list();
		tx.commit();//提交事务
		Iterator it=list.iterator();
		Employee ww=new Employee();
		while(it.hasNext()){
			ww=(Employee)it.next();
		}
		HibernateUtil.closeSession();
		return ww; 
	}
	
	
	/*
	 * 此方法用来更新数据库中的员工。。。
	 * 
	 */
	
	public void updateEmployee(Employee ey) {
		// TODO Auto-generated method stub
		Session session=HibernateUtil.currentSession();
        Transaction tx=session.beginTransaction();//定义一个事务 
        Employee us=(Employee)session.get(Employee.class, ey.getEmployeeId());
		us.setEmployeeName(ey.getEmployeeName());
		us.setEmployeeUsername(ey.getEmployeeUsername());
		us.setEmployeePassword(ey.getEmployeePassword());
		us.setDepart(ey.getDepart());
		us.setEmployeeRemark(ey.getEmployeeRemark());
		session.save(us);
		tx.commit();//提交事务
		//HibernateUtil.closeSession();
	}
	
	/*
	 * 此方法用来添加员工。。。。
	 *
	 * 
	 */
	
	public void addEmployee(Employee ey) {
		// TODO Auto-generated method stub
		Session session=HibernateUtil.currentSession();
        Transaction tx=session.beginTransaction();//定义一个事务 
		session.save(ey);
		tx.commit();//提交事务
		//HibernateUtil.closeSession();
		
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		List list=new EmployeeDao().getEmployeeIteam();
		Iterator iter=list.iterator();
		while(iter.hasNext()){
			Employee aa=(Employee)iter.next();
			System.out.println(aa.getEmployeeName());
		}
		/*
		Employee wu=new EmployeeDao().getEmployeeByName("wuhaibao");
		System.out.println(wu.getEmployeeUsername());
		*/
	}
	
	
	public List loadAll() {
		// TODO Auto-generated method stub
		Session session=HibernateUtil.currentSession();
		Transaction tx=session.beginTransaction();//定义一个事务 
		String sql="select b from Employee as b where b.depart.departId=3";
		Query query=session.createQuery(sql);
		List list=query.list();
		tx.commit();//提交事务
		//HibernateUtil.closeSession();
		return list;
		
	}

}

⌨️ 快捷键说明

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