人事管理系统项目(六)

人事管理系统项目(六)

下面建一个dao包,这个包下有一个它的impl实现包,这里是真正操作数据库的方法,例如增,删,改,查等;下面来看代码:

AccountDao:

package com.jiazhong.hr.dao;

        import com.jiazhong.hr.model.Account;
        import com.jiazhong.hr.model.view.AccountView;

public interface AccountDao {
    public Account queryAccountByAccountNamePassword(AccountView accountView);
}

DeptDao:

package com.jiazhong.hr.dao;

import com.jiazhong.hr.model.Dept;

import java.util.List;

/**
 * @Author: 加中实训
 * @Date:2020/8/23 17:25
 * @Description:
 */
public interface DeptDao {
    public List<Dept> queryDept();
}

EmpDao:

package com.jiazhong.hr.dao;

import com.jiazhong.hr.commons.PageParams;
import com.jiazhong.hr.model.Emp;
import com.jiazhong.hr.model.Leave;
import com.jiazhong.hr.model.view.EmpView;

import java.util.List;

/**
 * @Author: 加中实训
 * @Date:2020/8/22 14:53
 * @Description: 员工数据库操作接口
 */
public interface EmpDao {
    /**
     * 查询所有员工信息
     * @return
     */
    public List<EmpView> queryEmp();

    /**
     * 分页查询员工数据
     * @param pageParams
     * @return
     */
    public List<EmpView> queryEmpForPagin(PageParams pageParams);

    /**
     * 获得员工总数量(含离职人员)
     * @return
     */
    public int getEmpSize();

    void addEmp(Emp emp);

    void updateStatus(int emp_id, int status);

    /**
     * 员工离职
     */
    void leave(List<Leave> leave);

    void updateEmp(Emp emp);
}

PostDao:

package com.jiazhong.hr.dao;

import com.jiazhong.hr.model.Post;

import java.util.List;

/**
 * @Author: 加中实训
 * @Date:2020/8/23 17:48
 * @Description:
 */
public interface PostDao {
    public List<Post> queryPostByDeptId(int dept_id);
}

AccountDaoImpl:

package com.jiazhong.hr.dao.impl;

import com.jiazhong.hr.dao.AccountDao;
import com.jiazhong.hr.model.Account;
import com.jiazhong.hr.model.view.AccountView;
import com.jiazhong.hr.utils.DBUtil;

import java.util.List;

public class AccountDaoImpl extends DBUtil implements AccountDao {
    @Override
    public Account queryAccountByAccountNamePassword(AccountView accountView) {
        String sql="select account_id,account_name,account_status,emp_id from tbl_account where account_name=? and account_password=? and account_status!=-1";
        List<Account> accountList=super.executeQuery(sql,Account.class,accountView.getAccount_name(),accountView.getAccount_password());
        if (accountList!=null && accountList.size()!=0){
            return accountList.get(0);
        }
        return null;
    }
    }

DeptDaoImpl:

package com.jiazhong.hr.dao.impl;

import com.jiazhong.hr.dao.DeptDao;
import com.jiazhong.hr.model.Dept;
import com.jiazhong.hr.utils.DBUtil;

import java.util.List;

/**
 * @Author: 加中实训
 * @Date:2020/8/23 17:26
 * @Description:
 */
public class DeptDaoImpl extends DBUtil implements DeptDao {
    @Override
    public List<Dept> queryDept() {
        String sql = "select * from tbl_dept";
        return super.executeQuery(sql,Dept.class);
    }
}

EmpDaoImpl:

package com.jiazhong.hr.dao.impl;

import com.jiazhong.hr.commons.PageParams;
import com.jiazhong.hr.dao.EmpDao;
import com.jiazhong.hr.model.Emp;
import com.jiazhong.hr.model.Leave;
import com.jiazhong.hr.model.view.EmpView;
import com.jiazhong.hr.utils.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 加中实训
 * @Date:2020/8/22 14:56
 * @Description: EmpDao实现类
 */
