尚硅谷之JDBC

117 篇文章 0 订阅
116 篇文章 0 订阅

1.5 继承BasicDAO的后的DAO实现类

DepartmentDAO实现类

package com.atguigu.dao.impl.basic;

 

import java.util.ArrayList;

import java.util.List;

 

import com.atguigu.bean.Department;

import com.atguigu.dao.DepartmentDAO;

import com.atguigu.dao.impl.BasicDAOImpl;

 

public class DepartmentDAOImplBasic extends BasicDAOImpl<Department> implements DepartmentDAO{

 

    @Override

    public void addDepartment(Department department) throws Exception {

        String sql = "INSERT INTO t_department(did,dname,description) VALUES(NULL,?,?)";

        update(sql, department.getName(),department.getDescription());

    }

 

    @Override

    public void updateDepartment(Department department) throws Exception {

        String sql = "UPDATE t_department SET dname = ?,description = ? WHERE did = ?";

        update(sql, department.getName(),department.getDescription(),department.getId());

    }

 

    @Override

    public void deleteById(String did) throws Exception {

        String sql = "DELETE FROM t_department WHERE did = ?";

        update(sql, did);

    }

 

    @Override

    public Department getById(String did) throws Exception {

        String sql = "SELECT did as id,dname as name,description FROM t_department WHERE did = ?";

        Department department = get(sql, did);

        return department;

    }

 

    @Override

    public List<Department> getAll() throws Exception {

        String sql = "SELECT did as id,dname as name,description FROM t_department";

        ArrayList<Department> list = getList(sql);

        return list;

    }

 

}

 

EmployeeDAO实现类

package com.atguigu.dao.impl.basic;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

 

import com.atguigu.bean.Employee;

import com.atguigu.dao.EmployeeDAO;

import com.atguigu.dao.impl.BasicDAOImpl;

 

public class EmployeeDAOImpl extends BasicDAOImpl<Employee> implements EmployeeDAO{

 

    @Override

    public void addEmployee(Employee emp) throws Exception {

        String sql = "INSERT INTO t_employee "

                 + "(eid,ename,tel,gender,salary,commission_pct,birthday,hiredate,job_id,email,mid,address,native_place,did)"

                 + "VALUES(NULL,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        Object[] args = new Object[13];

        args[0] = emp.getEname();

        args[1] = emp.getTel();

        args[2] = emp.getGender();

        args[3] = emp.getSalary();

        args[4] = emp.getCommissionPct();

        args[5] = emp.getBirthday();

        args[6] = emp.getHiredate();

        args[7] = emp.getJobId();

        args[8] = emp.getEmail();

        args[9] = emp.getMid();

        args[10] = emp.getAddress();

        args[11] = emp.getNativePlace();

        args[12] = emp.getDid();

       

        update(sql, args);

    }

 

    @Override

    public void updateEmployee(Employee emp) throws Exception {

        String sql = "UPDATE t_employee SET "

                 + "ename=?,tel=?,gender=?,salary=?,commission_pct=?,birthday=?,hiredate=?,job_id=?,email=?,MID=?,address=?,native_place=?,did=?"

                 +" WHERE eid = ?";

        Object[] args = new Object[14];

        args[0] = emp.getEname();

        args[1] = emp.getTel();

        args[2] = emp.getGender();

        args[3] = emp.getSalary();

        args[4] = emp.getCommissionPct();

        args[5] = emp.getBirthday();

        args[6] = emp.getHiredate();

        args[7] = emp.getJobId();

        args[8] = emp.getEmail();

        args[9] = emp.getMid();

        args[10] = emp.getAddress();

        args[11] = emp.getNativePlace();

        args[12] = emp.getDid();

        args[13] = emp.getEid();

        update(sql, args);

    }

 

    @Override

    public void deleteById(String eid) throws Exception {

        String sql = "DELETE FROM t_employee WHERE eid = ?";

        update(sql, eid);

    }

 

    @Override

    public Employee getById(String eid) throws Exception {

        String sql = "SELECT eid,ename,tel,gender,salary,commission_pct as 'commissionPct',birthday,hiredate,"

                + "job_id as 'jobId',email,mid,address,native_place as 'nativePlace' ,did FROM t_employee WHERE eid = ?";

        Employee employee = get(sql, eid);

        return employee;

    }

 

    @Override

    public List<Employee> getAll() throws Exception {

        String sql = "SELECT eid,ename,tel,gender,salary,commission_pct as 'commissionPct',birthday,hiredate,"

                + "job_id as 'jobId',email,mid,address,native_place as 'nativePlace' ,did FROM t_employee";

       

        ArrayList<Employee> list = getList(sql);

        return list;

    }

 

    @Override

    public Long getCount() throws Exception {

        String sql = "SELECT COUNT(*) FROM t_employee";

        Long value = (Long) getValue(sql);

        return value;

    }

 

    @Override

    public List<Employee> getAll(int page, int pageSize) throws Exception {

        String sql = "SELECT eid,ename,tel,gender,salary,commission_pct as 'commissionPct',birthday,hiredate,"

                + "job_id as 'jobId',email,mid,address,native_place as 'nativePlace' ,did FROM t_employee LIMIT ?,?";

       

        ArrayList<Employee> list = getList( sql, (page-1)*pageSize, pageSize);

        return list;

    }

 

    @Override

    public Double getMaxSalary() throws Exception {

        String sql = "SELECT MAX(salary) FROM t_employee";

        Double value = (Double) getValue(sql);

        return value;

    }

 

    @Override

    public Map<Integer, Double> getAvgSalaryByDid() throws Exception {

        String sql = "SELECT did,MAX(salary) FROM t_employee GROUP BY did";

        List<Map<String, Object>> list = getListMap(sql);

       

        HashMap<Integer, Double> result = new HashMap<>();

        for (Map<String, Object> map : list) {

            Set<Entry<String, Object>> entrySet = map.entrySet();

            Integer did = null;

            Double salary = null;

            for (Entry<String, Object> entry : entrySet) {

                String key = entry.getKey();

                if("did".equals(key)){

                    did = (Integer) entry.getValue();

                }else{

                    salary = (Double) entry.getValue();

                }

            }

           

            result.put(did, salary);

        }

        return result;

    }

 

}

 

 

 

 

本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值