xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.EmployeeMapper">
<select id="selbyemptype" parameterType="int" resultType="Employee">
select * from employee where emptype=#{empType}
</select> <resultMap type="Employee" id="selAll">
<id property="empId" column="empid"/>
<result property="realName" column="realname"/>
<result property="sex" column="sex"/>
<result property="hireDate" column="hiredate"/>
<result property="phone" column="phone"/> <collection property="mgr" ofType="Employee">
<id property="empId" column="mgrId"/>
<result property="realName" column="realname"/>
</collection> <collection property="position" ofType="position">
<id property="posid" column="posid"/>
<result property="pname" column="pname"/>
</collection> <collection property="dept" ofType="Department">
<id property="deptno" column="deptno"/>
<result property="deptname" column="deptname"/>
</collection> </resultMap>
<select id="selAll" resultMap="selAll">
select e.empid,e.realname,e.sex,e.hiredate,e.phone,d.deptname,p.pname,e2.realname
from employee e left join dept d on e.deptno=d.deptno
left join position p on e.posid=p.posid
left join employee e2 on e.mgrid=e2.empid
order by e.empid
</select>
</mapper>

实体类:

package com.bjsxt.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List; /**
* 员工类
*
* 如何表示员工所属一个部门、一个岗位、一个上级、甚至多个下级的信息呢?
* 在数据库中通过外键来实现:deptno、posid,mgrid
* 在Java类中通过属性关联来实现
*
* private Department dept; //员工所属部门 不仅包含部门的编号,还包含其他信息
private Position position;
private Employee mgr;//上级领导的信息
private List<Employee> empList = new ArrayList<Employee>();//下级的信息,可能多个
* @author Administrator
*
*/
public class Employee {
private String empId;//员工编号
private String password;//密码
private String realName;//真实姓名
private String sex;//性别
private Date birthDate;//出生日期
private Date hireDate;//入职日期
private Date leaveDate;//离职日期
private int onDuty;//是否在职 0-离职 1-在职
private int empType;//员工类型 1.普通员工 2.管理人员 含经理、总监、总裁等 3.管理员
private String phone;//联系方式
private String qq;
private String emerContactPerson;//紧急联系人信息
private String idCard;//身份证号码
/*
private int deptno;
private int posId;
private String mgrId;
*/
private Department dept; //员工所属部门 不仅包含部门的编号,还包含其他信息
private Position position;
private Employee mgr;//上级领导的信息
private List<Employee> empList = new ArrayList<Employee>();//下级的信息,可能多个 public Employee() {
super();
} public Employee(String empId, String password, String realName, String sex,
Date birthDate, Date hireDate, Date leaveDate, int onDuty,
int empType, String phone, String qq, String emerContactPerson,
String idCard, Department dept, Position position, Employee mgr) {
super();
this.empId = empId;
this.password = password;
this.realName = realName;
this.sex = sex;
this.birthDate = birthDate;
this.hireDate = hireDate;
this.leaveDate = leaveDate;
this.onDuty = onDuty;
this.empType = empType;
this.phone = phone;
this.qq = qq;
this.emerContactPerson = emerContactPerson;
this.idCard = idCard;
this.dept = dept;
this.position = position;
this.mgr = mgr;
} public Employee(String empId, String password, String realName, String sex,
Date birthDate, Date hireDate, Date leaveDate, int onDuty,
int empType, String phone, String qq, String emerContactPerson,
String idCard, Department dept, Position position, Employee mgr,
List<Employee> empList) {
super();
this.empId = empId;
this.password = password;
this.realName = realName;
this.sex = sex;
this.birthDate = birthDate;
this.hireDate = hireDate;
this.leaveDate = leaveDate;
this.onDuty = onDuty;
this.empType = empType;
this.phone = phone;
this.qq = qq;
this.emerContactPerson = emerContactPerson;
this.idCard = idCard;
this.dept = dept;
this.position = position;
this.mgr = mgr;
this.empList = empList;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public Date getLeaveDate() {
return leaveDate;
}
public void setLeaveDate(Date leaveDate) {
this.leaveDate = leaveDate;
}
public int getOnDuty() {
return onDuty;
}
public void setOnDuty(int onDuty) {
this.onDuty = onDuty;
}
public int getEmpType() {
return empType;
}
public void setEmpType(int empType) {
this.empType = empType;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmerContactPerson() {
return emerContactPerson;
}
public void setEmerContactPerson(String emerContactPerson) {
this.emerContactPerson = emerContactPerson;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
public Employee getMgr() {
return mgr;
}
public void setMgr(Employee mgr) {
this.mgr = mgr;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
} @Override
public String toString() {
return "Employee [empId=" + empId + ", password=" + password
+ ", realName=" + realName + ", sex=" + sex + ", birthDate="
+ birthDate + ", hireDate=" + hireDate + ", leaveDate="
+ leaveDate + ", onDuty=" + onDuty + ", empType=" + empType
+ ", phone=" + phone + ", qq=" + qq + ", emerContactPerson="
+ emerContactPerson + ", idCard=" + idCard + ", dept=" + dept
+ ", position=" + position + ", mgr=" + mgr + ", empList="
+ empList + "]";
} }

Mapper接口:

package com.bjsxt.mapper;

import java.util.List;

import com.bjsxt.entity.Employee;

public interface EmployeeMapper {

	List<Employee> selbyemptype(int empType);

	List<Employee> selAll();
}

service方法:

package com.bjsxt.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.bjsxt.dao.EmployeeDao;
import com.bjsxt.dao.impl.EmployeeDaoImpl;
import com.bjsxt.entity.Employee;
import com.bjsxt.mapper.EmployeeMapper;
import com.bjsxt.service.EmployeeService;
import com.bjsxt.util.MyBatisUtil; public class EmployeeServiceImpl implements EmployeeService{ private EmployeeDao empDao = new EmployeeDaoImpl();
SqlSession session = MyBatisUtil.getSession();
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
@Override
public int add(Employee emp) {
return this.empDao.save(emp);
} @Override
public List<Employee> findEmpByType(int i) {
List<Employee> selbyemptype = mapper.selbyemptype(i);
session.close();
return selbyemptype;
} @Override
public List<Employee> selAll() {
List<Employee> selAll = mapper.selAll();
session.close();
return selAll;
} }

service接口:

package com.bjsxt.service;

import java.util.List;

import com.bjsxt.entity.Employee;

public interface EmployeeService {
/**
* 添加员工
* @param emp
* @return
*/
public int add(Employee emp);
/**
* 查询指定类型的员工
* @param i
* @return
*/
public List<Employee> findEmpByType(int i); List<Employee> selAll(); }

Servlet实现类:

package com.bjsxt.servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.bjsxt.entity.Department;
import com.bjsxt.entity.Employee;
import com.bjsxt.entity.Position;
import com.bjsxt.service.DepartmentService;
import com.bjsxt.service.EmployeeService;
import com.bjsxt.service.PositionService;
import com.bjsxt.service.impl.DepartmentServiceImpl;
import com.bjsxt.service.impl.EmployeeServiceImpl;
import com.bjsxt.service.impl.PositionServiceImol; public class EmployeeServlet extends BaseServlet { public void selinfo(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String empid = request.getParameter("empId");
String deptno = request.getParameter("deptno");
System.out.println("=============="+deptno);
String shireDate = request.getParameter("hireDate");
String onduty = request.getParameter("onDuty");
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date hiredate =null;
try {
hiredate = df.parse(shireDate);
} catch (ParseException e) {
e.printStackTrace();
}
//获取所有的部门信息
DepartmentService ds=new DepartmentServiceImpl();
List<Department> deptList = ds.seAll();
request.setAttribute("deptList", deptList);
EmployeeService es = new EmployeeServiceImpl();
List<Employee> selAll = es.selAll();
request.setAttribute("empid", empid);
request.setAttribute("deptno", deptno);
request.setAttribute("onduty", onduty);
request.setAttribute("hireDate", shireDate);
request.setAttribute("selAll", selAll);
request.getRequestDispatcher("/system/empList.jsp").forward(request, response); } public void selAll(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取所有的部门信息
DepartmentService ds=new DepartmentServiceImpl();
List<Department> deptList = ds.seAll();
request.setAttribute("deptList", deptList); EmployeeService es = new EmployeeServiceImpl();
List<Employee> selAll = es.selAll();
request.setAttribute("selAll", selAll);
request.getRequestDispatcher("/system/empList.jsp").forward(request, response);
} public void findsth(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取所有的部门信息
DepartmentService ds=new DepartmentServiceImpl();
List<Department> seAll = ds.seAll();
request.setAttribute("seAll", seAll);
//获取所有的岗位信息
PositionService ps=new PositionServiceImol();
List<Position> selpos = ps.selpos();
request.setAttribute("selpos", selpos);
//获取上级员工
EmployeeService empService = new EmployeeServiceImpl();
List<Employee> mgrList = empService.findEmpByType(2);//1 基层员工 2 各级管理人员
request.setAttribute("mgrList",mgrList);
//跳转到system/empAdd.jsp
request.getRequestDispatcher("/system/empAdd.jsp").forward(request, response);
} public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取员工的信息
String empId = request.getParameter("empId");
String password ="123456";
String realName = request.getParameter("realName");
String sex = request.getParameter("sex");
//日期类型的处理
String sbirthDate = request.getParameter("birthDate");
String shireDate = request.getParameter("hireDate");
String sleaveDate = request.getParameter("leaveDate"); Date birthDate= null,hireDate = null,leaveDate = null;
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
birthDate = sdf.parse(sbirthDate);
} catch (ParseException e) {
e.printStackTrace();
}
try {
hireDate = sdf.parse(shireDate);
} catch (ParseException e) {
e.printStackTrace();
}
try {
leaveDate = sdf.parse(sleaveDate);
} catch (ParseException e) {
System.out.println("暂时没有离职日期~");
}
//整数的处理
int onDuty = Integer.parseInt(request.getParameter("onDuty"));
int empType = Integer.parseInt(request.getParameter("empType")); String phone = request.getParameter("phone");
String qq = request.getParameter("qq");
String emerContactPerson = request.getParameter("emerContactPerson");
String idCard = request.getParameter("idCard"); //对象的处理
int deptno = Integer.parseInt(request.getParameter("deptno"));
Department dept = new Department();
dept.setDeptno(deptno); int posid = Integer.parseInt(request.getParameter("posid"));
Position position=new Position();
position.setPosid(posid); String mgrId = request.getParameter("mgrId");
Employee mgr = new Employee();
mgr.setEmpId(mgrId);//!!! //调用业务层完成添加操作
Employee emp = new Employee(empId, password, realName, sex, birthDate, hireDate, leaveDate, onDuty, empType, phone, qq, emerContactPerson, idCard, dept, position, mgr);
EmployeeService empService = new EmployeeServiceImpl();
int n = empService.add(emp);
//根据结果进行页面跳转
if(n>0){
response.sendRedirect(request.getContextPath()+"/system/empList.jsp");
}
else{
request.setAttribute("error", "添加员工失败");
request.getRequestDispatcher("/system/empAdd.jsp").forward(request, response);
} } }

