首页 > 代码库 > 第七周(2)

第七周(2)

本周的难点在于持久接口和实体的的转换,需要较强的思维逻辑。

比如这个持久接口

package com.project.dao;

 

import java.util.List;

import com.project.bean.EmployeeBean;

/**
* 员工持久接口
*
* @author zxiaoyuer
*
*/
public interface IEmployeeDao {

/**
* 登录业务。查询拥有用户名和密码的对象,如果没有返回空;
*
* @param emAccord
* 用户名
* @param pwd
* 密码
* @return 拥有用户名和密码的员工对象,如果没有找到返回null
*/
public EmployeeBean login(String emAccord, String pwd);

/**
* 员工添加
*
* @param bean
* 员工对象
*/

public void add(EmployeeBean bean);

/**
* 按id删除员工;
*
*
* @param id
* 员工id
*/
public void del(int id);

/**
* 根据id查找员工信息,链表查询职位名称,部门名称,
*
* @param id
* 员工id
* @return 员工对象
*/
public EmployeeBean findById(int id);

/**
* 根据员工id修改员工
* @param id
* 员工id
* @param phone
* 员工联系方式
* @param political
* 员工政治面貌
* @param posId 员工表外键
* 员工职位id
* @param deptId
* 员工部门id
*/
public void update(int id, String phone, String political, int posId, int deptId);

/**
* 动态条件查询员工,同时查询员工的部门名称和员工名称
* @param emName 按员工姓名查找
* @param grade 按员工学历查找
* @param posName 按工作职位查找
* @param deptName 按所在部门查找
* @return 员工集合
*/
public List<EmployeeBean> findByItem(String emName, String grade, String posName, String deptName);

/**
* 根据员工id修改密码
*
* @param emId
* 员工id
* @param newPwd
* 新密码
*/
public void updatePwd(int emId,String newPwd);
/**
* 按部门id查询该部门所有员工
* @param deptId 部门id
* @return 员工集合
*/
public List<EmployeeBean> findByDeptId(int deptId);
/**
* 查询指定id的学校,负责部门所有员工
* @param schoolID 学校id
* @return 员工集合
*/
public List<EmployeeBean> findBySchoolId(int schoolID);
}

 

 

而他的实现类应该这样书写

package com.project.dao.impl;

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

import com.project.bean.EmployeeBean;
import com.project.dao.IEmployeeDao;