public class EmpDaoImpl extends DBUtil implements EmpDao {
    @Override
    public List<EmpView> queryEmp() {
        String sql = "SELECT " +
                "           emp.emp_id," +
                "           emp.emp_name," +
                "           emp.emp_sex," +
                "           emp.emp_birthday," +
                "           emp.emp_phone," +
                "           emp.emp_entry_time," +
                "           emp.emp_status," +
                "           dept.dept_name," +
                "           post.post_name " +
                "       FROM tbl_emp emp " +
                "                   INNER JOIN tbl_post post ON emp.post_id=post.post_id " +
                "                   INNER JOIN tbl_dept dept ON post.dept_id=dept.dept_id";
        try {
            super.getConn();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            List<EmpView> empViewList = new ArrayList<>();
            while (rs.next()) {
                EmpView empView = new EmpView();
                empView.setEmp_id(rs.getInt(1));
                empView.setEmp_name(rs.getString(2));
                empView.setEmp_sex(rs.getInt(3));
                empView.setEmp_birthday(rs.getDate(4));
                empView.setEmp_phone(rs.getString(5));
                empView.setEmp_entry_time(rs.getDate(6));
                empView.setEmp_status(rs.getInt(7));
                empView.setDept_name(rs.getString(8));
                empView.setPost_name(rs.getString(9));
                //将封装好的empView对象添加到list集合中
                empViewList.add(empView);
            }
            return empViewList;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll();
        }
        return null;
    }

    @Override
    public List<EmpView> queryEmpForPagin(PageParams pageParams) {
        String sql = "SELECT " +
                "           emp.emp_id," +
                "           emp.emp_name," +
                "           emp.emp_sex," +
                "           emp.emp_birthday," +
                "           emp.emp_phone," +
                "           emp.emp_entry_time," +
                "           emp.emp_status," +
                "           dept.dept_name," +
                "           post.post_name " +
                "       FROM tbl_emp emp " +
                "                   INNER JOIN tbl_post post ON emp.post_id=post.post_id " +
                "                   INNER JOIN tbl_dept dept ON post.dept_id=dept.dept_id" +
                "       LIMIT ?,?";
        try {
            //获得查询的起始行号,根据页码获取
            int start = (pageParams.getPageNumber() - 1) * pageParams.getPageSize();
            super.getConn();
            ps = conn.prepareStatement(sql);

            ps.setInt(1, start);
            ps.setInt(2, pageParams.getPageSize());

            rs = ps.executeQuery();
            List<EmpView> empViewList = new ArrayList<>();
            while (rs.next()) {
                EmpView empView = new EmpView();
                empView.setEmp_id(rs.getInt(1));
                empView.setEmp_name(rs.getString(2));
                empView.setEmp_sex(rs.getInt(3));
                empView.setEmp_birthday(rs.getDate(4));
                empView.setEmp_phone(rs.getString(5));
                empView.setEmp_entry_time(rs.getDate(6));
                empView.setEmp_status(rs.getInt(7));
                empView.setDept_name(rs.getString(8));
                empView.setPost_name(rs.getString(9));
                //将封装好的empView对象添加到list集合中
                empViewList.add(empView);
            }
            return empViewList;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll();
        }
        return null;
    }

    @Override
    public int getEmpSize() {
        String sql = "select count(emp_id) as emp_id from tbl_emp";
        List<Emp> empList = super.executeQuery(sql, Emp.class);
        if (empList.size() != 0) {
            return empList.get(0).getEmp_id();
        }
        return 0;
    }

    @Override
    public void addEmp(Emp emp) {
        String sql = "insert into tbl_emp values(default,?,?,?,?,?,default,?)";
        super.executeUpdate(sql, emp.getEmp_name(), emp.getEmp_sex(), emp.getEmp_birthday(),
                emp.getEmp_phone(), emp.getEmp_entry_time(), emp.getPost_id());
    }

    @Override
    public void updateStatus(int emp_id, int status) {
        String sql = "update  tbl_emp set emp_status=? where emp_id=?";
        super.executeUpdate(sql, status, emp_id);
    }