实现效果:

OA项目之Mybatis多表链接查询的更多相关文章

  1. OA项目之mybatis动态查询

    类似于三个条件,可以全部选择,也可以选择几个条件进行查询 Mapper.xml文件: <resultMap type="Employee" id="selAll&q ...

  2. Mybatis多表链接查询重复字段问题

    A表和B表一对多的关系 A表 B表 A表和C表也是一对多关系 C表 我现在向查询出A表的所有字段和B表的name字段,C表的name字段 这是我错误的sql语句,可以看出我没有查B表和C表的id字段, ...

  3. oa项目面试准备

    熟悉项目在ssm框架下的编程流程,了解mysql html spring springmvc mybatis技术.了解过springboot编程. 在上个寒假跟着培训机构用springboot框架编写 ...

  4. 【Java EE 学习 67 上】【OA项目练习】【JBPM工作流的使用】

    OA项目中有极大可能性使用到JBPM框架解决流程控制问题,比如请假流程.报销流程等等. JBPM:JBoss Business Process Management,翻译过来就是业务流程管理.实际上就 ...

  5. Mybatis使用MySQL模糊查询时输入中文检索不到结果怎么办--转自http://www.jb51.net/article/88236.htm

    这篇文章主要介绍了Mybatis使用MySQL模糊查询时输入中文检索不到结果的解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下   项目开发中,在做Mybatis动态查询时,遇到了 ...

  6. mybatis深入之动态查询和连接池介绍

    mybatis深入之动态查询和连接池介绍 一.mybatis条件查询 在mybatis前述案例中,我们的查询条件都是确定的.但在实际使用的时候,我们的查询条件有可能是动态变化的.例如,查询参数为一个u ...

  7. MyBatis工程搭建&MyBatis实现Mapper配置查询

    一.MyMyBatis工程搭建 新建Maven项目:mybatis-demo 准备数据源 1 # 删除mybatis_demo数据库 2 drop database if exists mybatis ...

  8. MyBatis的多表查询笔记

    MyBatis的多表查询 随着学习的进步,需求的提高,我们在实际开发中用的最多的还是多表查询,就让我们一起学习MyBatis中的多表查询. 数据库准备 Class表 Student表 项目结构 这次使 ...

  9. MyBatis实现关联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

