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; }
}
|
本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源。