    /**
     * 离职操作
     *
     * @param leaves 使用事务实现:
     *               1.保证所有的操作在一个连接对象上进行
     *               2.JDBC默认使用的是自动提交事务,我们要去掉JDBC的自动提交功能
     *               conn.setAutoCommit(false);
     */
    @Override
    public void leave(List<Leave> leaves) {
        int result = 0;
        try {
            this.getConn();
            //去掉事务的自动提交功能
            super.conn.setAutoCommit(false);

            for (Leave leave : leaves) {
                /*******向离职表中添加离职信息*******/
                //创建增加离职信息的SQL语句
                String leaveSQL = "insert into tbl_leave values(default,?,?,now())";
                //创建预处理对象
                ps = conn.prepareStatement(leaveSQL);
                //设置?号占位符的值
                ps.setInt(1, leave.getEmp_id());
                ps.setString(2, leave.getLeave_reasons());
                //执行SQL语句
                result = result + ps.executeUpdate();
                ps.close();
                System.out.println("离职信息添加成功-----------------------");


                /*********更新员工状态为离职状态**********/

                String updateEmpStatusSQL = "update  tbl_emp set emp_status=1 where emp_id=?";
                //创建预处理对象
                ps = conn.prepareStatement(updateEmpStatusSQL);
                ps.setInt(1, leave.getEmp_id());
                result = result + ps.executeUpdate();
                ps.close();
            }

            //判断两次执行是否成功
            if (result == leaves.size() * 2) {
                System.out.println("离职人员信息处理成功----------");
                //事务提交
                conn.commit();
            } else {
                conn.rollback();//事务回滚
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll();
        }
    }

    @Override
    public void updateEmp(Emp emp) {
        String sql = "update tbl_emp set emp_name=?,emp_sex=?,emp_birthday=?, emp_phone=?,emp_entry_time=?,post_id=? where emp_id=? ";
        super.executeUpdate(sql, emp.getEmp_name(), emp.getEmp_sex(),
                                    emp.getEmp_birthday(), emp.getEmp_phone(),
                                    emp.getEmp_entry_time(), emp.getPost_id(),
                                    emp.getEmp_id());
    }
}

PostDaoImpl:

package com.jiazhong.hr.dao.impl;

import com.jiazhong.hr.dao.PostDao;
import com.jiazhong.hr.model.Post;
import com.jiazhong.hr.utils.DBUtil;

import java.util.List;

/**
 * @Author: 加中实训
 * @Date:2020/8/23 17:48
 * @Description:
 */
public class PostDaoImpl extends DBUtil implements PostDao {
    @Override
    public List<Post> queryPostByDeptId(int dept_id) {
        String sql = "select * from tbl_post where dept_id=?";
        return super.executeQuery(sql,Post.class,dept_id);
    }
}

这里建一个model包,下面建一个view视图包,model包下封装了所有我要用到的实体对象,而view包下是用来在页面上需要显示的信息,代码如下:
Account:

package com.jiazhong.hr.model;

public class Account {
    private int account_id;
    private String account_name;
    private String account_password;
    private int account_status;
    private int emp_id;

    public int getAccount_id() {
        return account_id;
    }

    public void setAccount_id(int account_id) {
        this.account_id = account_id;
    }

    public String getAccount_name() {
        return account_name;
    }

    public void setAccount_name(String account_name) {
        this.account_name = account_name;
    }

    public String getAccount_password() {
        return account_password;
    }

    public void setAccount_password(String account_password) {
        this.account_password = account_password;
    }

    public int getAccount_status() {
        return account_status;
    }

    public void setAccount_status(int account_status) {
        this.account_status = account_status;
    }

    public int getEmp_id() {
        return emp_id;
    }

    public void setEmp_id(int emp_id) {
        this.emp_id = emp_id;
    }
}

Dept:

package com.jiazhong.hr.model;

/**
 * @Author: 加中实训
 * @Date:2020/8/23 17:24
 * @Description:
 */
public class Dept {
    private int dept_id;
    private String dept_name;

    public int getDept_id() {
        return dept_id;
    }

    public void setDept_id(int dept_id) {
        this.dept_id = dept_id;
    }

    public String getDept_name() {
        return dept_name;
    }

    public void setDept_name(String dept_name) {
        this.dept_name = dept_name;
    }
}

Emp:

package com.jiazhong.hr.model;

import java.util.Date;

public class Emp {
    private int emp_id;
    private String emp_name;
    private int emp_sex;
    private Date emp_birthday;
    private String emp_phone;
    private Date emp_entry_time;
    private int emp_status;
    private int post_id;

    public int getEmp_id() {
        return emp_id;
    }

    public void setEmp_id(int emp_id) {
        this.emp_id = emp_id;
    }

    public String getEmp_name() {
        return emp_name;
    }

    public void setEmp_name(String emp_name) {
        this.emp_name = emp_name;
    }

    public int getEmp_sex() {
        return emp_sex;
    }

    public void setEmp_sex(int emp_sex) {
        this.emp_sex = emp_sex;
    }

    public Date getEmp_birthday() {
        return emp_birthday;
    }

    public void setEmp_birthday(Date emp_birthday) {
        this.emp_birthday = emp_birthday;
    }

    public String getEmp_phone() {
        return emp_phone;
    }

    public void setEmp_phone(String emp_phone) {
        this.emp_phone = emp_phone;
    }

    public Date getEmp_entry_time() {
        return emp_entry_time;
    }

