标签:service highlight 修改密码 tor connect 连表 print tostring 实现
主要业务层和持久层的联系
员工实体Bean
package com.project.bean;
import java.sql.Date;
/**
* 员工信息实体类
* @author 45470
*
*/
public class EmployeeBean {
/**员工id*/
private int empId;
/**员工登录名*/
private String empAccount;
/**员工登录密码*/
private String empPwd="123456";
/**员工姓名*/
private String empName;
/**员工性别*/
private String empSex;
/**员工文化程度*/
private String empEdu;
/**员工出生日期*/
private Date empBirthday;
/**员工联系电话*/
private String empTle;
/**员工所属专业*/
private String empProfession;
/**员工政治面貌*/
private String empPolitic;
/**员工家庭住址*/
private String empAddress;
/**工作职位id*/
private int jobId;
/**职位名称*/
private String jobName;
/**员工部门id*/
private int deptId;
/**部门名称*/
private String deptName;
/**员工入职日期*/
private Date joinDate=new Date(System.currentTimeMillis());
/**员工头像文件名称或文件路径*/
private String imgPath;
/**职位对象,封装用户登录权限*/
private JobBean job;
public EmployeeBean() {
super();
// TODO Auto-generated constructor stub
}
public EmployeeBean(String empAccount, String empPwd, String empName, String empSex, String empEdu,
Date empBirthday, String empTle, String empProfession, String empPolitic, String empAddress, int jobId,
int deptId, Date joinDate, String imgPath) {
super();
this.empAccount = empAccount;
this.empPwd = empPwd;
this.empName = empName;
this.empSex = empSex;
this.empEdu = empEdu;
this.empBirthday = empBirthday;
this.empTle = empTle;
this.empProfession = empProfession;
this.empPolitic = empPolitic;
this.empAddress = empAddress;
this.jobId = jobId;
this.deptId = deptId;
this.joinDate=joinDate;
this.imgPath = imgPath;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpAccount() {
return empAccount;
}
public void setEmpAccount(String empAccount) {
this.empAccount = empAccount;
}
public String getEmpPwd() {
return empPwd;
}
public void setEmpPwd(String empPwd) {
this.empPwd = empPwd;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpSex() {
return empSex;
}
public void setEmpSex(String empSex) {
this.empSex = empSex;
}
public String getEmpEdu() {
return empEdu;
}
public void setEmpEdu(String empEdu) {
this.empEdu = empEdu;
}
public Date getEmpBirthday() {
return empBirthday;
}
public void setEmpBirthday(Date empBirthday) {
this.empBirthday = empBirthday;
}
public String getEmpTle() {
return empTle;
}
public void setEmpTle(String empTle) {
this.empTle = empTle;
}
public String getEmpProfession() {
return empProfession;
}
public void setEmpProfession(String empProfession) {
this.empProfession = empProfession;
}
public String getEmpPolitic() {
return empPolitic;
}
public void setEmpPolitic(String empPolitic) {
this.empPolitic = empPolitic;
}
public String getEmpAddress() {
return empAddress;
}
public void setEmpAddress(String empAddress) {
this.empAddress = empAddress;
}
public int getJobId() {
return jobId;
}
public void setJobId(int jobId) {
this.jobId = jobId;
}
public String getJobName() {
return jobName;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
public JobBean getJob() {
return job;
}
public void setJob(JobBean job) {
this.job = job;
}
@Override
public String toString() {
return "EmployeeBean [empId=" + empId + ", empAccount=" + empAccount + ", empPwd=" + empPwd + ", empName="
+ empName + ", empSex=" + empSex + ", empEdu=" + empEdu + ", empBirthday=" + empBirthday + ", empTle="
+ empTle + ", empProfession=" + empProfession + ", empPolitic=" + empPolitic + ", empAddress="
+ empAddress + ", jobId=" + jobId + ", jobName=" + jobName + ", deptId=" + deptId + ", deptName="
+ deptName + ", joinDate=" + joinDate + ", imgPath=" + imgPath + ", job=" + job + "]";
}
}
持久层接口
package com.project.dao;
import java.util.List;
import com.project.bean.DeptBean;
import com.project.bean.EmployeeBean;
import com.project.bean.JobBean;
/**
* 员工类持久化接口
* @author Administrator
*
*/
public interface IEmployeeDao {
/**
* 登录方法,查询拥有用户名和密码的对象,若果没有返回空
* @param userName 用户登录名
* @param pwd 密码
* @return 拥有用户名和密码的员工对象,如果没有返回空
*/
public EmployeeBean login(String userName,String pwd);
/**
* 添加员工
* @param bean 员工对象
*/
public void add(EmployeeBean bean);
/**
* 按id删除员工
* @param emId
*/
public void del(int emId);
/**
* 按照员工id查询员工信息,包含职位名称,部门名称
* @param emId
* @return
*/
public EmployeeBean findById(int emId);
/**
* 按照员工的id,修改员工的所在部门id,联系方式,政治面貌,工作职位
* @param emId 员工的id
* @param emTel 员工新的联系方式
* @param deptId 员工新的部门id
* @param politic 员工新的政治面貌
* @param jobId 员工新的职位
*/
public void update(int emId,String emTel,
int deptId,String politic,int jobId);
/**
* 动态查询员工信息,姓名为模糊查询
* @param enName
* @param emEdu
* @param emDept
* @param jobName
* @return
*/
public List<EmployeeBean> findByItem(String enName,String emEdu,String emDept,String jobName );
/**
* 通过部门id,连表查询部门所有员工,部门表左外连接
* @param deptId
* @return
*/
public List<EmployeeBean> findByDeptId(int deptId);
/**
*连接学校表,部门表,员工表,按照学校id查询负责部门的所有员工
* @param schoolId 学校id
* @return 员工对象集合
*/
public List<EmployeeBean> findBySchool(int schoolId);
/**
* 按照id修改密码
* @param id 员工id
* @param newPwd xinmi
*/
public void updatePwd(int id,String newPwd);
}
持久层实现类
package com.project.impl;
import java.sql.Connection;
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 Administrator
*
*/
public class EmployeeDaoImpl extends BaseDao implements IEmployeeDao {
public static void main(String[] args) {
IEmployeeDao dao=new EmployeeDaoImpl();
//System.out.println(dao.login("aaa", "123"));
//dao.add(new EmployeeBean("tuhao","2222", "java", "男", "本科", Date.valueOf("2001-02-01"), "12323244", "篮球", "团员", "北京", 1, 1, new Date(System.currentTimeMillis()), "c:/djsdasj"));
//dao.del(1);
//System.out.println(dao.findById(2));
//dao.update(2,"22222",3,"党员", 3);
//System.out.println(dao.findByItem("","大专", null,null));
//dao.updatePwd(3, "22");
//System.out.println( dao.findByDeptId(4));
// System.out.println(dao.findBySchool(3));
}
@Override
public EmployeeBean login(String userName, String pwd) {
EmployeeBean bean=null;
this.setConnection();
try {
ps=con.prepareStatement("select*from t_employee where em_account=? and em_pwd=?");
ps.setString(1, userName);
ps.setString(2, pwd);
rs=ps.executeQuery();
if(rs.next()){
bean=new EmployeeBean();
bean.setEmpId(rs.getInt("em_id"));
bean.setEmpAccount(rs.getString("em_account"));
bean.setEmpPwd(rs.getString("em_pwd"));
bean.setEmpName(rs.getString("em_name"));
bean.setEmpSex(rs.getString("em_sex"));
bean.setEmpEdu(rs.getString("em_edu"));
bean.setEmpBirthday(rs.getDate("em_birthday"));
bean.setEmpTle(rs.getString("em_tel"));
bean.setEmpProfession(rs.getString("em_profession"));
bean.setEmpPolitic(rs.getString("em_politic"));
bean.setEmpAddress(rs.getString("em_address"));
bean.setJobId(rs.getInt("em_jobId"));
bean.setDeptId(rs.getInt("em_deptId"));
bean.setImgPath(rs.getString("em_imgPath"));
bean.setJoinDate(rs.getDate("em_joinDate"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
this.closeConnection();
}
return bean;
}
@Override
public void add(EmployeeBean bean) {
this.setConnection();
try {
ps=con.prepareStatement("insert into t_employee(em_account,em_pwd,em_name,em_sex,em_edu,em_birthday,"
+ "em_tel,em_profession,em_politic,em_address,em_jobId,em_deptId,em_joindate,em_imgPath) "
+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
ps.setString(1,bean.getEmpAccount());
ps.setString(2,bean.getEmpPwd());
ps.setString(3,bean.getEmpName());
ps.setString(4,bean.getEmpSex());
ps.setString(5,bean.getEmpEdu());
ps.setDate(6,bean.getEmpBirthday());
ps.setString(7,bean.getEmpTle());
ps.setString(8,bean.getEmpProfession());
ps.setString(9, bean.getEmpPolitic());
ps.setString(10, bean.getEmpAddress());
ps.setInt(11,bean.getJobId());
ps.setInt(12, bean.getDeptId());
ps.setDate(13,bean.getJoinDate());
ps.setString(14,bean.getImgPath());
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
this.closeConnection();
}
}
@Override
public void del(int emId) {
this.setConnection();
try {
ps=con.prepareStatement("delete from t_employee where em_id=?");
ps.setInt(1, emId);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
this.closeConnection();
}
}
@Override
public EmployeeBean findById(int emId) {
this.setConnection();
EmployeeBean bean=new EmployeeBean();
try {
ps=con.prepareStatement("SELECT e.*,j.job_name,d.dept_name FROM t_employee e,t_job j,t_dept d WHERE e.em_jobId=j.job_id AND e.em_deptId=d.dept_id AND em_id =?");
ps.setInt(1, emId);
rs=ps.executeQuery();
if(rs.next()){
bean.setEmpId(rs.getInt("em_id"));
bean.setEmpAccount(rs.getString("em_account"));
bean.setEmpPwd(rs.getString("em_pwd"));
bean.setEmpName(rs.getString("em_name"));
bean.setEmpSex(rs.getString("em_sex"));
bean.setEmpEdu(rs.getString("em_edu"));
bean.setEmpBirthday(rs.getDate("em_birthday"));
bean.setEmpTle(rs.getString("em_tel"));
bean.setEmpProfession(rs.getString("em_profession"));
bean.setEmpPolitic(rs.getString("em_politic"));
bean.setEmpAddress(rs.getString("em_address"));
bean.setJobId(rs.getInt("em_jobId"));
bean.setDeptId(rs.getInt("em_deptId"));
bean.setImgPath(rs.getString("em_imgPath"));
bean.setJoinDate(rs.getDate("em_joinDate"));
bean.setDeptName(rs.getString("dept_name"));
bean.setJobName(rs.getString("job_name"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
this.closeConnection();
}
return bean;
}
@Override
public void update(
int emId, String emTel, int deptId, String politic, int jobId) {
this.setConnection();
try {
ps=con.prepareStatement("update t_employee set em_tel=?,em_deptId=?,em_politic=?,em_jobId=? where em_id=? ");
ps.setString(1, emTel);
ps.setInt(2, deptId);
ps.setString(3,politic);
ps.setInt(4, jobId);
ps.setInt(5, emId);
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
this.closeConnection();
}
}
@Override
public List<EmployeeBean> findByItem(String enName, String emEdu, String emDept, String jobName) {
this.setConnection();
List<EmployeeBean> list =new ArrayList<EmployeeBean>();
String sql="SELECT e.*,d.dept_name,j.job_name FROM t_employee e,t_dept d,t_job j WHERE e.em_deptId=d.dept_id AND e.em_jobId=j.job_id ";
if(enName!=null&&enName.length()!=0){
sql+=" and e.em_name like ‘%"+enName+"%‘ ";
}
if(emEdu!=null&&emEdu.length()!=0){
sql+=" and e.em_edu=‘"+emEdu+"‘ " ;
}
if(emDept!=null&&emDept.length()!=0){
sql+=" and d.dept_name like ‘%"+emDept+"&‘ ";
}
if(jobName!=null&&jobName.length()!=0){
sql+=" and j.job_namelike ‘%"+jobName+"&‘ ";
}
try {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
EmployeeBean bean=new EmployeeBean();
bean.setEmpId(rs.getInt("em_id"));
bean.setEmpAccount(rs.getString("em_account"));
bean.setEmpPwd(rs.getString("em_pwd"));
bean.setEmpName(rs.getString("em_name"));
bean.setEmpSex(rs.getString("em_sex"));
bean.setEmpEdu(rs.getString("em_edu"));
bean.setEmpBirthday(rs.getDate("em_birthday"));
bean.setEmpTle(rs.getString("em_tel"));
bean.setEmpProfession(rs.getString("em_profession"));
bean.setEmpPolitic(rs.getString("em_politic"));
bean.setEmpAddress(rs.getString("em_address"));
bean.setJobId(rs.getInt("em_jobId"));
bean.setDeptId(rs.getInt("em_deptId"));
bean.setImgPath(rs.getString("em_imgPath"));
bean.setJoinDate(rs.getDate("em_joinDate"));
list.add(bean);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
this.closeConnection();
}
return list;
}
@Override
public List<EmployeeBean> findByDeptId(int deptId) {
List<EmployeeBean> list=new ArrayList<EmployeeBean>();
this.setConnection();
try {// 我SELECT e.* FROM t_employee e,t_dept d WHERE e.em_deptId=d.dept_id AND d.dept_id=?
ps=con.prepareStatement("select *from t_employee where em_deptId=?");
ps.setInt(1, deptId);
rs=ps.executeQuery();
while(rs.next()){
EmployeeBean bean=new EmployeeBean();
bean.setEmpId(rs.getInt("em_id"));
bean.setEmpAccount(rs.getString("em_account"));
bean.setEmpPwd(rs.getString("em_pwd"));
bean.setEmpName(rs.getString("em_name"));
bean.setEmpSex(rs.getString("em_sex"));
bean.setEmpEdu(rs.getString("em_edu"));
bean.setEmpBirthday(rs.getDate("em_birthday"));
bean.setEmpTle(rs.getString("em_tel"));
bean.setEmpProfession(rs.getString("em_profession"));
bean.setEmpPolitic(rs.getString("em_politic"));
bean.setEmpAddress(rs.getString("em_address"));
bean.setJobId(rs.getInt("em_jobId"));
bean.setDeptId(rs.getInt("em_deptId"));
bean.setImgPath(rs.getString("em_imgPath"));
bean.setJoinDate(rs.getDate("em_joinDate"));
list.add(bean);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
this.closeConnection();
}
return list;
}
@Override
public List<EmployeeBean> findBySchool(int schoolId) {
List<EmployeeBean> list=new ArrayList<EmployeeBean>();
this.setConnection();
try {//我SELECT e.* FROM t_employee e,t_school s WHERE e.em_id=s.sch_functionary AND sch_id=?
ps=con.prepareStatement("SELECT e.* FROM t_employee e,t_dept d,t_school s WHERE e.em_deptId=d.dept_id AND s.sch_dept=d.dept_id AND s.sch_id=?");
ps.setInt(1, schoolId);
rs=ps.executeQuery();
while(rs.next()){
EmployeeBean bean=new EmployeeBean();
bean.setEmpId(rs.getInt("em_id"));
bean.setEmpAccount(rs.getString("em_account"));
bean.setEmpPwd(rs.getString("em_pwd"));
bean.setEmpName(rs.getString("em_name"));
bean.setEmpSex(rs.getString("em_sex"));
bean.setEmpEdu(rs.getString("em_edu"));
bean.setEmpBirthday(rs.getDate("em_birthday"));
bean.setEmpTle(rs.getString("em_tel"));
bean.setEmpProfession(rs.getString("em_profession"));
bean.setEmpPolitic(rs.getString("em_politic"));
bean.setEmpAddress(rs.getString("em_address"));
bean.setJobId(rs.getInt("em_jobId"));
bean.setDeptId(rs.getInt("em_deptId"));
bean.setImgPath(rs.getString("em_imgPath"));
bean.setJoinDate(rs.getDate("em_joinDate"));
list.add(bean);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeConnection();
}
return list;
}
@Override
public void updatePwd(int id, String newPwd) {
this.setConnection();
try {
ps=con.prepareStatement("update t_employee set em_pwd=? where em_id=?");
ps.setString(1, newPwd);
ps.setInt(2, id);
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
this.closeConnection();
}
}
}
业务层接口
package com.project.service;
import java.util.List;
import com.project.bean.EmployeeBean;
/**
* 员工实体类业务接口
* @author Administrator
*
*/
public interface IEmployeeService {
/**
* 登录业务。查询拥有用户名和密码的对象,如果没找到返回空
* 如果登录成功,封装职位对象
* @param userName 用户名
* @param pwd 密码
* @return 拥有用户密码的员工对象,如果没有返回null
*/
public EmployeeBean login(String userName,String pwd);
/**
* 添加新员工
* @param bean 员工对象
*/
public void add(EmployeeBean bean);
/**
* 按员工id删除员工,删除员工之前,先将该员工负责的学校,负责人外键设置为null
* @param emId 员工id
*/
public void del(int emId);
/**
* 通过id查询员工,同时查询部门的名称和职位的名称
* @param emId 员工id
* @return 员工对象
*/
public EmployeeBean findById(int emId);
/**
* 按照选择员工的id修改员工的所在部门,联系方式,政治面貌,工作职位
* @param emId 所选择员工的id
* @param emTel 员工新的联系方式
* @param deptId 员工新的部门id
* @param politic 员工新的政治面貌
* @param jobId 员工新的职位id
*/
public void update(int emId,String emTel,int deptId,String politic,int jobId);
/**
* 动态条件查询员工,同时查询员工的部门名称和职位名称
* @param emName 按员工姓名模糊查找
* @param emEdu 按文化程度查找
* @param emDept 按所在部门查询
* @param jobName 按工作职位查询
* @return 员工对象集合
*/
public List<EmployeeBean> findByItem(String emName,String emEdu,String emDept,String jobName);
/**
* 通过部门id查询部门所有员工
* @param deptId 部门id
* @return 员工对象集合
*/
public List<EmployeeBean> findByDeptId(int deptId);
/**
* 查询指定id的学校,负责部门所有员工
* @param schoolId 学校ID
* @return 员工集合
*/
public List<EmployeeBean> findBySchool(int schoolId);
/**
* 按id修改密码
* @param id 员工id
* @param newPwd 员工新密码
*/
public void updatePwd(int id,String newPwd);
}
业务层实现类
package com.project.service.impl;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import com.project.bean.EmployeeBean;
import com.project.bean.LogBean;
import com.project.dao.IEmployeeDao;
import com.project.dao.IJobDao;
import com.project.dao.ILogDao;
import com.project.dao.ISchoolDao;
import com.project.impl.EmployeeDaoImpl;
import com.project.impl.JobDaoImpl;
import com.project.impl.LogDaoImpl;
import com.project.impl.SchoolDaoImpl;
import com.project.service.IEmployeeService;
import sun.java2d.pipe.SpanShapeRenderer.Simple;
/**
* 员工业务接口实现类
* @author Administrator
*
*/
public class EmployeeServiceImpl implements IEmployeeService {
private IEmployeeDao emDao=new EmployeeDaoImpl();
private IJobDao jobDao=new JobDaoImpl();
private ISchoolDao schDao=new SchoolDaoImpl();
private ILogDao logDao=new LogDaoImpl();
public static void main(String[] args) {
IEmployeeService service=new EmployeeServiceImpl();
//System.out.println(service.login("jjj","123"));
}
@Override
public EmployeeBean login(String userName, String pwd) {
EmployeeBean bean=emDao.login(userName, pwd);
if(bean!=null){
bean.setJob(jobDao.findById(bean.getJobId()));
SimpleDateFormat sd=new SimpleDateFormat("HH:hh");
String str="用户"+userName+"在"+sd.format(new Date(System.currentTimeMillis()))+"登录系统";
LogBean logBean=new LogBean();
logBean.setLogContent(str);
logDao.add(logBean);
}
return bean;
}
@Override
public void add(EmployeeBean bean) {
emDao.add(bean);
}
@Override
public void del(int emId) {
schDao.updateSchoolByEmIdNULL(emId);
emDao.del(emId);
}
@Override
public EmployeeBean findById(int emId) {
return emDao.findById(emId);
}
@Override
public void update(int emId, String emTel, int deptId, String politic, int jobId) {
emDao.update(emId, emTel, deptId, politic, jobId);
}
@Override
public List<EmployeeBean> findByItem(String emName, String emEdu, String emDept, String jobName) {
return emDao.findByItem(emName, emEdu, emDept, jobName);
}
@Override
public List<EmployeeBean> findByDeptId(int deptId) {
return emDao.findByDeptId(deptId);
}
@Override
public List<EmployeeBean> findBySchool(int schoolId) {
return emDao.findBySchool(schoolId);
}
@Override
public void updatePwd(int id, String newPwd) {
emDao.updatePwd(id, newPwd);
}
}
标签:service highlight 修改密码 tor connect 连表 print tostring 实现
原文地址:http://www.cnblogs.com/291959101lf/p/6883500.html