随机推荐

  1. 使用客户机和主机做DNS服务正向解析及小问题解决

    1.下载yum包 命令:yum install bind-chroot 2.更改配置文件 在这里,要了解到主配置文件为:   /etc/named.conf 但是,为了避免经常修改主配置文件named ...

  2. hdu 1087 Super Jumping! Jumping! Jumping!(动态规划DP)

    Super Jumping! Jumping! Jumping!Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. 附009.Kubernetes永久存储之GlusterFS独立部署

    一 前期准备 1.1 基础知识 Heketi提供了一个RESTful管理界面,可以用来管理GlusterFS卷的生命周期.Heketi会动态在集群内选择bricks构建所需的volumes,从而确保数 ...

  4. 《JAVA 程序员面试宝典(第四版)》之传递与引用篇

    废话开场白         这个周末突然很想创业,为什么呢?原因很简单,我周围的同学或者说玩的比较好的朋友都发达了,前一个月和一个两年前还睡在一张床上的朋友,他现在已经在深圳买房买车了,没错是在深圳买 ...

  5. 解构ffmpeg(二)

    通过比较DirectShow和ffmpeg两者的FilterGraph,分析ffmpeg的FilterGraph运作. 首先FilterGraph是一个图,图由点和边构成.在FilterGraph中的 ...

  6. opencv 3 core组件进阶(1 访问图像中的像素)

    访问图像像素的三类方法 ·方法一 指针访问:C操作符[ ]; ·方法二 迭代器iterator; ·方法三 动态地址计算. #include <opencv2/core/core.hpp> ...

  7. VueJS学习之Vue-cli项目模板

    1. 首先是学习vue时需要依赖于node.js和webpack打包工具,具体的安装如下 http://nodejs.cn/download/下载安装nodejs,具体的安装步骤不在此赘述 2. 安装 ...

  8. SpringSecurity动态加载用户角色权限实现登录及鉴权

    很多人觉得Spring Security实现登录验证很难,我最开始学习的时候也这样觉得.因为我好久都没看懂我该怎么样将自己写的用于接收用户名密码的Controller与Spring Security结 ...

  9. Flex容器拖动(Bordercontainer为例)

    Bordercontainer的拖放到任意位置. mxml: 为Bordercontainer添加鼠标按下和弹起事件 <s:BorderContainer id="bdShow&quo ...

  10. iOS开发tips-PhotoKit

    概述 PhotoKit应该是iOS 8 开始引入为了替代之前ALAssetsLibrary的相册资源访问的标准库,后者在iOS 9开始被弃用.当然相对于ALAssetsLibrary其扩展性更高,ap ...