    public void setEmp_entry_time(Date emp_entry_time) {
        this.emp_entry_time = emp_entry_time;
    }

    public int getEmp_status() {
        return emp_status;
    }

    public void setEmp_status(int emp_status) {
        this.emp_status = emp_status;
    }

    public int getPost_id() {
        return post_id;
    }

    public void setPost_id(int post_id) {
        this.post_id = post_id;
    }
}

Leave:

package com.jiazhong.hr.model;

import java.util.Date;

/**
 * @Author: 加中实训
 * @Date:2020/9/14 16:36
 * @Description: 离职表对应的实体对象
 */
public class Leave {
    private Integer leave_id;
    private Integer emp_id;
    private String leave_reasons;
    private Date leave_date;

    public Integer getLeave_id() {
        return leave_id;
    }

    public void setLeave_id(Integer leave_id) {
        this.leave_id = leave_id;
    }

    public Integer getEmp_id() {
        return emp_id;
    }

    public void setEmp_id(Integer emp_id) {
        this.emp_id = emp_id;
    }

    public String getLeave_reasons() {
        return leave_reasons;
    }

    public void setLeave_reasons(String leave_reasons) {
        this.leave_reasons = leave_reasons;
    }

    public Date getLeave_date() {
        return leave_date;
    }

    public void setLeave_date(Date leave_date) {
        this.leave_date = leave_date;
    }
}

Post:

package com.jiazhong.hr.model;

/**
 * @Author: 加中实训
 * @Date:2020/8/23 17:46
 * @Description:
 */
public class Post {
    private int post_id;
    private String post_name;
    private int dept_id;
    private String post_desc;

    public int getPost_id() {
        return post_id;
    }

    public void setPost_id(int post_id) {
        this.post_id = post_id;
    }

    public String getPost_name() {
        return post_name;
    }

    public void setPost_name(String post_name) {
        this.post_name = post_name;
    }

    public int getDept_id() {
        return dept_id;
    }

    public void setDept_id(int dept_id) {
        this.dept_id = dept_id;
    }

    public String getPost_desc() {
        return post_desc;
    }

    public void setPost_desc(String post_desc) {
        this.post_desc = post_desc;
    }
}

AccountView:

package com.jiazhong.hr.model.view;

public class AccountView {
    private String account_name;
    private String account_password;

    public AccountView() {
    }

    public AccountView(String account_name, String account_password) {
        this.account_name = account_name;
        this.account_password = account_password;
    }

    public String getAccount_name() {
        return account_name;
    }

    public void setAccount_name(String account_name) {
        this.account_name = account_name;
    }

    public String getAccount_password() {
        return account_password;
    }

    public void setAccount_password(String account_password) {
        this.account_password = account_password;
    }
}

EmpView:

package com.jiazhong.hr.model.view;

import java.util.Date;

//其中包含页面上要显示的员工信息
public class EmpView {
    private int emp_id;
    private String emp_name;
    private int emp_sex;
    private Date emp_birthday;
    private String emp_phone;
    private Date emp_entry_time;
    private int emp_status;
    private String dept_name;//部门名称
    private String post_name;//职位名称

    public int getEmp_id() {
        return emp_id;
    }

    public void setEmp_id(int emp_id) {
        this.emp_id = emp_id;
    }

    public String getEmp_name() {
        return emp_name;
    }

    public void setEmp_name(String emp_name) {
        this.emp_name = emp_name;
    }

    public int getEmp_sex() {
        return emp_sex;
    }

    public void setEmp_sex(int emp_sex) {
        this.emp_sex = emp_sex;
    }

    public Date getEmp_birthday() {
        return emp_birthday;
    }

    public void setEmp_birthday(Date emp_birthday) {
        this.emp_birthday = emp_birthday;
    }

    public String getEmp_phone() {
        return emp_phone;
    }

    public void setEmp_phone(String emp_phone) {
        this.emp_phone = emp_phone;
    }

    public Date getEmp_entry_time() {
        return emp_entry_time;
    }

    public void setEmp_entry_time(Date emp_entry_time) {
        this.emp_entry_time = emp_entry_time;
    }

    public int getEmp_status() {
        return emp_status;
    }

    public void setEmp_status(int emp_status) {
        this.emp_status = emp_status;
    }

    public String getDept_name() {
        return dept_name;
    }

    public void setDept_name(String dept_name) {
        this.dept_name = dept_name;
    }

    public String getPost_name() {
        return post_name;
    }

    public void setPost_name(String post_name) {
        this.post_name = post_name;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值