/**
* 员工持久接口实现类
*
* @author zxiaoyuer
*
*/
public class EmployeeDaoImpl extends BaseDao implements IEmployeeDao {

public static void main(String[] args) {
EmployeeDaoImpl dao = new EmployeeDaoImpl();
// EmployeeBean bean=dao.login("zhanghao", "123456");
// System.out.println(bean);
// dao.add(new EmployeeBean("duanyu", "段誉", "男", Date.valueOf("1990-06-11"),
// "大理路", "17711050087", "本科", "民主党", "厨师", 3,"助理", 2, "财务部", "段誉头像",null));
// dao.del(11);
// List<EmployeeBean> list = dao.findByItem("王", "大", "理", "部");
// System.out.println(list);
// dao.update(1, "17712344321", "群众", 1, 1);
// EmployeeBean bean = dao.findById(1);
// System.out.println(bean);
// dao.updatePwd(1, "669996");
// List<EmployeeBean> list = dao.findByDeptId(2);
//// System.out.println(list);
List<EmployeeBean> list=dao.findBySchoolId(3);
System.out.println(list);
}

@Override
public EmployeeBean login(String emAccord, String pwd) {
EmployeeBean bean = null;
this.setConnection();

try {
ps = con.prepareStatement(
"SELECT e.*,p.pos_name,d.dept_name FROM t_employee e,t_position p,t_department d WHERE e.pos_id=p.id AND e.dept_id=d.id AND e.em_accord=? AND e.em_pwd=?");
ps.setString(1, emAccord);
ps.setString(2, pwd);
rs = ps.executeQuery();

if (rs.next())
{
bean = new EmployeeBean();
bean.setEmployeeId(rs.getInt("id"));
bean.setEmAccord(rs.getString("em_accord"));
bean.setEmName(rs.getString("em_name"));
bean.setEmPwd(rs.getString("em_pwd"));
bean.setEmSex(rs.getString("em_sex"));
bean.setEmBrithday(rs.getDate("em_birthday"));
bean.setEmAddress(rs.getString("em_address"));
bean.setEmPhone(rs.getString("em_phone"));
bean.setEmGrade(rs.getString("em_grade"));
bean.setEmPolitical(rs.getString("em_political"));
bean.setEmSpecialty(rs.getString("em_specialty"));
bean.setPosId(rs.getInt("pos_id"));
bean.setPosName(rs.getString("pos_name"));
bean.setDeptId(rs.getInt("dept_id"));
bean.setDeptName(rs.getString("dept_name"));
bean.setEmDate(rs.getDate("em_date"));
bean.setAccImage(rs.getString("em_imageName"));
return bean;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeConnection();
}
return null;
}

@Override
public void add(EmployeeBean bean) {
this.setConnection();

try {
ps = con.prepareStatement(
"INSERT INTO t_employee(em_accord,em_name,em_pwd,em_sex,em_birthday,em_address,em_phone,em_grade,em_political,em_specialty,em_date,em_imageName,pos_id,dept_id)"
+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,? )");
ps.setString(1, bean.getEmAccord());
ps.setString(2, bean.getEmName());
ps.setString(3, bean.getEmPwd());
ps.setString(4, bean.getEmSex());
ps.setDate(5, bean.getEmBrithday());
ps.setString(6, bean.getEmAddress());
ps.setString(7, bean.getEmPhone());
ps.setString(8, bean.getEmGrade());
ps.setString(9, bean.getEmPolitical());
ps.setString(10, bean.getEmSpecialty());
ps.setDate(11, bean.getEmDate());
ps.setString(12, bean.getAccImage());
ps.setInt(13, bean.getPosId());
ps.setInt(14, bean.getDeptId());
ps.executeUpdate();
} catch (SQLException e) {

e.printStackTrace();
} finally {
this.closeConnection();
}

}

@Override
public void del(int id) {
this.setConnection();
try {
ps = con.prepareStatement("DELETE FROM t_employee WHERE id=?");
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeConnection();
}

}

@Override
public EmployeeBean findById(int id) {
EmployeeBean bean = null;
this.setConnection();

try {
ps = con.prepareStatement(
"SELECT e.*,p.pos_name,d.dept_name "
+ "FROM t_employee e,t_position p,t_department d "
+ "WHERE e.pos_id=p.id AND e.dept_id=d.id AND e.id=?");
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
bean = new EmployeeBean();
bean.setEmployeeId(rs.getInt("id"));
bean.setEmAccord(rs.getString("em_accord"));
bean.setEmName(rs.getString("em_name"));
bean.setEmPwd(rs.getString("em_pwd"));
bean.setEmSex(rs.getString("em_sex"));
bean.setEmBrithday(rs.getDate("em_birthday"));
bean.setEmAddress(rs.getString("em_address"));
bean.setEmPhone(rs.getString("em_phone"));
bean.setEmGrade(rs.getString("em_grade"));
bean.setEmPolitical(rs.getString("em_political"));
bean.setEmSpecialty(rs.getString("em_specialty"));
bean.setPosId(rs.getInt("pos_id"));
bean.setPosName(rs.getString("pos_name"));
bean.setDeptId(rs.getInt("dept_id"));
bean.setDeptName(rs.getString("dept_name"));
bean.setEmDate(rs.getDate("em_date"));
bean.setAccImage(rs.getString("em_imageName"));
return bean;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeConnection();
}
return null;
}

@Override
public void update(int id, String phone, String political, int posId, int deptId) {

this.setConnection();
try {
ps = con.prepareStatement("UPDATE t_employee "
+ "SET em_phone=?,em_political=?,pos_id=?,dept_id=? "
+ "WHERE id=? ");
ps.setString(1, phone);
ps.setString(2, political);
ps.setInt(3, posId);
ps.setInt(4, deptId);
ps.setInt(5, id);

ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeConnection();
}
}

@Override
public List<EmployeeBean> findByItem(String emName, String grade, String posName, String deptName) {
List<EmployeeBean> list = new ArrayList<EmployeeBean>();
String sql = "SELECT e.*,p.pos_name,d.dept_name "
+ "FROM t_employee e,t_position p,t_department d "
+ "WHERE e.pos_id=p.id AND e.dept_id=d.id";
if (emName != null && emName.length() != 0) {
sql += " AND em_name LIKE ‘%" + emName + "%‘ ";
}
if (grade != null && grade.length() != 0) {
sql += " AND em_grade LIKE ‘%" + grade + "%‘ ";
}
if (posName != null && posName.length() != 0) {
sql += " AND p.pos_name LIKE ‘%" + posName + "%‘ ";
}
if (deptName != null && deptName.length() != 0) {
sql += " AND d.dept_name LIKE ‘%" + deptName + "%‘ ";
}
this.setConnection();
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
EmployeeBean bean = new EmployeeBean();
bean.setEmployeeId(rs.getInt("id"));
bean.setEmAccord(rs.getString("em_accord"));
bean.setEmName(rs.getString("em_name"));
bean.setEmPwd(rs.getString("em_pwd"));
bean.setEmSex(rs.getString("em_sex"));
bean.setEmBrithday(rs.getDate("em_birthday"));
bean.setEmAddress(rs.getString("em_address"));
bean.setEmPhone(rs.getString("em_phone"));
bean.setEmGrade(rs.getString("em_grade"));
bean.setEmPolitical(rs.getString("em_political"));
bean.setEmSpecialty(rs.getString("em_specialty"));
bean.setPosId(rs.getInt("pos_id"));
bean.setPosName(rs.getString("pos_name"));
bean.setDeptId(rs.getInt("dept_id"));
bean.setDeptName(rs.getString("dept_name"));
bean.setEmDate(rs.getDate("em_date"));
bean.setAccImage(rs.getString("em_imageName"));
list.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeConnection();
}
return list;
}

@Override
public void updatePwd(int emId, String newPwd) {
this.setConnection();

try {
ps = con.prepareStatement("UPDATE t_employee SET em_pwd=? WHERE id=?");
ps.setString(1, newPwd);
ps.setInt(2, emId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeConnection();
}

}

@Override
public List<EmployeeBean> findByDeptId(int deptId) {
List<EmployeeBean> list = new ArrayList<EmployeeBean>();
this.setConnection();

try {
ps = con.prepareStatement(
"SELECT e.*,p.pos_name,d.dept_name "
+ "FROM t_employee e,t_position p,t_department d "
+ "WHERE e.pos_id=p.id AND e.dept_id=d.id AND d.id=? GROUP BY e.id;");
ps.setInt(1, deptId);
rs = ps.executeQuery();
while (rs.next()) {
EmployeeBean bean = new EmployeeBean();
bean.setEmployeeId(rs.getInt("id"));
bean.setEmAccord(rs.getString("em_accord"));
bean.setEmName(rs.getString("em_name"));
bean.setEmPwd(rs.getString("em_pwd"));
bean.setEmSex(rs.getString("em_sex"));
bean.setEmBrithday(rs.getDate("em_birthday"));
bean.setEmAddress(rs.getString("em_address"));
bean.setEmPhone(rs.getString("em_phone"));
bean.setEmGrade(rs.getString("em_grade"));
bean.setEmPolitical(rs.getString("em_political"));
bean.setEmSpecialty(rs.getString("em_specialty"));
bean.setPosId(rs.getInt("pos_id"));
bean.setPosName(rs.getString("pos_name"));
bean.setDeptId(rs.getInt("dept_id"));
bean.setDeptName(rs.getString("dept_name"));
bean.setEmDate(rs.getDate("em_date"));
bean.setAccImage(rs.getString("em_imageName"));
list.add(bean);
}
} catch (SQLException e) {

e.printStackTrace();
} finally {
this.closeConnection();
}

return list;
}

@Override
public List<EmployeeBean> findBySchoolId(int schoolID) {
List<EmployeeBean> list = new ArrayList<EmployeeBean>();
this.setConnection();
try {
ps = con.prepareStatement(
"SELECT*FROM t_addschool s,t_employee e,t_department d WHERE s.employee_id=e.id AND s.dept_id=d.id AND s.school_id=?");
ps.setInt(1, schoolID);
rs = ps.executeQuery();
while (rs.next()) {
EmployeeBean bean = new EmployeeBean();
bean.setEmployeeId(rs.getInt("id"));
bean.setEmAccord(rs.getString("em_accord"));
bean.setEmName(rs.getString("em_name"));
bean.setEmPwd(rs.getString("em_pwd"));
bean.setEmSex(rs.getString("em_sex"));
bean.setEmBrithday(rs.getDate("em_birthday"));
bean.setEmAddress(rs.getString("em_address"));
bean.setEmPhone(rs.getString("em_phone"));
bean.setEmGrade(rs.getString("em_grade"));
bean.setEmPolitical(rs.getString("em_political"));
bean.setEmSpecialty(rs.getString("em_specialty"));
bean.setDeptId(rs.getInt("dept_id"));
bean.setDeptName(rs.getString("dept_name"));
bean.setEmDate(rs.getDate("em_date"));
bean.setAccImage(rs.getString("em_imageName"));
list.add(bean);
}
} catch (SQLException e) {

e.printStackTrace();
} finally {
this.closeConnection();
}
return list;
}

}

第七周(2)